| author | ymh <ymh.work@gmail.com> |
| Wed, 21 Sep 2022 18:19:35 +0200 | |
| changeset 18 | be944660c56a |
| parent 16 | a86126ab1dd4 |
| child 19 | 3d72ae0968f4 |
| permissions | -rw-r--r-- |
| 0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Plugin Administration API |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
10 |
* Parses the plugin contents to retrieve plugin's metadata. |
| 0 | 11 |
* |
| 16 | 12 |
* All plugin headers must be on their own line. Plugin description must not have |
13 |
* any newlines, otherwise only parts of the description will be displayed. |
|
14 |
* The below is formatted for printing. |
|
| 0 | 15 |
* |
| 5 | 16 |
* /* |
| 16 | 17 |
* Plugin Name: Name of the plugin. |
18 |
* Plugin URI: The home page of the plugin. |
|
19 |
* Description: Plugin description. |
|
20 |
* Author: Plugin author's name. |
|
21 |
* Author URI: Link to the author's website. |
|
22 |
* Version: Plugin version. |
|
| 5 | 23 |
* Text Domain: Optional. Unique identifier, should be same as the one used in |
| 16 | 24 |
* load_plugin_textdomain(). |
| 5 | 25 |
* Domain Path: Optional. Only useful if the translations are located in a |
| 9 | 26 |
* folder above the plugin's base path. For example, if .mo files are |
27 |
* located in the locale folder then Domain Path will be "/locale/" and |
|
28 |
* must have the first slash. Defaults to the base folder the plugin is |
|
29 |
* located in. |
|
| 5 | 30 |
* Network: Optional. Specify "Network: true" to require that a plugin is activated |
| 9 | 31 |
* across all sites in an installation. This will prevent a plugin from being |
32 |
* activated on a single site when Multisite is enabled. |
|
| 16 | 33 |
* Requires at least: Optional. Specify the minimum required WordPress version. |
34 |
* Requires PHP: Optional. Specify the minimum required PHP version. |
|
35 |
* * / # Remove the space to close comment. |
|
| 0 | 36 |
* |
| 16 | 37 |
* The first 8 KB of the file will be pulled in and if the plugin data is not |
38 |
* within that first 8 KB, then the plugin author should correct their plugin |
|
| 0 | 39 |
* and move the plugin data headers to the top. |
40 |
* |
|
41 |
* The plugin file is assumed to have permissions to allow for scripts to read |
|
42 |
* the file. This is not checked however and the file is only opened for |
|
43 |
* reading. |
|
44 |
* |
|
45 |
* @since 1.5.0 |
|
| 16 | 46 |
* @since 5.3.0 Added support for `Requires at least` and `Requires PHP` headers. |
| 18 | 47 |
* @since 5.8.0 Added support for `Update URI` header. |
| 0 | 48 |
* |
| 9 | 49 |
* @param string $plugin_file Absolute path to the main plugin file. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
* @param bool $markup Optional. If the returned data should have HTML markup applied. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
51 |
* Default true. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
* @param bool $translate Optional. If the returned data should be translated. Default true. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
* @return array { |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
* Plugin data. Values will be empty if not supplied by the plugin. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
* @type string $Name Name of the plugin. Should be unique. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
* @type string $Title Title of the plugin and link to the plugin's site (if set). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
* @type string $Description Plugin description. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
* @type string $Author Author's name. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
* @type string $AuthorURI Author's website address (if set). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
* @type string $Version Plugin version. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
* @type string $TextDomain Plugin textdomain. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
* @type string $DomainPath Plugins relative directory path to .mo files. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
* @type bool $Network Whether the plugin can only be activated network-wide. |
| 16 | 65 |
* @type string $RequiresWP Minimum required version of WordPress. |
66 |
* @type string $RequiresPHP Minimum required version of PHP. |
|
| 18 | 67 |
* @type string $UpdateURI ID of the plugin for update purposes, should be a URI. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
68 |
* } |
| 0 | 69 |
*/ |
70 |
function get_plugin_data( $plugin_file, $markup = true, $translate = true ) { |
|
71 |
||
72 |
$default_headers = array( |
|
| 9 | 73 |
'Name' => 'Plugin Name', |
74 |
'PluginURI' => 'Plugin URI', |
|
75 |
'Version' => 'Version', |
|
| 0 | 76 |
'Description' => 'Description', |
| 9 | 77 |
'Author' => 'Author', |
78 |
'AuthorURI' => 'Author URI', |
|
79 |
'TextDomain' => 'Text Domain', |
|
80 |
'DomainPath' => 'Domain Path', |
|
81 |
'Network' => 'Network', |
|
| 16 | 82 |
'RequiresWP' => 'Requires at least', |
83 |
'RequiresPHP' => 'Requires PHP', |
|
| 18 | 84 |
'UpdateURI' => 'Update URI', |
| 0 | 85 |
// Site Wide Only is deprecated in favor of Network. |
| 9 | 86 |
'_sitewide' => 'Site Wide Only', |
| 0 | 87 |
); |
88 |
||
89 |
$plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' ); |
|
90 |
||
| 16 | 91 |
// Site Wide Only is the old header for Network. |
| 0 | 92 |
if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
/* translators: 1: Site Wide Only: true, 2: Network: true */ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
_deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The %1$s plugin header is deprecated. Use %2$s instead.' ), '<code>Site Wide Only: true</code>', '<code>Network: true</code>' ) ); |
| 0 | 95 |
$plugin_data['Network'] = $plugin_data['_sitewide']; |
96 |
} |
|
| 16 | 97 |
$plugin_data['Network'] = ( 'true' === strtolower( $plugin_data['Network'] ) ); |
| 0 | 98 |
unset( $plugin_data['_sitewide'] ); |
99 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
100 |
// If no text domain is defined fall back to the plugin slug. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
101 |
if ( ! $plugin_data['TextDomain'] ) { |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
102 |
$plugin_slug = dirname( plugin_basename( $plugin_file ) ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
103 |
if ( '.' !== $plugin_slug && false === strpos( $plugin_slug, '/' ) ) { |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
$plugin_data['TextDomain'] = $plugin_slug; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
105 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
|
| 0 | 108 |
if ( $markup || $translate ) { |
109 |
$plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate ); |
|
110 |
} else { |
|
111 |
$plugin_data['Title'] = $plugin_data['Name']; |
|
112 |
$plugin_data['AuthorName'] = $plugin_data['Author']; |
|
113 |
} |
|
114 |
||
115 |
return $plugin_data; |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Sanitizes plugin data, optionally adds markup, optionally translates. |
|
120 |
* |
|
121 |
* @since 2.7.0 |
|
| 9 | 122 |
* |
123 |
* @see get_plugin_data() |
|
124 |
* |
|
| 0 | 125 |
* @access private |
| 9 | 126 |
* |
127 |
* @param string $plugin_file Path to the main plugin file. |
|
128 |
* @param array $plugin_data An array of plugin data. See `get_plugin_data()`. |
|
129 |
* @param bool $markup Optional. If the returned data should have HTML markup applied. |
|
130 |
* Default true. |
|
131 |
* @param bool $translate Optional. If the returned data should be translated. Default true. |
|
132 |
* @return array { |
|
133 |
* Plugin data. Values will be empty if not supplied by the plugin. |
|
134 |
* |
|
135 |
* @type string $Name Name of the plugin. Should be unique. |
|
136 |
* @type string $Title Title of the plugin and link to the plugin's site (if set). |
|
137 |
* @type string $Description Plugin description. |
|
138 |
* @type string $Author Author's name. |
|
139 |
* @type string $AuthorURI Author's website address (if set). |
|
140 |
* @type string $Version Plugin version. |
|
141 |
* @type string $TextDomain Plugin textdomain. |
|
142 |
* @type string $DomainPath Plugins relative directory path to .mo files. |
|
143 |
* @type bool $Network Whether the plugin can only be activated network-wide. |
|
144 |
* } |
|
| 0 | 145 |
*/ |
146 |
function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) { |
|
147 |
||
| 16 | 148 |
// Sanitize the plugin filename to a WP_PLUGIN_DIR relative path. |
| 0 | 149 |
$plugin_file = plugin_basename( $plugin_file ); |
150 |
||
| 16 | 151 |
// Translate fields. |
| 0 | 152 |
if ( $translate ) { |
| 16 | 153 |
$textdomain = $plugin_data['TextDomain']; |
154 |
if ( $textdomain ) { |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
155 |
if ( ! is_textdomain_loaded( $textdomain ) ) { |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
if ( $plugin_data['DomainPath'] ) { |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
} else { |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
} |
| 16 | 162 |
} elseif ( 'hello.php' === basename( $plugin_file ) ) { |
| 0 | 163 |
$textdomain = 'default'; |
164 |
} |
|
165 |
if ( $textdomain ) { |
|
| 9 | 166 |
foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field ) { |
167 |
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain |
|
| 0 | 168 |
$plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain ); |
| 9 | 169 |
} |
| 0 | 170 |
} |
171 |
} |
|
172 |
||
| 16 | 173 |
// Sanitize fields. |
| 9 | 174 |
$allowed_tags_in_links = array( |
| 0 | 175 |
'abbr' => array( 'title' => true ), |
176 |
'acronym' => array( 'title' => true ), |
|
177 |
'code' => true, |
|
178 |
'em' => true, |
|
179 |
'strong' => true, |
|
180 |
); |
|
| 9 | 181 |
|
182 |
$allowed_tags = $allowed_tags_in_links; |
|
183 |
$allowed_tags['a'] = array( |
|
184 |
'href' => true, |
|
185 |
'title' => true, |
|
186 |
); |
|
| 0 | 187 |
|
188 |
// Name is marked up inside <a> tags. Don't allow these. |
|
189 |
// Author is too, but some plugins have used <a> here (omitting Author URI). |
|
| 9 | 190 |
$plugin_data['Name'] = wp_kses( $plugin_data['Name'], $allowed_tags_in_links ); |
191 |
$plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags ); |
|
| 0 | 192 |
|
193 |
$plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags ); |
|
| 9 | 194 |
$plugin_data['Version'] = wp_kses( $plugin_data['Version'], $allowed_tags ); |
| 0 | 195 |
|
| 9 | 196 |
$plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] ); |
197 |
$plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] ); |
|
| 0 | 198 |
|
199 |
$plugin_data['Title'] = $plugin_data['Name']; |
|
200 |
$plugin_data['AuthorName'] = $plugin_data['Author']; |
|
201 |
||
| 16 | 202 |
// Apply markup. |
| 0 | 203 |
if ( $markup ) { |
| 9 | 204 |
if ( $plugin_data['PluginURI'] && $plugin_data['Name'] ) { |
| 5 | 205 |
$plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '">' . $plugin_data['Name'] . '</a>'; |
| 9 | 206 |
} |
| 0 | 207 |
|
| 9 | 208 |
if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] ) { |
| 5 | 209 |
$plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '">' . $plugin_data['Author'] . '</a>'; |
| 9 | 210 |
} |
| 0 | 211 |
|
212 |
$plugin_data['Description'] = wptexturize( $plugin_data['Description'] ); |
|
213 |
||
| 9 | 214 |
if ( $plugin_data['Author'] ) { |
| 16 | 215 |
$plugin_data['Description'] .= sprintf( |
216 |
/* translators: %s: Plugin author. */ |
|
217 |
' <cite>' . __( 'By %s.' ) . '</cite>', |
|
218 |
$plugin_data['Author'] |
|
219 |
); |
|
| 9 | 220 |
} |
| 0 | 221 |
} |
222 |
||
223 |
return $plugin_data; |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Get a list of a plugin's files. |
|
228 |
* |
|
229 |
* @since 2.8.0 |
|
230 |
* |
|
| 9 | 231 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
| 16 | 232 |
* @return string[] Array of file names relative to the plugin root. |
| 0 | 233 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
function get_plugin_files( $plugin ) { |
| 0 | 235 |
$plugin_file = WP_PLUGIN_DIR . '/' . $plugin; |
| 9 | 236 |
$dir = dirname( $plugin_file ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
$plugin_files = array( plugin_basename( $plugin_file ) ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
if ( is_dir( $dir ) && WP_PLUGIN_DIR !== $dir ) { |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
* Filters the array of excluded directories and files while scanning the folder. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
244 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
245 |
* @since 4.9.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
* |
| 9 | 247 |
* @param string[] $exclusions Array of excluded directories and files. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
$exclusions = (array) apply_filters( 'plugin_files_exclusions', array( 'CVS', 'node_modules', 'vendor', 'bower_components' ) ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
$list_files = list_files( $dir, 100, $exclusions ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
$list_files = array_map( 'plugin_basename', $list_files ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
253 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
$plugin_files = array_merge( $plugin_files, $list_files ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
$plugin_files = array_values( array_unique( $plugin_files ) ); |
| 0 | 256 |
} |
257 |
||
258 |
return $plugin_files; |
|
259 |
} |
|
260 |
||
261 |
/** |
|
262 |
* Check the plugins directory and retrieve all plugin files with plugin data. |
|
263 |
* |
|
264 |
* WordPress only supports plugin files in the base plugins directory |
|
265 |
* (wp-content/plugins) and in one directory above the plugins directory |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
* (wp-content/plugins/my-plugin). The file it looks for has the plugin data |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
* and must be found in those two locations. It is recommended to keep your |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
268 |
* plugin files in their own directories. |
| 0 | 269 |
* |
270 |
* The file with the plugin data is the file that will be included and therefore |
|
271 |
* needs to have the main execution for the plugin. This does not mean |
|
272 |
* everything must be contained in the file and it is recommended that the file |
|
273 |
* be split for maintainability. Keep everything in one file for extreme |
|
274 |
* optimization purposes. |
|
275 |
* |
|
276 |
* @since 1.5.0 |
|
277 |
* |
|
278 |
* @param string $plugin_folder Optional. Relative path to single plugin folder. |
|
| 16 | 279 |
* @return array[] Array of arrays of plugin data, keyed by plugin file name. See `get_plugin_data()`. |
| 0 | 280 |
*/ |
| 9 | 281 |
function get_plugins( $plugin_folder = '' ) { |
| 0 | 282 |
|
| 9 | 283 |
$cache_plugins = wp_cache_get( 'plugins', 'plugins' ); |
284 |
if ( ! $cache_plugins ) { |
|
| 0 | 285 |
$cache_plugins = array(); |
| 9 | 286 |
} |
| 0 | 287 |
|
| 9 | 288 |
if ( isset( $cache_plugins[ $plugin_folder ] ) ) { |
| 0 | 289 |
return $cache_plugins[ $plugin_folder ]; |
| 9 | 290 |
} |
| 0 | 291 |
|
| 9 | 292 |
$wp_plugins = array(); |
| 0 | 293 |
$plugin_root = WP_PLUGIN_DIR; |
| 9 | 294 |
if ( ! empty( $plugin_folder ) ) { |
| 0 | 295 |
$plugin_root .= $plugin_folder; |
| 9 | 296 |
} |
| 0 | 297 |
|
| 16 | 298 |
// Files in wp-content/plugins directory. |
| 18 | 299 |
$plugins_dir = @opendir( $plugin_root ); |
| 0 | 300 |
$plugin_files = array(); |
| 16 | 301 |
|
| 0 | 302 |
if ( $plugins_dir ) { |
| 9 | 303 |
while ( ( $file = readdir( $plugins_dir ) ) !== false ) { |
| 16 | 304 |
if ( '.' === substr( $file, 0, 1 ) ) { |
| 0 | 305 |
continue; |
| 9 | 306 |
} |
| 16 | 307 |
|
| 9 | 308 |
if ( is_dir( $plugin_root . '/' . $file ) ) { |
| 18 | 309 |
$plugins_subdir = @opendir( $plugin_root . '/' . $file ); |
| 16 | 310 |
|
| 0 | 311 |
if ( $plugins_subdir ) { |
| 9 | 312 |
while ( ( $subfile = readdir( $plugins_subdir ) ) !== false ) { |
| 16 | 313 |
if ( '.' === substr( $subfile, 0, 1 ) ) { |
| 0 | 314 |
continue; |
| 9 | 315 |
} |
| 16 | 316 |
|
317 |
if ( '.php' === substr( $subfile, -4 ) ) { |
|
| 0 | 318 |
$plugin_files[] = "$file/$subfile"; |
| 9 | 319 |
} |
| 0 | 320 |
} |
| 16 | 321 |
|
| 0 | 322 |
closedir( $plugins_subdir ); |
323 |
} |
|
324 |
} else { |
|
| 16 | 325 |
if ( '.php' === substr( $file, -4 ) ) { |
| 0 | 326 |
$plugin_files[] = $file; |
| 9 | 327 |
} |
| 0 | 328 |
} |
329 |
} |
|
| 16 | 330 |
|
| 0 | 331 |
closedir( $plugins_dir ); |
332 |
} |
|
333 |
||
| 9 | 334 |
if ( empty( $plugin_files ) ) { |
| 0 | 335 |
return $wp_plugins; |
| 9 | 336 |
} |
| 0 | 337 |
|
338 |
foreach ( $plugin_files as $plugin_file ) { |
|
| 9 | 339 |
if ( ! is_readable( "$plugin_root/$plugin_file" ) ) { |
| 0 | 340 |
continue; |
| 9 | 341 |
} |
| 0 | 342 |
|
| 16 | 343 |
// Do not apply markup/translate as it will be cached. |
344 |
$plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); |
|
| 0 | 345 |
|
| 9 | 346 |
if ( empty( $plugin_data['Name'] ) ) { |
| 0 | 347 |
continue; |
| 9 | 348 |
} |
| 0 | 349 |
|
| 9 | 350 |
$wp_plugins[ plugin_basename( $plugin_file ) ] = $plugin_data; |
| 0 | 351 |
} |
352 |
||
353 |
uasort( $wp_plugins, '_sort_uname_callback' ); |
|
354 |
||
355 |
$cache_plugins[ $plugin_folder ] = $wp_plugins; |
|
| 9 | 356 |
wp_cache_set( 'plugins', $cache_plugins, 'plugins' ); |
| 0 | 357 |
|
358 |
return $wp_plugins; |
|
359 |
} |
|
360 |
||
361 |
/** |
|
362 |
* Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data. |
|
363 |
* |
|
364 |
* WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins). |
|
365 |
* |
|
366 |
* @since 3.0.0 |
|
| 16 | 367 |
* @return array[] Array of arrays of mu-plugin data, keyed by plugin file name. See `get_plugin_data()`. |
| 0 | 368 |
*/ |
369 |
function get_mu_plugins() { |
|
| 16 | 370 |
$wp_plugins = array(); |
| 0 | 371 |
$plugin_files = array(); |
372 |
||
| 9 | 373 |
if ( ! is_dir( WPMU_PLUGIN_DIR ) ) { |
| 0 | 374 |
return $wp_plugins; |
| 9 | 375 |
} |
| 16 | 376 |
|
377 |
// Files in wp-content/mu-plugins directory. |
|
378 |
$plugins_dir = @opendir( WPMU_PLUGIN_DIR ); |
|
379 |
if ( $plugins_dir ) { |
|
| 0 | 380 |
while ( ( $file = readdir( $plugins_dir ) ) !== false ) { |
| 16 | 381 |
if ( '.php' === substr( $file, -4 ) ) { |
| 0 | 382 |
$plugin_files[] = $file; |
| 9 | 383 |
} |
| 0 | 384 |
} |
385 |
} else { |
|
386 |
return $wp_plugins; |
|
387 |
} |
|
388 |
||
| 16 | 389 |
closedir( $plugins_dir ); |
| 0 | 390 |
|
| 9 | 391 |
if ( empty( $plugin_files ) ) { |
| 0 | 392 |
return $wp_plugins; |
| 9 | 393 |
} |
| 0 | 394 |
|
395 |
foreach ( $plugin_files as $plugin_file ) { |
|
| 9 | 396 |
if ( ! is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) ) { |
| 0 | 397 |
continue; |
| 9 | 398 |
} |
| 0 | 399 |
|
| 16 | 400 |
// Do not apply markup/translate as it will be cached. |
401 |
$plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); |
|
| 0 | 402 |
|
| 9 | 403 |
if ( empty( $plugin_data['Name'] ) ) { |
| 0 | 404 |
$plugin_data['Name'] = $plugin_file; |
| 9 | 405 |
} |
| 0 | 406 |
|
407 |
$wp_plugins[ $plugin_file ] = $plugin_data; |
|
408 |
} |
|
409 |
||
| 16 | 410 |
if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php' ) <= 30 ) { |
411 |
// Silence is golden. |
|
| 0 | 412 |
unset( $wp_plugins['index.php'] ); |
| 9 | 413 |
} |
| 0 | 414 |
|
415 |
uasort( $wp_plugins, '_sort_uname_callback' ); |
|
416 |
||
417 |
return $wp_plugins; |
|
418 |
} |
|
419 |
||
420 |
/** |
|
421 |
* Callback to sort array by a 'Name' key. |
|
422 |
* |
|
423 |
* @since 3.1.0 |
|
| 9 | 424 |
* |
| 0 | 425 |
* @access private |
| 9 | 426 |
* |
427 |
* @param array $a array with 'Name' key. |
|
428 |
* @param array $b array with 'Name' key. |
|
429 |
* @return int Return 0 or 1 based on two string comparison. |
|
| 0 | 430 |
*/ |
431 |
function _sort_uname_callback( $a, $b ) { |
|
432 |
return strnatcasecmp( $a['Name'], $b['Name'] ); |
|
433 |
} |
|
434 |
||
435 |
/** |
|
436 |
* Check the wp-content directory and retrieve all drop-ins with any plugin data. |
|
437 |
* |
|
438 |
* @since 3.0.0 |
|
| 16 | 439 |
* @return array[] Array of arrays of dropin plugin data, keyed by plugin file name. See `get_plugin_data()`. |
| 0 | 440 |
*/ |
441 |
function get_dropins() { |
|
| 9 | 442 |
$dropins = array(); |
| 0 | 443 |
$plugin_files = array(); |
444 |
||
445 |
$_dropins = _get_dropins(); |
|
446 |
||
| 16 | 447 |
// Files in wp-content directory. |
448 |
$plugins_dir = @opendir( WP_CONTENT_DIR ); |
|
449 |
if ( $plugins_dir ) { |
|
| 0 | 450 |
while ( ( $file = readdir( $plugins_dir ) ) !== false ) { |
| 9 | 451 |
if ( isset( $_dropins[ $file ] ) ) { |
| 0 | 452 |
$plugin_files[] = $file; |
| 9 | 453 |
} |
| 0 | 454 |
} |
455 |
} else { |
|
456 |
return $dropins; |
|
457 |
} |
|
458 |
||
| 16 | 459 |
closedir( $plugins_dir ); |
| 0 | 460 |
|
| 9 | 461 |
if ( empty( $plugin_files ) ) { |
| 0 | 462 |
return $dropins; |
| 9 | 463 |
} |
| 0 | 464 |
|
465 |
foreach ( $plugin_files as $plugin_file ) { |
|
| 9 | 466 |
if ( ! is_readable( WP_CONTENT_DIR . "/$plugin_file" ) ) { |
| 0 | 467 |
continue; |
| 9 | 468 |
} |
| 16 | 469 |
|
470 |
// Do not apply markup/translate as it will be cached. |
|
471 |
$plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); |
|
472 |
||
| 9 | 473 |
if ( empty( $plugin_data['Name'] ) ) { |
| 0 | 474 |
$plugin_data['Name'] = $plugin_file; |
| 9 | 475 |
} |
| 16 | 476 |
|
| 0 | 477 |
$dropins[ $plugin_file ] = $plugin_data; |
478 |
} |
|
479 |
||
480 |
uksort( $dropins, 'strnatcasecmp' ); |
|
481 |
||
482 |
return $dropins; |
|
483 |
} |
|
484 |
||
485 |
/** |
|
486 |
* Returns drop-ins that WordPress uses. |
|
487 |
* |
|
488 |
* Includes Multisite drop-ins only when is_multisite() |
|
489 |
* |
|
490 |
* @since 3.0.0 |
|
| 16 | 491 |
* @return array[] Key is file name. The value is an array, with the first value the |
| 9 | 492 |
* purpose of the drop-in and the second value the name of the constant that must be |
493 |
* true for the drop-in to be used, or true if no constant is required. |
|
| 0 | 494 |
*/ |
495 |
function _get_dropins() { |
|
496 |
$dropins = array( |
|
| 16 | 497 |
'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE |
498 |
'db.php' => array( __( 'Custom database class.' ), true ), // Auto on load. |
|
499 |
'db-error.php' => array( __( 'Custom database error message.' ), true ), // Auto on error. |
|
500 |
'install.php' => array( __( 'Custom installation script.' ), true ), // Auto on installation. |
|
501 |
'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // Auto on maintenance. |
|
502 |
'object-cache.php' => array( __( 'External object cache.' ), true ), // Auto on load. |
|
503 |
'php-error.php' => array( __( 'Custom PHP error message.' ), true ), // Auto on error. |
|
504 |
'fatal-error-handler.php' => array( __( 'Custom PHP fatal error handler.' ), true ), // Auto on error. |
|
| 0 | 505 |
); |
506 |
||
507 |
if ( is_multisite() ) { |
|
| 9 | 508 |
$dropins['sunrise.php'] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE |
| 16 | 509 |
$dropins['blog-deleted.php'] = array( __( 'Custom site deleted message.' ), true ); // Auto on deleted blog. |
510 |
$dropins['blog-inactive.php'] = array( __( 'Custom site inactive message.' ), true ); // Auto on inactive blog. |
|
511 |
$dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // Auto on archived or spammed blog. |
|
| 0 | 512 |
} |
513 |
||
514 |
return $dropins; |
|
515 |
} |
|
516 |
||
517 |
/** |
|
| 9 | 518 |
* Determines whether a plugin is active. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
519 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
520 |
* Only plugins installed in the plugins/ folder can be active. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
522 |
* Plugins in the mu-plugins/ folder can't be "activated," so this function will |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
523 |
* return false for those plugins. |
| 0 | 524 |
* |
| 9 | 525 |
* For more information on this and similar theme functions, check out |
526 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
527 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
528 |
* |
|
| 0 | 529 |
* @since 2.5.0 |
530 |
* |
|
| 9 | 531 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
| 0 | 532 |
* @return bool True, if in the active plugins list. False, not in the list. |
533 |
*/ |
|
534 |
function is_plugin_active( $plugin ) { |
|
| 16 | 535 |
return in_array( $plugin, (array) get_option( 'active_plugins', array() ), true ) || is_plugin_active_for_network( $plugin ); |
| 0 | 536 |
} |
537 |
||
538 |
/** |
|
| 9 | 539 |
* Determines whether the plugin is inactive. |
| 0 | 540 |
* |
541 |
* Reverse of is_plugin_active(). Used as a callback. |
|
542 |
* |
|
| 9 | 543 |
* For more information on this and similar theme functions, check out |
544 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
545 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
546 |
* |
|
| 0 | 547 |
* @since 3.1.0 |
| 16 | 548 |
* |
| 0 | 549 |
* @see is_plugin_active() |
550 |
* |
|
| 9 | 551 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
| 0 | 552 |
* @return bool True if inactive. False if active. |
553 |
*/ |
|
554 |
function is_plugin_inactive( $plugin ) { |
|
555 |
return ! is_plugin_active( $plugin ); |
|
556 |
} |
|
557 |
||
558 |
/** |
|
| 9 | 559 |
* Determines whether the plugin is active for the entire network. |
| 0 | 560 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
561 |
* Only plugins installed in the plugins/ folder can be active. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
562 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
563 |
* Plugins in the mu-plugins/ folder can't be "activated," so this function will |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
564 |
* return false for those plugins. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
565 |
* |
| 9 | 566 |
* For more information on this and similar theme functions, check out |
567 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
568 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
569 |
* |
|
| 0 | 570 |
* @since 3.0.0 |
571 |
* |
|
| 9 | 572 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
573 |
* @return bool True if active for the network, otherwise false. |
|
| 0 | 574 |
*/ |
575 |
function is_plugin_active_for_network( $plugin ) { |
|
| 9 | 576 |
if ( ! is_multisite() ) { |
| 0 | 577 |
return false; |
| 9 | 578 |
} |
| 0 | 579 |
|
| 9 | 580 |
$plugins = get_site_option( 'active_sitewide_plugins' ); |
581 |
if ( isset( $plugins[ $plugin ] ) ) { |
|
| 0 | 582 |
return true; |
| 9 | 583 |
} |
| 0 | 584 |
|
585 |
return false; |
|
586 |
} |
|
587 |
||
588 |
/** |
|
589 |
* Checks for "Network: true" in the plugin header to see if this should |
|
590 |
* be activated only as a network wide plugin. The plugin would also work |
|
591 |
* when Multisite is not enabled. |
|
592 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
593 |
* Checks for "Site Wide Only: true" for backward compatibility. |
| 0 | 594 |
* |
595 |
* @since 3.0.0 |
|
596 |
* |
|
| 9 | 597 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
| 0 | 598 |
* @return bool True if plugin is network only, false otherwise. |
599 |
*/ |
|
600 |
function is_network_only_plugin( $plugin ) { |
|
601 |
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); |
|
| 9 | 602 |
if ( $plugin_data ) { |
| 0 | 603 |
return $plugin_data['Network']; |
| 9 | 604 |
} |
| 0 | 605 |
return false; |
606 |
} |
|
607 |
||
608 |
/** |
|
609 |
* Attempts activation of plugin in a "sandbox" and redirects on success. |
|
610 |
* |
|
611 |
* A plugin that is already activated will not attempt to be activated again. |
|
612 |
* |
|
613 |
* The way it works is by setting the redirection to the error before trying to |
|
614 |
* include the plugin file. If the plugin fails, then the redirection will not |
|
615 |
* be overwritten with the success message. Also, the options will not be |
|
616 |
* updated and the activation hook will not be called on plugin error. |
|
617 |
* |
|
618 |
* It should be noted that in no way the below code will actually prevent errors |
|
619 |
* within the file. The code should not be used elsewhere to replicate the |
|
620 |
* "sandbox", which uses redirection to work. |
|
621 |
* {@source 13 1} |
|
622 |
* |
|
623 |
* If any errors are found or text is outputted, then it will be captured to |
|
624 |
* ensure that the success redirection will update the error redirection. |
|
625 |
* |
|
626 |
* @since 2.5.0 |
|
| 9 | 627 |
* @since 5.2.0 Test for WordPress version and PHP version compatibility. |
| 0 | 628 |
* |
| 9 | 629 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
630 |
* @param string $redirect Optional. URL to redirect to. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
631 |
* @param bool $network_wide Optional. Whether to enable the plugin for all sites in the network |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
632 |
* or just the current site. Multisite only. Default false. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
633 |
* @param bool $silent Optional. Whether to prevent calling activation hooks. Default false. |
| 16 | 634 |
* @return null|WP_Error Null on success, WP_Error on invalid file. |
| 0 | 635 |
*/ |
636 |
function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) { |
|
637 |
$plugin = plugin_basename( trim( $plugin ) ); |
|
638 |
||
| 9 | 639 |
if ( is_multisite() && ( $network_wide || is_network_only_plugin( $plugin ) ) ) { |
640 |
$network_wide = true; |
|
641 |
$current = get_site_option( 'active_sitewide_plugins', array() ); |
|
| 0 | 642 |
$_GET['networkwide'] = 1; // Back compat for plugins looking for this value. |
643 |
} else { |
|
644 |
$current = get_option( 'active_plugins', array() ); |
|
645 |
} |
|
646 |
||
| 9 | 647 |
$valid = validate_plugin( $plugin ); |
648 |
if ( is_wp_error( $valid ) ) { |
|
| 0 | 649 |
return $valid; |
| 9 | 650 |
} |
651 |
||
652 |
$requirements = validate_plugin_requirements( $plugin ); |
|
653 |
if ( is_wp_error( $requirements ) ) { |
|
654 |
return $requirements; |
|
655 |
} |
|
| 0 | 656 |
|
| 18 | 657 |
if ( $network_wide && ! isset( $current[ $plugin ] ) |
658 |
|| ! $network_wide && ! in_array( $plugin, $current, true ) |
|
659 |
) { |
|
| 9 | 660 |
if ( ! empty( $redirect ) ) { |
| 16 | 661 |
// We'll override this later if the plugin can be included without fatal error. |
662 |
wp_redirect( add_query_arg( '_error_nonce', wp_create_nonce( 'plugin-activation-error_' . $plugin ), $redirect ) ); |
|
| 9 | 663 |
} |
664 |
||
| 0 | 665 |
ob_start(); |
| 16 | 666 |
|
| 18 | 667 |
// Load the plugin to test whether it throws any errors. |
668 |
plugin_sandbox_scrape( $plugin ); |
|
| 0 | 669 |
|
670 |
if ( ! $silent ) { |
|
671 |
/** |
|
| 5 | 672 |
* Fires before a plugin is activated. |
673 |
* |
|
674 |
* If a plugin is silently activated (such as during an update), |
|
675 |
* this hook does not fire. |
|
| 0 | 676 |
* |
677 |
* @since 2.9.0 |
|
678 |
* |
|
| 9 | 679 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
| 0 | 680 |
* @param bool $network_wide Whether to enable the plugin for all sites in the network |
| 16 | 681 |
* or just the current site. Multisite only. Default false. |
| 0 | 682 |
*/ |
683 |
do_action( 'activate_plugin', $plugin, $network_wide ); |
|
684 |
||
685 |
/** |
|
| 5 | 686 |
* Fires as a specific plugin is being activated. |
| 0 | 687 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
688 |
* This hook is the "activation" hook used internally by register_activation_hook(). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
689 |
* The dynamic portion of the hook name, `$plugin`, refers to the plugin basename. |
| 5 | 690 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
691 |
* If a plugin is silently activated (such as during an update), this hook does not fire. |
| 0 | 692 |
* |
693 |
* @since 2.0.0 |
|
694 |
* |
|
695 |
* @param bool $network_wide Whether to enable the plugin for all sites in the network |
|
| 16 | 696 |
* or just the current site. Multisite only. Default false. |
| 0 | 697 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
698 |
do_action( "activate_{$plugin}", $network_wide ); |
| 0 | 699 |
} |
700 |
||
701 |
if ( $network_wide ) { |
|
| 9 | 702 |
$current = get_site_option( 'active_sitewide_plugins', array() ); |
703 |
$current[ $plugin ] = time(); |
|
| 0 | 704 |
update_site_option( 'active_sitewide_plugins', $current ); |
705 |
} else { |
|
| 9 | 706 |
$current = get_option( 'active_plugins', array() ); |
| 0 | 707 |
$current[] = $plugin; |
| 9 | 708 |
sort( $current ); |
709 |
update_option( 'active_plugins', $current ); |
|
| 0 | 710 |
} |
711 |
||
712 |
if ( ! $silent ) { |
|
713 |
/** |
|
| 5 | 714 |
* Fires after a plugin has been activated. |
715 |
* |
|
716 |
* If a plugin is silently activated (such as during an update), |
|
717 |
* this hook does not fire. |
|
| 0 | 718 |
* |
719 |
* @since 2.9.0 |
|
720 |
* |
|
| 9 | 721 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
| 0 | 722 |
* @param bool $network_wide Whether to enable the plugin for all sites in the network |
| 16 | 723 |
* or just the current site. Multisite only. Default false. |
| 0 | 724 |
*/ |
725 |
do_action( 'activated_plugin', $plugin, $network_wide ); |
|
726 |
} |
|
727 |
||
728 |
if ( ob_get_length() > 0 ) { |
|
729 |
$output = ob_get_clean(); |
|
| 9 | 730 |
return new WP_Error( 'unexpected_output', __( 'The plugin generated unexpected output.' ), $output ); |
| 0 | 731 |
} |
| 18 | 732 |
|
| 0 | 733 |
ob_end_clean(); |
734 |
} |
|
735 |
||
736 |
return null; |
|
737 |
} |
|
738 |
||
739 |
/** |
|
740 |
* Deactivate a single plugin or multiple plugins. |
|
741 |
* |
|
742 |
* The deactivation hook is disabled by the plugin upgrader by using the $silent |
|
743 |
* parameter. |
|
744 |
* |
|
745 |
* @since 2.5.0 |
|
746 |
* |
|
| 16 | 747 |
* @param string|string[] $plugins Single plugin or list of plugins to deactivate. |
748 |
* @param bool $silent Prevent calling deactivation hooks. Default false. |
|
749 |
* @param bool|null $network_wide Whether to deactivate the plugin for all sites in the network. |
|
750 |
* A value of null will deactivate plugins for both the network |
|
751 |
* and the current site. Multisite only. Default null. |
|
| 0 | 752 |
*/ |
753 |
function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) { |
|
| 9 | 754 |
if ( is_multisite() ) { |
| 0 | 755 |
$network_current = get_site_option( 'active_sitewide_plugins', array() ); |
| 9 | 756 |
} |
| 16 | 757 |
$current = get_option( 'active_plugins', array() ); |
758 |
$do_blog = false; |
|
759 |
$do_network = false; |
|
| 0 | 760 |
|
761 |
foreach ( (array) $plugins as $plugin ) { |
|
762 |
$plugin = plugin_basename( trim( $plugin ) ); |
|
| 9 | 763 |
if ( ! is_plugin_active( $plugin ) ) { |
| 0 | 764 |
continue; |
| 9 | 765 |
} |
| 0 | 766 |
|
| 18 | 767 |
$network_deactivating = ( false !== $network_wide ) && is_plugin_active_for_network( $plugin ); |
| 0 | 768 |
|
| 5 | 769 |
if ( ! $silent ) { |
| 0 | 770 |
/** |
| 5 | 771 |
* Fires before a plugin is deactivated. |
772 |
* |
|
773 |
* If a plugin is silently deactivated (such as during an update), |
|
774 |
* this hook does not fire. |
|
| 0 | 775 |
* |
776 |
* @since 2.9.0 |
|
777 |
* |
|
| 9 | 778 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
| 0 | 779 |
* @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network |
| 16 | 780 |
* or just the current site. Multisite only. Default false. |
| 0 | 781 |
*/ |
782 |
do_action( 'deactivate_plugin', $plugin, $network_deactivating ); |
|
| 5 | 783 |
} |
| 0 | 784 |
|
785 |
if ( false !== $network_wide ) { |
|
786 |
if ( is_plugin_active_for_network( $plugin ) ) { |
|
787 |
$do_network = true; |
|
788 |
unset( $network_current[ $plugin ] ); |
|
789 |
} elseif ( $network_wide ) { |
|
790 |
continue; |
|
791 |
} |
|
792 |
} |
|
793 |
||
794 |
if ( true !== $network_wide ) { |
|
| 16 | 795 |
$key = array_search( $plugin, $current, true ); |
| 0 | 796 |
if ( false !== $key ) { |
797 |
$do_blog = true; |
|
798 |
unset( $current[ $key ] ); |
|
799 |
} |
|
800 |
} |
|
801 |
||
| 9 | 802 |
if ( $do_blog && wp_is_recovery_mode() ) { |
803 |
list( $extension ) = explode( '/', $plugin ); |
|
804 |
wp_paused_plugins()->delete( $extension ); |
|
805 |
} |
|
806 |
||
| 0 | 807 |
if ( ! $silent ) { |
808 |
/** |
|
| 5 | 809 |
* Fires as a specific plugin is being deactivated. |
| 0 | 810 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
811 |
* This hook is the "deactivation" hook used internally by register_deactivation_hook(). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
812 |
* The dynamic portion of the hook name, `$plugin`, refers to the plugin basename. |
| 5 | 813 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
814 |
* If a plugin is silently deactivated (such as during an update), this hook does not fire. |
| 0 | 815 |
* |
816 |
* @since 2.0.0 |
|
817 |
* |
|
818 |
* @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network |
|
| 16 | 819 |
* or just the current site. Multisite only. Default false. |
| 0 | 820 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
821 |
do_action( "deactivate_{$plugin}", $network_deactivating ); |
| 0 | 822 |
|
823 |
/** |
|
| 5 | 824 |
* Fires after a plugin is deactivated. |
825 |
* |
|
826 |
* If a plugin is silently deactivated (such as during an update), |
|
827 |
* this hook does not fire. |
|
| 0 | 828 |
* |
829 |
* @since 2.9.0 |
|
830 |
* |
|
| 9 | 831 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
| 16 | 832 |
* @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network |
| 5 | 833 |
* or just the current site. Multisite only. Default false. |
| 0 | 834 |
*/ |
835 |
do_action( 'deactivated_plugin', $plugin, $network_deactivating ); |
|
836 |
} |
|
837 |
} |
|
838 |
||
| 9 | 839 |
if ( $do_blog ) { |
840 |
update_option( 'active_plugins', $current ); |
|
841 |
} |
|
842 |
if ( $do_network ) { |
|
| 0 | 843 |
update_site_option( 'active_sitewide_plugins', $network_current ); |
| 9 | 844 |
} |
| 0 | 845 |
} |
846 |
||
847 |
/** |
|
848 |
* Activate multiple plugins. |
|
849 |
* |
|
850 |
* When WP_Error is returned, it does not mean that one of the plugins had |
|
| 16 | 851 |
* errors. It means that one or more of the plugin file paths were invalid. |
| 0 | 852 |
* |
853 |
* The execution will be halted as soon as one of the plugins has an error. |
|
854 |
* |
|
855 |
* @since 2.6.0 |
|
856 |
* |
|
| 16 | 857 |
* @param string|string[] $plugins Single plugin or list of plugins to activate. |
858 |
* @param string $redirect Redirect to page after successful activation. |
|
859 |
* @param bool $network_wide Whether to enable the plugin for all sites in the network. |
|
860 |
* Default false. |
|
861 |
* @param bool $silent Prevent calling activation hooks. Default false. |
|
| 0 | 862 |
* @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation. |
863 |
*/ |
|
864 |
function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) { |
|
| 9 | 865 |
if ( ! is_array( $plugins ) ) { |
866 |
$plugins = array( $plugins ); |
|
867 |
} |
|
| 0 | 868 |
|
869 |
$errors = array(); |
|
870 |
foreach ( $plugins as $plugin ) { |
|
| 9 | 871 |
if ( ! empty( $redirect ) ) { |
872 |
$redirect = add_query_arg( 'plugin', $plugin, $redirect ); |
|
873 |
} |
|
874 |
$result = activate_plugin( $plugin, $redirect, $network_wide, $silent ); |
|
875 |
if ( is_wp_error( $result ) ) { |
|
876 |
$errors[ $plugin ] = $result; |
|
877 |
} |
|
| 0 | 878 |
} |
879 |
||
| 9 | 880 |
if ( ! empty( $errors ) ) { |
881 |
return new WP_Error( 'plugins_invalid', __( 'One of the plugins is invalid.' ), $errors ); |
|
882 |
} |
|
| 0 | 883 |
|
884 |
return true; |
|
885 |
} |
|
886 |
||
887 |
/** |
|
| 5 | 888 |
* Remove directory and files of a plugin for a list of plugins. |
| 0 | 889 |
* |
890 |
* @since 2.6.0 |
|
891 |
* |
|
| 9 | 892 |
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
893 |
* |
| 9 | 894 |
* @param string[] $plugins List of plugin paths to delete, relative to the plugins directory. |
895 |
* @param string $deprecated Not used. |
|
896 |
* @return bool|null|WP_Error True on success, false if `$plugins` is empty, `WP_Error` on failure. |
|
897 |
* `null` if filesystem credentials are required to proceed. |
|
| 0 | 898 |
*/ |
| 5 | 899 |
function delete_plugins( $plugins, $deprecated = '' ) { |
| 0 | 900 |
global $wp_filesystem; |
901 |
||
| 9 | 902 |
if ( empty( $plugins ) ) { |
| 0 | 903 |
return false; |
| 9 | 904 |
} |
| 0 | 905 |
|
906 |
$checked = array(); |
|
| 9 | 907 |
foreach ( $plugins as $plugin ) { |
| 0 | 908 |
$checked[] = 'checked[]=' . $plugin; |
| 9 | 909 |
} |
| 0 | 910 |
|
| 9 | 911 |
$url = wp_nonce_url( 'plugins.php?action=delete-selected&verify-delete=1&' . implode( '&', $checked ), 'bulk-plugins' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
912 |
|
| 0 | 913 |
ob_start(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
914 |
$credentials = request_filesystem_credentials( $url ); |
| 9 | 915 |
$data = ob_get_clean(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
916 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
917 |
if ( false === $credentials ) { |
| 9 | 918 |
if ( ! empty( $data ) ) { |
| 16 | 919 |
require_once ABSPATH . 'wp-admin/admin-header.php'; |
| 0 | 920 |
echo $data; |
| 16 | 921 |
require_once ABSPATH . 'wp-admin/admin-footer.php'; |
| 0 | 922 |
exit; |
923 |
} |
|
924 |
return; |
|
925 |
} |
|
926 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
927 |
if ( ! WP_Filesystem( $credentials ) ) { |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
928 |
ob_start(); |
| 16 | 929 |
// Failed to connect. Error and request again. |
930 |
request_filesystem_credentials( $url, '', true ); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
931 |
$data = ob_get_clean(); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
932 |
|
| 9 | 933 |
if ( ! empty( $data ) ) { |
| 16 | 934 |
require_once ABSPATH . 'wp-admin/admin-header.php'; |
| 0 | 935 |
echo $data; |
| 16 | 936 |
require_once ABSPATH . 'wp-admin/admin-footer.php'; |
| 0 | 937 |
exit; |
938 |
} |
|
939 |
return; |
|
940 |
} |
|
941 |
||
| 9 | 942 |
if ( ! is_object( $wp_filesystem ) ) { |
943 |
return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) ); |
|
944 |
} |
|
| 0 | 945 |
|
| 9 | 946 |
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { |
947 |
return new WP_Error( 'fs_error', __( 'Filesystem error.' ), $wp_filesystem->errors ); |
|
948 |
} |
|
| 0 | 949 |
|
| 5 | 950 |
// Get the base plugin folder. |
| 0 | 951 |
$plugins_dir = $wp_filesystem->wp_plugins_dir(); |
| 5 | 952 |
if ( empty( $plugins_dir ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
953 |
return new WP_Error( 'fs_no_plugins_dir', __( 'Unable to locate WordPress plugin directory.' ) ); |
| 5 | 954 |
} |
| 0 | 955 |
|
956 |
$plugins_dir = trailingslashit( $plugins_dir ); |
|
957 |
||
| 5 | 958 |
$plugin_translations = wp_get_installed_translations( 'plugins' ); |
959 |
||
| 0 | 960 |
$errors = array(); |
961 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
962 |
foreach ( $plugins as $plugin_file ) { |
| 5 | 963 |
// Run Uninstall hook. |
964 |
if ( is_uninstallable_plugin( $plugin_file ) ) { |
|
| 9 | 965 |
uninstall_plugin( $plugin_file ); |
| 5 | 966 |
} |
| 0 | 967 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
968 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
969 |
* Fires immediately before a plugin deletion attempt. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
970 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
971 |
* @since 4.4.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
* |
| 9 | 973 |
* @param string $plugin_file Path to the plugin file relative to the plugins directory. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
974 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
975 |
do_action( 'delete_plugin', $plugin_file ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
976 |
|
| 5 | 977 |
$this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin_file ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
978 |
|
| 0 | 979 |
// If plugin is in its own directory, recursively delete the directory. |
| 16 | 980 |
// Base check on if plugin includes directory separator AND that it's not the root plugin folder. |
| 18 | 981 |
if ( strpos( $plugin_file, '/' ) && $this_plugin_dir !== $plugins_dir ) { |
| 5 | 982 |
$deleted = $wp_filesystem->delete( $this_plugin_dir, true ); |
983 |
} else { |
|
984 |
$deleted = $wp_filesystem->delete( $plugins_dir . $plugin_file ); |
|
985 |
} |
|
986 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
987 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
988 |
* Fires immediately after a plugin deletion attempt. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
989 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
990 |
* @since 4.4.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
991 |
* |
| 9 | 992 |
* @param string $plugin_file Path to the plugin file relative to the plugins directory. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
993 |
* @param bool $deleted Whether the plugin deletion was successful. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
994 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
995 |
do_action( 'deleted_plugin', $plugin_file, $deleted ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
996 |
|
| 5 | 997 |
if ( ! $deleted ) { |
998 |
$errors[] = $plugin_file; |
|
999 |
continue; |
|
1000 |
} |
|
1001 |
||
| 18 | 1002 |
$plugin_slug = dirname( $plugin_file ); |
1003 |
||
1004 |
if ( 'hello.php' === $plugin_file ) { |
|
1005 |
$plugin_slug = 'hello-dolly'; |
|
1006 |
} |
|
1007 |
||
| 5 | 1008 |
// Remove language files, silently. |
1009 |
if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) { |
|
1010 |
$translations = $plugin_translations[ $plugin_slug ]; |
|
| 0 | 1011 |
|
| 5 | 1012 |
foreach ( $translations as $translation => $data ) { |
1013 |
$wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.po' ); |
|
1014 |
$wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.mo' ); |
|
| 9 | 1015 |
|
1016 |
$json_translation_files = glob( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '-*.json' ); |
|
1017 |
if ( $json_translation_files ) { |
|
1018 |
array_map( array( $wp_filesystem, 'delete' ), $json_translation_files ); |
|
1019 |
} |
|
| 5 | 1020 |
} |
1021 |
} |
|
1022 |
} |
|
1023 |
||
1024 |
// Remove deleted plugins from the plugin updates list. |
|
| 16 | 1025 |
$current = get_site_transient( 'update_plugins' ); |
1026 |
if ( $current ) { |
|
| 5 | 1027 |
// Don't remove the plugins that weren't deleted. |
1028 |
$deleted = array_diff( $plugins, $errors ); |
|
1029 |
||
1030 |
foreach ( $deleted as $plugin_file ) { |
|
1031 |
unset( $current->response[ $plugin_file ] ); |
|
1032 |
} |
|
1033 |
||
1034 |
set_site_transient( 'update_plugins', $current ); |
|
| 0 | 1035 |
} |
1036 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1037 |
if ( ! empty( $errors ) ) { |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1038 |
if ( 1 === count( $errors ) ) { |
| 16 | 1039 |
/* translators: %s: Plugin filename. */ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1040 |
$message = __( 'Could not fully remove the plugin %s.' ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1041 |
} else { |
| 16 | 1042 |
/* translators: %s: Comma-separated list of plugin filenames. */ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1043 |
$message = __( 'Could not fully remove the plugins %s.' ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1044 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1045 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1046 |
return new WP_Error( 'could_not_remove_plugin', sprintf( $message, implode( ', ', $errors ) ) ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1047 |
} |
| 0 | 1048 |
|
1049 |
return true; |
|
1050 |
} |
|
1051 |
||
1052 |
/** |
|
1053 |
* Validate active plugins |
|
1054 |
* |
|
1055 |
* Validate all active plugins, deactivates invalid and |
|
1056 |
* returns an array of deactivated ones. |
|
1057 |
* |
|
1058 |
* @since 2.5.0 |
|
| 16 | 1059 |
* @return WP_Error[] Array of plugin errors keyed by plugin file name. |
| 0 | 1060 |
*/ |
1061 |
function validate_active_plugins() { |
|
1062 |
$plugins = get_option( 'active_plugins', array() ); |
|
| 5 | 1063 |
// Validate vartype: array. |
| 0 | 1064 |
if ( ! is_array( $plugins ) ) { |
1065 |
update_option( 'active_plugins', array() ); |
|
1066 |
$plugins = array(); |
|
1067 |
} |
|
1068 |
||
| 5 | 1069 |
if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) { |
| 0 | 1070 |
$network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() ); |
| 9 | 1071 |
$plugins = array_merge( $plugins, array_keys( $network_plugins ) ); |
| 0 | 1072 |
} |
1073 |
||
| 9 | 1074 |
if ( empty( $plugins ) ) { |
| 5 | 1075 |
return array(); |
| 9 | 1076 |
} |
| 0 | 1077 |
|
1078 |
$invalid = array(); |
|
1079 |
||
| 5 | 1080 |
// Invalid plugins get deactivated. |
| 0 | 1081 |
foreach ( $plugins as $plugin ) { |
1082 |
$result = validate_plugin( $plugin ); |
|
1083 |
if ( is_wp_error( $result ) ) { |
|
| 9 | 1084 |
$invalid[ $plugin ] = $result; |
| 0 | 1085 |
deactivate_plugins( $plugin, true ); |
1086 |
} |
|
1087 |
} |
|
1088 |
return $invalid; |
|
1089 |
} |
|
1090 |
||
1091 |
/** |
|
1092 |
* Validate the plugin path. |
|
1093 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1094 |
* Checks that the main plugin file exists and is a valid plugin. See validate_file(). |
| 0 | 1095 |
* |
1096 |
* @since 2.5.0 |
|
1097 |
* |
|
| 9 | 1098 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
| 16 | 1099 |
* @return int|WP_Error 0 on success, WP_Error on failure. |
| 0 | 1100 |
*/ |
| 9 | 1101 |
function validate_plugin( $plugin ) { |
1102 |
if ( validate_file( $plugin ) ) { |
|
1103 |
return new WP_Error( 'plugin_invalid', __( 'Invalid plugin path.' ) ); |
|
1104 |
} |
|
1105 |
if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) { |
|
1106 |
return new WP_Error( 'plugin_not_found', __( 'Plugin file does not exist.' ) ); |
|
1107 |
} |
|
| 0 | 1108 |
|
1109 |
$installed_plugins = get_plugins(); |
|
| 9 | 1110 |
if ( ! isset( $installed_plugins[ $plugin ] ) ) { |
1111 |
return new WP_Error( 'no_plugin_header', __( 'The plugin does not have a valid header.' ) ); |
|
1112 |
} |
|
| 0 | 1113 |
return 0; |
1114 |
} |
|
1115 |
||
1116 |
/** |
|
| 16 | 1117 |
* Validates the plugin requirements for WordPress version and PHP version. |
1118 |
* |
|
1119 |
* Uses the information from `Requires at least` and `Requires PHP` headers |
|
1120 |
* defined in the plugin's main PHP file. |
|
1121 |
* |
|
| 9 | 1122 |
* @since 5.2.0 |
| 16 | 1123 |
* @since 5.3.0 Added support for reading the headers from the plugin's |
1124 |
* main PHP file, with `readme.txt` as a fallback. |
|
| 18 | 1125 |
* @since 5.8.0 Removed support for using `readme.txt` as a fallback. |
| 9 | 1126 |
* |
1127 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
|
1128 |
* @return true|WP_Error True if requirements are met, WP_Error on failure. |
|
1129 |
*/ |
|
1130 |
function validate_plugin_requirements( $plugin ) { |
|
| 16 | 1131 |
$plugin_headers = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); |
1132 |
||
1133 |
$requirements = array( |
|
1134 |
'requires' => ! empty( $plugin_headers['RequiresWP'] ) ? $plugin_headers['RequiresWP'] : '', |
|
1135 |
'requires_php' => ! empty( $plugin_headers['RequiresPHP'] ) ? $plugin_headers['RequiresPHP'] : '', |
|
1136 |
); |
|
1137 |
||
1138 |
$compatible_wp = is_wp_version_compatible( $requirements['requires'] ); |
|
1139 |
$compatible_php = is_php_version_compatible( $requirements['requires_php'] ); |
|
| 9 | 1140 |
|
| 16 | 1141 |
$php_update_message = '</p><p>' . sprintf( |
| 18 | 1142 |
/* translators: %s: URL to Update PHP page. */ |
| 16 | 1143 |
__( '<a href="%s">Learn more about updating PHP</a>.' ), |
1144 |
esc_url( wp_get_update_php_url() ) |
|
1145 |
); |
|
| 9 | 1146 |
|
| 16 | 1147 |
$annotation = wp_get_update_php_annotation(); |
1148 |
||
1149 |
if ( $annotation ) { |
|
1150 |
$php_update_message .= '</p><p><em>' . $annotation . '</em>'; |
|
1151 |
} |
|
1152 |
||
1153 |
if ( ! $compatible_wp && ! $compatible_php ) { |
|
| 9 | 1154 |
return new WP_Error( |
1155 |
'plugin_wp_php_incompatible', |
|
| 16 | 1156 |
'<p>' . sprintf( |
1157 |
/* translators: 1: Current WordPress version, 2: Current PHP version, 3: Plugin name, 4: Required WordPress version, 5: Required PHP version. */ |
|
1158 |
_x( '<strong>Error:</strong> Current versions of WordPress (%1$s) and PHP (%2$s) do not meet minimum requirements for %3$s. The plugin requires WordPress %4$s and PHP %5$s.', 'plugin' ), |
|
1159 |
get_bloginfo( 'version' ), |
|
1160 |
phpversion(), |
|
1161 |
$plugin_headers['Name'], |
|
1162 |
$requirements['requires'], |
|
1163 |
$requirements['requires_php'] |
|
1164 |
) . $php_update_message . '</p>' |
|
| 9 | 1165 |
); |
| 16 | 1166 |
} elseif ( ! $compatible_php ) { |
| 9 | 1167 |
return new WP_Error( |
1168 |
'plugin_php_incompatible', |
|
| 16 | 1169 |
'<p>' . sprintf( |
1170 |
/* translators: 1: Current PHP version, 2: Plugin name, 3: Required PHP version. */ |
|
1171 |
_x( '<strong>Error:</strong> Current PHP version (%1$s) does not meet minimum requirements for %2$s. The plugin requires PHP %3$s.', 'plugin' ), |
|
1172 |
phpversion(), |
|
1173 |
$plugin_headers['Name'], |
|
1174 |
$requirements['requires_php'] |
|
1175 |
) . $php_update_message . '</p>' |
|
| 9 | 1176 |
); |
| 16 | 1177 |
} elseif ( ! $compatible_wp ) { |
| 9 | 1178 |
return new WP_Error( |
1179 |
'plugin_wp_incompatible', |
|
| 16 | 1180 |
'<p>' . sprintf( |
1181 |
/* translators: 1: Current WordPress version, 2: Plugin name, 3: Required WordPress version. */ |
|
1182 |
_x( '<strong>Error:</strong> Current WordPress version (%1$s) does not meet minimum requirements for %2$s. The plugin requires WordPress %3$s.', 'plugin' ), |
|
1183 |
get_bloginfo( 'version' ), |
|
1184 |
$plugin_headers['Name'], |
|
1185 |
$requirements['requires'] |
|
1186 |
) . '</p>' |
|
| 9 | 1187 |
); |
1188 |
} |
|
1189 |
||
1190 |
return true; |
|
1191 |
} |
|
1192 |
||
1193 |
/** |
|
| 0 | 1194 |
* Whether the plugin can be uninstalled. |
1195 |
* |
|
1196 |
* @since 2.7.0 |
|
1197 |
* |
|
| 9 | 1198 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
| 0 | 1199 |
* @return bool Whether plugin can be uninstalled. |
1200 |
*/ |
|
| 9 | 1201 |
function is_uninstallable_plugin( $plugin ) { |
1202 |
$file = plugin_basename( $plugin ); |
|
| 0 | 1203 |
|
| 9 | 1204 |
$uninstallable_plugins = (array) get_option( 'uninstall_plugins' ); |
1205 |
if ( isset( $uninstallable_plugins[ $file ] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php' ) ) { |
|
| 0 | 1206 |
return true; |
| 9 | 1207 |
} |
| 0 | 1208 |
|
1209 |
return false; |
|
1210 |
} |
|
1211 |
||
1212 |
/** |
|
1213 |
* Uninstall a single plugin. |
|
1214 |
* |
|
1215 |
* Calls the uninstall hook, if it is available. |
|
1216 |
* |
|
1217 |
* @since 2.7.0 |
|
1218 |
* |
|
| 9 | 1219 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
| 18 | 1220 |
* @return true|void True if a plugin's uninstall.php file has been found and included. |
1221 |
* Void otherwise. |
|
| 0 | 1222 |
*/ |
| 9 | 1223 |
function uninstall_plugin( $plugin ) { |
1224 |
$file = plugin_basename( $plugin ); |
|
| 0 | 1225 |
|
| 9 | 1226 |
$uninstallable_plugins = (array) get_option( 'uninstall_plugins' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1227 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1228 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1229 |
* Fires in uninstall_plugin() immediately before the plugin is uninstalled. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1230 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1231 |
* @since 4.5.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1232 |
* |
| 9 | 1233 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1234 |
* @param array $uninstallable_plugins Uninstallable plugins. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1235 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1236 |
do_action( 'pre_uninstall_plugin', $plugin, $uninstallable_plugins ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1237 |
|
| 9 | 1238 |
if ( file_exists( WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php' ) ) { |
1239 |
if ( isset( $uninstallable_plugins[ $file ] ) ) { |
|
1240 |
unset( $uninstallable_plugins[ $file ] ); |
|
1241 |
update_option( 'uninstall_plugins', $uninstallable_plugins ); |
|
| 0 | 1242 |
} |
| 9 | 1243 |
unset( $uninstallable_plugins ); |
| 0 | 1244 |
|
| 9 | 1245 |
define( 'WP_UNINSTALL_PLUGIN', $file ); |
| 16 | 1246 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1247 |
wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file ); |
| 16 | 1248 |
include_once WP_PLUGIN_DIR . '/' . dirname( $file ) . '/uninstall.php'; |
| 0 | 1249 |
|
1250 |
return true; |
|
1251 |
} |
|
1252 |
||
| 9 | 1253 |
if ( isset( $uninstallable_plugins[ $file ] ) ) { |
1254 |
$callable = $uninstallable_plugins[ $file ]; |
|
1255 |
unset( $uninstallable_plugins[ $file ] ); |
|
1256 |
update_option( 'uninstall_plugins', $uninstallable_plugins ); |
|
1257 |
unset( $uninstallable_plugins ); |
|
| 0 | 1258 |
|
| 5 | 1259 |
wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file ); |
| 16 | 1260 |
include_once WP_PLUGIN_DIR . '/' . $file; |
| 0 | 1261 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1262 |
add_action( "uninstall_{$file}", $callable ); |
| 0 | 1263 |
|
1264 |
/** |
|
1265 |
* Fires in uninstall_plugin() once the plugin has been uninstalled. |
|
1266 |
* |
|
1267 |
* The action concatenates the 'uninstall_' prefix with the basename of the |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1268 |
* plugin passed to uninstall_plugin() to create a dynamically-named action. |
| 0 | 1269 |
* |
1270 |
* @since 2.7.0 |
|
1271 |
*/ |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1272 |
do_action( "uninstall_{$file}" ); |
| 0 | 1273 |
} |
1274 |
} |
|
1275 |
||
1276 |
// |
|
| 16 | 1277 |
// Menu. |
| 0 | 1278 |
// |
1279 |
||
1280 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1281 |
* Add a top-level menu page. |
| 0 | 1282 |
* |
1283 |
* This function takes a capability which will be used to determine whether |
|
1284 |
* or not a page is included in the menu. |
|
1285 |
* |
|
1286 |
* The function which is hooked in to handle the output of the page must check |
|
1287 |
* that the user has the required capability as well. |
|
1288 |
* |
|
| 9 | 1289 |
* @since 1.5.0 |
1290 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1291 |
* @global array $menu |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1292 |
* @global array $admin_page_hooks |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1293 |
* @global array $_registered_pages |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1294 |
* @global array $_parent_pages |
| 0 | 1295 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1296 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1297 |
* @param string $menu_title The text to be used for the menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1298 |
* @param string $capability The capability required for this menu to be displayed to the user. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1299 |
* @param string $menu_slug The slug name to refer to this menu by. Should be unique for this menu page and only |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1300 |
* include lowercase alphanumeric, dashes, and underscores characters to be compatible |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1301 |
* with sanitize_key(). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1302 |
* @param callable $function The function to be called to output the content for this page. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1303 |
* @param string $icon_url The URL to the icon to be used for this menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1304 |
* * Pass a base64-encoded SVG using a data URI, which will be colored to match |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1305 |
* the color scheme. This should begin with 'data:image/svg+xml;base64,'. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1306 |
* * Pass the name of a Dashicons helper class to use a font icon, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1307 |
* e.g. 'dashicons-chart-pie'. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1308 |
* * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS. |
| 16 | 1309 |
* @param int $position The position in the menu order this item should appear. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1310 |
* @return string The resulting page's hook_suffix. |
| 0 | 1311 |
*/ |
1312 |
function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) { |
|
1313 |
global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages; |
|
1314 |
||
1315 |
$menu_slug = plugin_basename( $menu_slug ); |
|
1316 |
||
| 9 | 1317 |
$admin_page_hooks[ $menu_slug ] = sanitize_title( $menu_title ); |
| 0 | 1318 |
|
1319 |
$hookname = get_plugin_page_hookname( $menu_slug, '' ); |
|
1320 |
||
| 9 | 1321 |
if ( ! empty( $function ) && ! empty( $hookname ) && current_user_can( $capability ) ) { |
| 0 | 1322 |
add_action( $hookname, $function ); |
| 9 | 1323 |
} |
| 0 | 1324 |
|
| 9 | 1325 |
if ( empty( $icon_url ) ) { |
1326 |
$icon_url = 'dashicons-admin-generic'; |
|
| 0 | 1327 |
$icon_class = 'menu-icon-generic '; |
1328 |
} else { |
|
| 9 | 1329 |
$icon_url = set_url_scheme( $icon_url ); |
| 0 | 1330 |
$icon_class = ''; |
1331 |
} |
|
1332 |
||
1333 |
$new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url ); |
|
1334 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1335 |
if ( null === $position ) { |
| 0 | 1336 |
$menu[] = $new_menu; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1337 |
} elseif ( isset( $menu[ "$position" ] ) ) { |
| 9 | 1338 |
$position = $position + substr( base_convert( md5( $menu_slug . $menu_title ), 16, 10 ), -5 ) * 0.00001; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1339 |
$menu[ "$position" ] = $new_menu; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1340 |
} else { |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1341 |
$menu[ $position ] = $new_menu; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1342 |
} |
| 0 | 1343 |
|
| 9 | 1344 |
$_registered_pages[ $hookname ] = true; |
| 0 | 1345 |
|
| 16 | 1346 |
// No parent as top level. |
| 9 | 1347 |
$_parent_pages[ $menu_slug ] = false; |
| 0 | 1348 |
|
1349 |
return $hookname; |
|
1350 |
} |
|
1351 |
||
1352 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1353 |
* Add a submenu page. |
| 0 | 1354 |
* |
1355 |
* This function takes a capability which will be used to determine whether |
|
1356 |
* or not a page is included in the menu. |
|
1357 |
* |
|
1358 |
* The function which is hooked in to handle the output of the page must check |
|
1359 |
* that the user has the required capability as well. |
|
1360 |
* |
|
| 9 | 1361 |
* @since 1.5.0 |
| 16 | 1362 |
* @since 5.3.0 Added the `$position` parameter. |
| 9 | 1363 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1364 |
* @global array $submenu |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1365 |
* @global array $menu |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1366 |
* @global array $_wp_real_parent_file |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1367 |
* @global bool $_wp_submenu_nopriv |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1368 |
* @global array $_registered_pages |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1369 |
* @global array $_parent_pages |
| 0 | 1370 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1371 |
* @param string $parent_slug The slug name for the parent menu (or the file name of a standard |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1372 |
* WordPress admin page). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1373 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1374 |
* is selected. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1375 |
* @param string $menu_title The text to be used for the menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1376 |
* @param string $capability The capability required for this menu to be displayed to the user. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1377 |
* @param string $menu_slug The slug name to refer to this menu by. Should be unique for this menu |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1378 |
* and only include lowercase alphanumeric, dashes, and underscores characters |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1379 |
* to be compatible with sanitize_key(). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1380 |
* @param callable $function The function to be called to output the content for this page. |
| 16 | 1381 |
* @param int $position The position in the menu order this item should appear. |
1382 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
| 0 | 1383 |
*/ |
| 16 | 1384 |
function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1385 |
global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1386 |
$_registered_pages, $_parent_pages; |
| 0 | 1387 |
|
| 9 | 1388 |
$menu_slug = plugin_basename( $menu_slug ); |
1389 |
$parent_slug = plugin_basename( $parent_slug ); |
|
| 0 | 1390 |
|
| 9 | 1391 |
if ( isset( $_wp_real_parent_file[ $parent_slug ] ) ) { |
1392 |
$parent_slug = $_wp_real_parent_file[ $parent_slug ]; |
|
1393 |
} |
|
| 0 | 1394 |
|
| 9 | 1395 |
if ( ! current_user_can( $capability ) ) { |
1396 |
$_wp_submenu_nopriv[ $parent_slug ][ $menu_slug ] = true; |
|
| 0 | 1397 |
return false; |
1398 |
} |
|
1399 |
||
| 5 | 1400 |
/* |
1401 |
* If the parent doesn't already have a submenu, add a link to the parent |
|
1402 |
* as the first item in the submenu. If the submenu file is the same as the |
|
1403 |
* parent file someone is trying to link back to the parent manually. In |
|
1404 |
* this case, don't automatically add a link back to avoid duplication. |
|
1405 |
*/ |
|
| 18 | 1406 |
if ( ! isset( $submenu[ $parent_slug ] ) && $menu_slug !== $parent_slug ) { |
| 9 | 1407 |
foreach ( (array) $menu as $parent_menu ) { |
| 18 | 1408 |
if ( $parent_menu[2] === $parent_slug && current_user_can( $parent_menu[1] ) ) { |
| 9 | 1409 |
$submenu[ $parent_slug ][] = array_slice( $parent_menu, 0, 4 ); |
1410 |
} |
|
| 0 | 1411 |
} |
1412 |
} |
|
1413 |
||
| 16 | 1414 |
$new_sub_menu = array( $menu_title, $capability, $menu_slug, $page_title ); |
1415 |
if ( ! is_int( $position ) ) { |
|
1416 |
if ( null !== $position ) { |
|
1417 |
_doing_it_wrong( |
|
1418 |
__FUNCTION__, |
|
1419 |
sprintf( |
|
1420 |
/* translators: %s: add_submenu_page() */ |
|
1421 |
__( 'The seventh parameter passed to %s should be an integer representing menu position.' ), |
|
1422 |
'<code>add_submenu_page()</code>' |
|
1423 |
), |
|
1424 |
'5.3.0' |
|
1425 |
); |
|
1426 |
} |
|
1427 |
||
1428 |
$submenu[ $parent_slug ][] = $new_sub_menu; |
|
1429 |
} else { |
|
1430 |
// Append the submenu if the parent item is not present in the submenu, |
|
1431 |
// or if position is equal or higher than the number of items in the array. |
|
1432 |
if ( ! isset( $submenu[ $parent_slug ] ) || $position >= count( $submenu[ $parent_slug ] ) ) { |
|
1433 |
$submenu[ $parent_slug ][] = $new_sub_menu; |
|
1434 |
} else { |
|
1435 |
// Test for a negative position. |
|
1436 |
$position = max( $position, 0 ); |
|
1437 |
if ( 0 === $position ) { |
|
1438 |
// For negative or `0` positions, prepend the submenu. |
|
1439 |
array_unshift( $submenu[ $parent_slug ], $new_sub_menu ); |
|
1440 |
} else { |
|
1441 |
// Grab all of the items before the insertion point. |
|
1442 |
$before_items = array_slice( $submenu[ $parent_slug ], 0, $position, true ); |
|
1443 |
// Grab all of the items after the insertion point. |
|
1444 |
$after_items = array_slice( $submenu[ $parent_slug ], $position, null, true ); |
|
1445 |
// Add the new item. |
|
1446 |
$before_items[] = $new_sub_menu; |
|
1447 |
// Merge the items. |
|
1448 |
$submenu[ $parent_slug ] = array_merge( $before_items, $after_items ); |
|
1449 |
} |
|
1450 |
} |
|
1451 |
} |
|
1452 |
// Sort the parent array. |
|
1453 |
ksort( $submenu[ $parent_slug ] ); |
|
| 0 | 1454 |
|
| 9 | 1455 |
$hookname = get_plugin_page_hookname( $menu_slug, $parent_slug ); |
1456 |
if ( ! empty( $function ) && ! empty( $hookname ) ) { |
|
| 0 | 1457 |
add_action( $hookname, $function ); |
| 9 | 1458 |
} |
| 0 | 1459 |
|
| 9 | 1460 |
$_registered_pages[ $hookname ] = true; |
| 5 | 1461 |
|
1462 |
/* |
|
| 9 | 1463 |
* Backward-compatibility for plugins using add_management_page(). |
1464 |
* See wp-admin/admin.php for redirect from edit.php to tools.php. |
|
| 5 | 1465 |
*/ |
| 16 | 1466 |
if ( 'tools.php' === $parent_slug ) { |
| 9 | 1467 |
$_registered_pages[ get_plugin_page_hookname( $menu_slug, 'edit.php' ) ] = true; |
1468 |
} |
|
| 0 | 1469 |
|
| 5 | 1470 |
// No parent as top level. |
| 9 | 1471 |
$_parent_pages[ $menu_slug ] = $parent_slug; |
| 0 | 1472 |
|
1473 |
return $hookname; |
|
1474 |
} |
|
1475 |
||
1476 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1477 |
* Add submenu page to the Tools main menu. |
| 0 | 1478 |
* |
1479 |
* This function takes a capability which will be used to determine whether |
|
1480 |
* or not a page is included in the menu. |
|
1481 |
* |
|
1482 |
* The function which is hooked in to handle the output of the page must check |
|
1483 |
* that the user has the required capability as well. |
|
1484 |
* |
|
| 9 | 1485 |
* @since 1.5.0 |
| 16 | 1486 |
* @since 5.3.0 Added the `$position` parameter. |
| 9 | 1487 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1488 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1489 |
* @param string $menu_title The text to be used for the menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1490 |
* @param string $capability The capability required for this menu to be displayed to the user. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1491 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1492 |
* @param callable $function The function to be called to output the content for this page. |
| 16 | 1493 |
* @param int $position The position in the menu order this item should appear. |
1494 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
| 0 | 1495 |
*/ |
| 16 | 1496 |
function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1497 |
return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
| 0 | 1498 |
} |
1499 |
||
1500 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1501 |
* Add submenu page to the Settings main menu. |
| 0 | 1502 |
* |
1503 |
* This function takes a capability which will be used to determine whether |
|
1504 |
* or not a page is included in the menu. |
|
1505 |
* |
|
1506 |
* The function which is hooked in to handle the output of the page must check |
|
1507 |
* that the user has the required capability as well. |
|
1508 |
* |
|
| 9 | 1509 |
* @since 1.5.0 |
| 16 | 1510 |
* @since 5.3.0 Added the `$position` parameter. |
| 9 | 1511 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1512 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1513 |
* @param string $menu_title The text to be used for the menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1514 |
* @param string $capability The capability required for this menu to be displayed to the user. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1515 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1516 |
* @param callable $function The function to be called to output the content for this page. |
| 16 | 1517 |
* @param int $position The position in the menu order this item should appear. |
1518 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
| 0 | 1519 |
*/ |
| 16 | 1520 |
function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1521 |
return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
| 0 | 1522 |
} |
1523 |
||
1524 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1525 |
* Add submenu page to the Appearance main menu. |
| 0 | 1526 |
* |
1527 |
* This function takes a capability which will be used to determine whether |
|
1528 |
* or not a page is included in the menu. |
|
1529 |
* |
|
1530 |
* The function which is hooked in to handle the output of the page must check |
|
1531 |
* that the user has the required capability as well. |
|
1532 |
* |
|
| 9 | 1533 |
* @since 2.0.0 |
| 16 | 1534 |
* @since 5.3.0 Added the `$position` parameter. |
| 9 | 1535 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1536 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1537 |
* @param string $menu_title The text to be used for the menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1538 |
* @param string $capability The capability required for this menu to be displayed to the user. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1539 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1540 |
* @param callable $function The function to be called to output the content for this page. |
| 16 | 1541 |
* @param int $position The position in the menu order this item should appear. |
1542 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
| 0 | 1543 |
*/ |
| 16 | 1544 |
function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1545 |
return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
| 0 | 1546 |
} |
1547 |
||
1548 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1549 |
* Add submenu page to the Plugins main menu. |
| 0 | 1550 |
* |
1551 |
* This function takes a capability which will be used to determine whether |
|
1552 |
* or not a page is included in the menu. |
|
1553 |
* |
|
1554 |
* The function which is hooked in to handle the output of the page must check |
|
1555 |
* that the user has the required capability as well. |
|
1556 |
* |
|
| 9 | 1557 |
* @since 3.0.0 |
| 16 | 1558 |
* @since 5.3.0 Added the `$position` parameter. |
| 9 | 1559 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1560 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1561 |
* @param string $menu_title The text to be used for the menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1562 |
* @param string $capability The capability required for this menu to be displayed to the user. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1563 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1564 |
* @param callable $function The function to be called to output the content for this page. |
| 16 | 1565 |
* @param int $position The position in the menu order this item should appear. |
1566 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
| 0 | 1567 |
*/ |
| 16 | 1568 |
function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1569 |
return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
| 0 | 1570 |
} |
1571 |
||
1572 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1573 |
* Add submenu page to the Users/Profile main menu. |
| 0 | 1574 |
* |
1575 |
* This function takes a capability which will be used to determine whether |
|
1576 |
* or not a page is included in the menu. |
|
1577 |
* |
|
1578 |
* The function which is hooked in to handle the output of the page must check |
|
1579 |
* that the user has the required capability as well. |
|
1580 |
* |
|
| 9 | 1581 |
* @since 2.1.3 |
| 16 | 1582 |
* @since 5.3.0 Added the `$position` parameter. |
| 9 | 1583 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1584 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1585 |
* @param string $menu_title The text to be used for the menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1586 |
* @param string $capability The capability required for this menu to be displayed to the user. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1587 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1588 |
* @param callable $function The function to be called to output the content for this page. |
| 16 | 1589 |
* @param int $position The position in the menu order this item should appear. |
1590 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
| 0 | 1591 |
*/ |
| 16 | 1592 |
function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
| 9 | 1593 |
if ( current_user_can( 'edit_users' ) ) { |
| 0 | 1594 |
$parent = 'users.php'; |
| 9 | 1595 |
} else { |
| 0 | 1596 |
$parent = 'profile.php'; |
| 9 | 1597 |
} |
| 16 | 1598 |
return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
| 0 | 1599 |
} |
| 16 | 1600 |
|
| 0 | 1601 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1602 |
* Add submenu page to the Dashboard main menu. |
| 0 | 1603 |
* |
1604 |
* This function takes a capability which will be used to determine whether |
|
1605 |
* or not a page is included in the menu. |
|
1606 |
* |
|
1607 |
* The function which is hooked in to handle the output of the page must check |
|
1608 |
* that the user has the required capability as well. |
|
1609 |
* |
|
| 9 | 1610 |
* @since 2.7.0 |
| 16 | 1611 |
* @since 5.3.0 Added the `$position` parameter. |
| 9 | 1612 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1613 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1614 |
* @param string $menu_title The text to be used for the menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1615 |
* @param string $capability The capability required for this menu to be displayed to the user. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1616 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1617 |
* @param callable $function The function to be called to output the content for this page. |
| 16 | 1618 |
* @param int $position The position in the menu order this item should appear. |
1619 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
| 0 | 1620 |
*/ |
| 16 | 1621 |
function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1622 |
return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
| 0 | 1623 |
} |
1624 |
||
1625 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1626 |
* Add submenu page to the Posts main menu. |
| 0 | 1627 |
* |
1628 |
* This function takes a capability which will be used to determine whether |
|
1629 |
* or not a page is included in the menu. |
|
1630 |
* |
|
1631 |
* The function which is hooked in to handle the output of the page must check |
|
1632 |
* that the user has the required capability as well. |
|
1633 |
* |
|
| 9 | 1634 |
* @since 2.7.0 |
| 16 | 1635 |
* @since 5.3.0 Added the `$position` parameter. |
| 9 | 1636 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1637 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1638 |
* @param string $menu_title The text to be used for the menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1639 |
* @param string $capability The capability required for this menu to be displayed to the user. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1640 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1641 |
* @param callable $function The function to be called to output the content for this page. |
| 16 | 1642 |
* @param int $position The position in the menu order this item should appear. |
1643 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
| 0 | 1644 |
*/ |
| 16 | 1645 |
function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1646 |
return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
| 0 | 1647 |
} |
1648 |
||
1649 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1650 |
* Add submenu page to the Media main menu. |
| 0 | 1651 |
* |
1652 |
* This function takes a capability which will be used to determine whether |
|
1653 |
* or not a page is included in the menu. |
|
1654 |
* |
|
1655 |
* The function which is hooked in to handle the output of the page must check |
|
1656 |
* that the user has the required capability as well. |
|
1657 |
* |
|
| 9 | 1658 |
* @since 2.7.0 |
| 16 | 1659 |
* @since 5.3.0 Added the `$position` parameter. |
| 9 | 1660 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1661 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1662 |
* @param string $menu_title The text to be used for the menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1663 |
* @param string $capability The capability required for this menu to be displayed to the user. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1664 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1665 |
* @param callable $function The function to be called to output the content for this page. |
| 16 | 1666 |
* @param int $position The position in the menu order this item should appear. |
1667 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
| 0 | 1668 |
*/ |
| 16 | 1669 |
function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1670 |
return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
| 0 | 1671 |
} |
1672 |
||
1673 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1674 |
* Add submenu page to the Links main menu. |
| 0 | 1675 |
* |
1676 |
* This function takes a capability which will be used to determine whether |
|
1677 |
* or not a page is included in the menu. |
|
1678 |
* |
|
1679 |
* The function which is hooked in to handle the output of the page must check |
|
1680 |
* that the user has the required capability as well. |
|
1681 |
* |
|
| 9 | 1682 |
* @since 2.7.0 |
| 16 | 1683 |
* @since 5.3.0 Added the `$position` parameter. |
| 9 | 1684 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1685 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1686 |
* @param string $menu_title The text to be used for the menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1687 |
* @param string $capability The capability required for this menu to be displayed to the user. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1688 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1689 |
* @param callable $function The function to be called to output the content for this page. |
| 16 | 1690 |
* @param int $position The position in the menu order this item should appear. |
1691 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
| 0 | 1692 |
*/ |
| 16 | 1693 |
function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1694 |
return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
| 0 | 1695 |
} |
1696 |
||
1697 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1698 |
* Add submenu page to the Pages main menu. |
| 0 | 1699 |
* |
1700 |
* This function takes a capability which will be used to determine whether |
|
1701 |
* or not a page is included in the menu. |
|
1702 |
* |
|
1703 |
* The function which is hooked in to handle the output of the page must check |
|
1704 |
* that the user has the required capability as well. |
|
1705 |
* |
|
| 9 | 1706 |
* @since 2.7.0 |
| 16 | 1707 |
* @since 5.3.0 Added the `$position` parameter. |
| 9 | 1708 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1709 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1710 |
* @param string $menu_title The text to be used for the menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1711 |
* @param string $capability The capability required for this menu to be displayed to the user. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1712 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1713 |
* @param callable $function The function to be called to output the content for this page. |
| 16 | 1714 |
* @param int $position The position in the menu order this item should appear. |
1715 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1716 |
*/ |
| 16 | 1717 |
function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1718 |
return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
| 0 | 1719 |
} |
1720 |
||
1721 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1722 |
* Add submenu page to the Comments main menu. |
| 0 | 1723 |
* |
1724 |
* This function takes a capability which will be used to determine whether |
|
1725 |
* or not a page is included in the menu. |
|
1726 |
* |
|
1727 |
* The function which is hooked in to handle the output of the page must check |
|
1728 |
* that the user has the required capability as well. |
|
1729 |
* |
|
| 9 | 1730 |
* @since 2.7.0 |
| 16 | 1731 |
* @since 5.3.0 Added the `$position` parameter. |
| 9 | 1732 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1733 |
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1734 |
* @param string $menu_title The text to be used for the menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1735 |
* @param string $capability The capability required for this menu to be displayed to the user. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1736 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1737 |
* @param callable $function The function to be called to output the content for this page. |
| 16 | 1738 |
* @param int $position The position in the menu order this item should appear. |
1739 |
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1740 |
*/ |
| 16 | 1741 |
function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) { |
1742 |
return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function, $position ); |
|
| 0 | 1743 |
} |
1744 |
||
1745 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1746 |
* Remove a top-level admin menu. |
| 0 | 1747 |
* |
1748 |
* @since 3.1.0 |
|
1749 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1750 |
* @global array $menu |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1751 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1752 |
* @param string $menu_slug The slug of the menu. |
| 18 | 1753 |
* @return array|false The removed menu on success, false if not found. |
| 0 | 1754 |
*/ |
1755 |
function remove_menu_page( $menu_slug ) { |
|
1756 |
global $menu; |
|
1757 |
||
1758 |
foreach ( $menu as $i => $item ) { |
|
| 18 | 1759 |
if ( $menu_slug === $item[2] ) { |
| 9 | 1760 |
unset( $menu[ $i ] ); |
| 0 | 1761 |
return $item; |
1762 |
} |
|
1763 |
} |
|
1764 |
||
1765 |
return false; |
|
1766 |
} |
|
1767 |
||
1768 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1769 |
* Remove an admin submenu. |
| 0 | 1770 |
* |
1771 |
* @since 3.1.0 |
|
1772 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1773 |
* @global array $submenu |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1774 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1775 |
* @param string $menu_slug The slug for the parent menu. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1776 |
* @param string $submenu_slug The slug of the submenu. |
| 18 | 1777 |
* @return array|false The removed submenu on success, false if not found. |
| 0 | 1778 |
*/ |
1779 |
function remove_submenu_page( $menu_slug, $submenu_slug ) { |
|
1780 |
global $submenu; |
|
1781 |
||
| 9 | 1782 |
if ( ! isset( $submenu[ $menu_slug ] ) ) { |
| 0 | 1783 |
return false; |
| 9 | 1784 |
} |
| 0 | 1785 |
|
| 9 | 1786 |
foreach ( $submenu[ $menu_slug ] as $i => $item ) { |
| 18 | 1787 |
if ( $submenu_slug === $item[2] ) { |
| 9 | 1788 |
unset( $submenu[ $menu_slug ][ $i ] ); |
| 0 | 1789 |
return $item; |
1790 |
} |
|
1791 |
} |
|
1792 |
||
1793 |
return false; |
|
1794 |
} |
|
1795 |
||
1796 |
/** |
|
| 16 | 1797 |
* Get the URL to access a particular menu page based on the slug it was registered with. |
| 0 | 1798 |
* |
| 16 | 1799 |
* If the slug hasn't been registered properly, no URL will be returned. |
| 0 | 1800 |
* |
| 5 | 1801 |
* @since 3.0.0 |
| 0 | 1802 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1803 |
* @global array $_parent_pages |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1804 |
* |
| 16 | 1805 |
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
1806 |
* @param bool $echo Whether or not to echo the URL. Default true. |
|
1807 |
* @return string The menu page URL. |
|
| 0 | 1808 |
*/ |
| 9 | 1809 |
function menu_page_url( $menu_slug, $echo = true ) { |
| 0 | 1810 |
global $_parent_pages; |
1811 |
||
| 9 | 1812 |
if ( isset( $_parent_pages[ $menu_slug ] ) ) { |
1813 |
$parent_slug = $_parent_pages[ $menu_slug ]; |
|
| 18 | 1814 |
|
| 9 | 1815 |
if ( $parent_slug && ! isset( $_parent_pages[ $parent_slug ] ) ) { |
| 0 | 1816 |
$url = admin_url( add_query_arg( 'page', $menu_slug, $parent_slug ) ); |
1817 |
} else { |
|
1818 |
$url = admin_url( 'admin.php?page=' . $menu_slug ); |
|
1819 |
} |
|
1820 |
} else { |
|
1821 |
$url = ''; |
|
1822 |
} |
|
1823 |
||
| 9 | 1824 |
$url = esc_url( $url ); |
| 0 | 1825 |
|
| 9 | 1826 |
if ( $echo ) { |
| 0 | 1827 |
echo $url; |
| 9 | 1828 |
} |
| 0 | 1829 |
|
1830 |
return $url; |
|
1831 |
} |
|
1832 |
||
1833 |
// |
|
| 16 | 1834 |
// Pluggable Menu Support -- Private. |
| 0 | 1835 |
// |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1836 |
/** |
| 16 | 1837 |
* Gets the parent file of the current admin page. |
1838 |
* |
|
1839 |
* @since 1.5.0 |
|
1840 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1841 |
* @global string $parent_file |
| 16 | 1842 |
* @global array $menu |
1843 |
* @global array $submenu |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1844 |
* @global string $pagenow |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1845 |
* @global string $typenow |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1846 |
* @global string $plugin_page |
| 16 | 1847 |
* @global array $_wp_real_parent_file |
1848 |
* @global array $_wp_menu_nopriv |
|
1849 |
* @global array $_wp_submenu_nopriv |
|
| 9 | 1850 |
* |
| 16 | 1851 |
* @param string $parent The slug name for the parent menu (or the file name of a standard |
1852 |
* WordPress admin page). Default empty string. |
|
1853 |
* @return string The parent file of the current admin page. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1854 |
*/ |
| 0 | 1855 |
function get_admin_page_parent( $parent = '' ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1856 |
global $parent_file, $menu, $submenu, $pagenow, $typenow, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1857 |
$plugin_page, $_wp_real_parent_file, $_wp_menu_nopriv, $_wp_submenu_nopriv; |
| 0 | 1858 |
|
| 16 | 1859 |
if ( ! empty( $parent ) && 'admin.php' !== $parent ) { |
| 9 | 1860 |
if ( isset( $_wp_real_parent_file[ $parent ] ) ) { |
1861 |
$parent = $_wp_real_parent_file[ $parent ]; |
|
1862 |
} |
|
| 18 | 1863 |
|
| 0 | 1864 |
return $parent; |
1865 |
} |
|
1866 |
||
| 16 | 1867 |
if ( 'admin.php' === $pagenow && isset( $plugin_page ) ) { |
| 9 | 1868 |
foreach ( (array) $menu as $parent_menu ) { |
| 18 | 1869 |
if ( $parent_menu[2] === $plugin_page ) { |
| 0 | 1870 |
$parent_file = $plugin_page; |
| 18 | 1871 |
|
| 9 | 1872 |
if ( isset( $_wp_real_parent_file[ $parent_file ] ) ) { |
1873 |
$parent_file = $_wp_real_parent_file[ $parent_file ]; |
|
1874 |
} |
|
| 18 | 1875 |
|
| 0 | 1876 |
return $parent_file; |
1877 |
} |
|
1878 |
} |
|
| 9 | 1879 |
if ( isset( $_wp_menu_nopriv[ $plugin_page ] ) ) { |
| 0 | 1880 |
$parent_file = $plugin_page; |
| 18 | 1881 |
|
| 9 | 1882 |
if ( isset( $_wp_real_parent_file[ $parent_file ] ) ) { |
1883 |
$parent_file = $_wp_real_parent_file[ $parent_file ]; |
|
1884 |
} |
|
| 18 | 1885 |
|
| 0 | 1886 |
return $parent_file; |
1887 |
} |
|
1888 |
} |
|
1889 |
||
| 9 | 1890 |
if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $pagenow ][ $plugin_page ] ) ) { |
| 0 | 1891 |
$parent_file = $pagenow; |
| 18 | 1892 |
|
| 9 | 1893 |
if ( isset( $_wp_real_parent_file[ $parent_file ] ) ) { |
1894 |
$parent_file = $_wp_real_parent_file[ $parent_file ]; |
|
1895 |
} |
|
| 18 | 1896 |
|
| 0 | 1897 |
return $parent_file; |
1898 |
} |
|
1899 |
||
| 9 | 1900 |
foreach ( array_keys( (array) $submenu ) as $parent ) { |
1901 |
foreach ( $submenu[ $parent ] as $submenu_array ) { |
|
1902 |
if ( isset( $_wp_real_parent_file[ $parent ] ) ) { |
|
1903 |
$parent = $_wp_real_parent_file[ $parent ]; |
|
1904 |
} |
|
| 18 | 1905 |
|
1906 |
if ( ! empty( $typenow ) && "$pagenow?post_type=$typenow" === $submenu_array[2] ) { |
|
| 0 | 1907 |
$parent_file = $parent; |
1908 |
return $parent; |
|
| 18 | 1909 |
} elseif ( empty( $typenow ) && $pagenow === $submenu_array[2] |
1910 |
&& ( empty( $parent_file ) || false === strpos( $parent_file, '?' ) ) |
|
1911 |
) { |
|
| 0 | 1912 |
$parent_file = $parent; |
1913 |
return $parent; |
|
| 18 | 1914 |
} elseif ( isset( $plugin_page ) && $plugin_page === $submenu_array[2] ) { |
| 5 | 1915 |
$parent_file = $parent; |
1916 |
return $parent; |
|
1917 |
} |
|
| 0 | 1918 |
} |
1919 |
} |
|
1920 |
||
| 9 | 1921 |
if ( empty( $parent_file ) ) { |
| 0 | 1922 |
$parent_file = ''; |
| 9 | 1923 |
} |
| 0 | 1924 |
return ''; |
1925 |
} |
|
1926 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1927 |
/** |
| 16 | 1928 |
* Gets the title of the current admin page. |
1929 |
* |
|
1930 |
* @since 1.5.0 |
|
1931 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1932 |
* @global string $title |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1933 |
* @global array $menu |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1934 |
* @global array $submenu |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1935 |
* @global string $pagenow |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1936 |
* @global string $plugin_page |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1937 |
* @global string $typenow |
| 9 | 1938 |
* |
| 16 | 1939 |
* @return string The title of the current admin page. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1940 |
*/ |
| 0 | 1941 |
function get_admin_page_title() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1942 |
global $title, $menu, $submenu, $pagenow, $plugin_page, $typenow; |
| 0 | 1943 |
|
| 9 | 1944 |
if ( ! empty( $title ) ) { |
| 0 | 1945 |
return $title; |
| 9 | 1946 |
} |
| 0 | 1947 |
|
1948 |
$hook = get_plugin_page_hook( $plugin_page, $pagenow ); |
|
1949 |
||
| 16 | 1950 |
$parent = get_admin_page_parent(); |
1951 |
$parent1 = $parent; |
|
| 0 | 1952 |
|
| 9 | 1953 |
if ( empty( $parent ) ) { |
1954 |
foreach ( (array) $menu as $menu_array ) { |
|
| 0 | 1955 |
if ( isset( $menu_array[3] ) ) { |
| 18 | 1956 |
if ( $menu_array[2] === $pagenow ) { |
| 0 | 1957 |
$title = $menu_array[3]; |
1958 |
return $menu_array[3]; |
|
| 18 | 1959 |
} elseif ( isset( $plugin_page ) && $plugin_page === $menu_array[2] && $hook === $menu_array[5] ) { |
| 5 | 1960 |
$title = $menu_array[3]; |
1961 |
return $menu_array[3]; |
|
1962 |
} |
|
| 0 | 1963 |
} else { |
1964 |
$title = $menu_array[0]; |
|
1965 |
return $title; |
|
1966 |
} |
|
1967 |
} |
|
1968 |
} else { |
|
1969 |
foreach ( array_keys( $submenu ) as $parent ) { |
|
| 9 | 1970 |
foreach ( $submenu[ $parent ] as $submenu_array ) { |
| 18 | 1971 |
if ( isset( $plugin_page ) |
1972 |
&& $plugin_page === $submenu_array[2] |
|
1973 |
&& ( $pagenow === $parent |
|
1974 |
|| $plugin_page === $parent |
|
1975 |
|| $plugin_page === $hook |
|
1976 |
|| 'admin.php' === $pagenow && $parent1 !== $submenu_array[2] |
|
1977 |
|| ! empty( $typenow ) && "$pagenow?post_type=$typenow" === $parent ) |
|
| 0 | 1978 |
) { |
1979 |
$title = $submenu_array[3]; |
|
1980 |
return $submenu_array[3]; |
|
| 9 | 1981 |
} |
| 0 | 1982 |
|
| 18 | 1983 |
if ( $submenu_array[2] !== $pagenow || isset( $_GET['page'] ) ) { // Not the current page. |
| 0 | 1984 |
continue; |
| 9 | 1985 |
} |
| 0 | 1986 |
|
1987 |
if ( isset( $submenu_array[3] ) ) { |
|
1988 |
$title = $submenu_array[3]; |
|
1989 |
return $submenu_array[3]; |
|
1990 |
} else { |
|
1991 |
$title = $submenu_array[0]; |
|
1992 |
return $title; |
|
1993 |
} |
|
1994 |
} |
|
1995 |
} |
|
| 9 | 1996 |
if ( empty( $title ) ) { |
| 0 | 1997 |
foreach ( $menu as $menu_array ) { |
| 18 | 1998 |
if ( isset( $plugin_page ) |
1999 |
&& $plugin_page === $menu_array[2] |
|
2000 |
&& 'admin.php' === $pagenow |
|
2001 |
&& $parent1 === $menu_array[2] |
|
2002 |
) { |
|
| 0 | 2003 |
$title = $menu_array[3]; |
2004 |
return $menu_array[3]; |
|
| 9 | 2005 |
} |
| 0 | 2006 |
} |
2007 |
} |
|
2008 |
} |
|
2009 |
||
2010 |
return $title; |
|
2011 |
} |
|
2012 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2013 |
/** |
| 16 | 2014 |
* Gets the hook attached to the administrative page of a plugin. |
2015 |
* |
|
2016 |
* @since 1.5.0 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2017 |
* |
| 9 | 2018 |
* @param string $plugin_page The slug name of the plugin page. |
2019 |
* @param string $parent_page The slug name for the parent menu (or the file name of a standard |
|
2020 |
* WordPress admin page). |
|
2021 |
* @return string|null Hook attached to the plugin page, null otherwise. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2022 |
*/ |
| 0 | 2023 |
function get_plugin_page_hook( $plugin_page, $parent_page ) { |
2024 |
$hook = get_plugin_page_hookname( $plugin_page, $parent_page ); |
|
| 9 | 2025 |
if ( has_action( $hook ) ) { |
| 0 | 2026 |
return $hook; |
| 9 | 2027 |
} else { |
| 0 | 2028 |
return null; |
| 9 | 2029 |
} |
| 0 | 2030 |
} |
2031 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2032 |
/** |
| 16 | 2033 |
* Gets the hook name for the administrative page of a plugin. |
2034 |
* |
|
2035 |
* @since 1.5.0 |
|
2036 |
* |
|
| 9 | 2037 |
* @global array $admin_page_hooks |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2038 |
* |
| 9 | 2039 |
* @param string $plugin_page The slug name of the plugin page. |
2040 |
* @param string $parent_page The slug name for the parent menu (or the file name of a standard |
|
2041 |
* WordPress admin page). |
|
2042 |
* @return string Hook name for the plugin page. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2043 |
*/ |
| 0 | 2044 |
function get_plugin_page_hookname( $plugin_page, $parent_page ) { |
2045 |
global $admin_page_hooks; |
|
2046 |
||
2047 |
$parent = get_admin_page_parent( $parent_page ); |
|
2048 |
||
2049 |
$page_type = 'admin'; |
|
| 16 | 2050 |
if ( empty( $parent_page ) || 'admin.php' === $parent_page || isset( $admin_page_hooks[ $plugin_page ] ) ) { |
| 9 | 2051 |
if ( isset( $admin_page_hooks[ $plugin_page ] ) ) { |
| 0 | 2052 |
$page_type = 'toplevel'; |
| 9 | 2053 |
} elseif ( isset( $admin_page_hooks[ $parent ] ) ) { |
2054 |
$page_type = $admin_page_hooks[ $parent ]; |
|
| 5 | 2055 |
} |
| 9 | 2056 |
} elseif ( isset( $admin_page_hooks[ $parent ] ) ) { |
2057 |
$page_type = $admin_page_hooks[ $parent ]; |
|
| 0 | 2058 |
} |
2059 |
||
2060 |
$plugin_name = preg_replace( '!\.php!', '', $plugin_page ); |
|
2061 |
||
2062 |
return $page_type . '_page_' . $plugin_name; |
|
2063 |
} |
|
2064 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2065 |
/** |
| 16 | 2066 |
* Determines whether the current user can access the current admin page. |
2067 |
* |
|
2068 |
* @since 1.5.0 |
|
2069 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2070 |
* @global string $pagenow |
| 16 | 2071 |
* @global array $menu |
2072 |
* @global array $submenu |
|
2073 |
* @global array $_wp_menu_nopriv |
|
2074 |
* @global array $_wp_submenu_nopriv |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2075 |
* @global string $plugin_page |
| 16 | 2076 |
* @global array $_registered_pages |
| 9 | 2077 |
* |
| 16 | 2078 |
* @return bool True if the current user can access the admin page, false otherwise. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2079 |
*/ |
| 0 | 2080 |
function user_can_access_admin_page() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2081 |
global $pagenow, $menu, $submenu, $_wp_menu_nopriv, $_wp_submenu_nopriv, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2082 |
$plugin_page, $_registered_pages; |
| 0 | 2083 |
|
2084 |
$parent = get_admin_page_parent(); |
|
2085 |
||
| 9 | 2086 |
if ( ! isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $parent ][ $pagenow ] ) ) { |
| 0 | 2087 |
return false; |
| 9 | 2088 |
} |
| 0 | 2089 |
|
2090 |
if ( isset( $plugin_page ) ) { |
|
| 9 | 2091 |
if ( isset( $_wp_submenu_nopriv[ $parent ][ $plugin_page ] ) ) { |
| 0 | 2092 |
return false; |
| 9 | 2093 |
} |
| 0 | 2094 |
|
| 9 | 2095 |
$hookname = get_plugin_page_hookname( $plugin_page, $parent ); |
| 0 | 2096 |
|
| 9 | 2097 |
if ( ! isset( $_registered_pages[ $hookname ] ) ) { |
| 0 | 2098 |
return false; |
| 9 | 2099 |
} |
| 0 | 2100 |
} |
2101 |
||
| 9 | 2102 |
if ( empty( $parent ) ) { |
2103 |
if ( isset( $_wp_menu_nopriv[ $pagenow ] ) ) { |
|
| 0 | 2104 |
return false; |
| 9 | 2105 |
} |
2106 |
if ( isset( $_wp_submenu_nopriv[ $pagenow ][ $pagenow ] ) ) { |
|
| 0 | 2107 |
return false; |
| 9 | 2108 |
} |
2109 |
if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $pagenow ][ $plugin_page ] ) ) { |
|
| 0 | 2110 |
return false; |
| 9 | 2111 |
} |
2112 |
if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[ $plugin_page ] ) ) { |
|
| 0 | 2113 |
return false; |
| 9 | 2114 |
} |
| 18 | 2115 |
|
| 9 | 2116 |
foreach ( array_keys( $_wp_submenu_nopriv ) as $key ) { |
2117 |
if ( isset( $_wp_submenu_nopriv[ $key ][ $pagenow ] ) ) { |
|
| 0 | 2118 |
return false; |
| 9 | 2119 |
} |
2120 |
if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $key ][ $plugin_page ] ) ) { |
|
2121 |
return false; |
|
2122 |
} |
|
| 0 | 2123 |
} |
| 18 | 2124 |
|
| 0 | 2125 |
return true; |
2126 |
} |
|
2127 |
||
| 18 | 2128 |
if ( isset( $plugin_page ) && $plugin_page === $parent && isset( $_wp_menu_nopriv[ $plugin_page ] ) ) { |
| 0 | 2129 |
return false; |
| 9 | 2130 |
} |
| 0 | 2131 |
|
| 9 | 2132 |
if ( isset( $submenu[ $parent ] ) ) { |
2133 |
foreach ( $submenu[ $parent ] as $submenu_array ) { |
|
| 18 | 2134 |
if ( isset( $plugin_page ) && $submenu_array[2] === $plugin_page ) { |
2135 |
return current_user_can( $submenu_array[1] ); |
|
2136 |
} elseif ( $submenu_array[2] === $pagenow ) { |
|
2137 |
return current_user_can( $submenu_array[1] ); |
|
| 0 | 2138 |
} |
2139 |
} |
|
2140 |
} |
|
2141 |
||
2142 |
foreach ( $menu as $menu_array ) { |
|
| 18 | 2143 |
if ( $menu_array[2] === $parent ) { |
2144 |
return current_user_can( $menu_array[1] ); |
|
| 0 | 2145 |
} |
2146 |
} |
|
2147 |
||
2148 |
return true; |
|
2149 |
} |
|
2150 |
||
| 16 | 2151 |
/* Allowed list functions */ |
| 0 | 2152 |
|
2153 |
/** |
|
| 16 | 2154 |
* Refreshes the value of the allowed options list available via the 'allowed_options' hook. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2155 |
* |
| 16 | 2156 |
* See the {@see 'allowed_options'} filter. |
| 0 | 2157 |
* |
2158 |
* @since 2.7.0 |
|
| 16 | 2159 |
* @since 5.5.0 `$new_whitelist_options` was renamed to `$new_allowed_options`. |
2160 |
* Please consider writing more inclusive code. |
|
| 0 | 2161 |
* |
| 16 | 2162 |
* @global array $new_allowed_options |
| 0 | 2163 |
* |
| 5 | 2164 |
* @param array $options |
2165 |
* @return array |
|
| 0 | 2166 |
*/ |
2167 |
function option_update_filter( $options ) { |
|
| 16 | 2168 |
global $new_allowed_options; |
| 0 | 2169 |
|
| 16 | 2170 |
if ( is_array( $new_allowed_options ) ) { |
2171 |
$options = add_allowed_options( $new_allowed_options, $options ); |
|
| 9 | 2172 |
} |
| 0 | 2173 |
|
2174 |
return $options; |
|
2175 |
} |
|
2176 |
||
2177 |
/** |
|
| 16 | 2178 |
* Adds an array of options to the list of allowed options. |
| 0 | 2179 |
* |
| 18 | 2180 |
* @since 5.5.0 |
| 0 | 2181 |
* |
| 16 | 2182 |
* @global array $allowed_options |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2183 |
* |
| 5 | 2184 |
* @param array $new_options |
2185 |
* @param string|array $options |
|
2186 |
* @return array |
|
| 0 | 2187 |
*/ |
| 16 | 2188 |
function add_allowed_options( $new_options, $options = '' ) { |
2189 |
if ( '' === $options ) { |
|
2190 |
global $allowed_options; |
|
| 9 | 2191 |
} else { |
| 16 | 2192 |
$allowed_options = $options; |
| 9 | 2193 |
} |
| 0 | 2194 |
|
2195 |
foreach ( $new_options as $page => $keys ) { |
|
2196 |
foreach ( $keys as $key ) { |
|
| 16 | 2197 |
if ( ! isset( $allowed_options[ $page ] ) || ! is_array( $allowed_options[ $page ] ) ) { |
2198 |
$allowed_options[ $page ] = array(); |
|
2199 |
$allowed_options[ $page ][] = $key; |
|
| 0 | 2200 |
} else { |
| 16 | 2201 |
$pos = array_search( $key, $allowed_options[ $page ], true ); |
2202 |
if ( false === $pos ) { |
|
2203 |
$allowed_options[ $page ][] = $key; |
|
| 9 | 2204 |
} |
| 0 | 2205 |
} |
2206 |
} |
|
2207 |
} |
|
2208 |
||
| 16 | 2209 |
return $allowed_options; |
| 0 | 2210 |
} |
2211 |
||
2212 |
/** |
|
| 16 | 2213 |
* Removes a list of options from the allowed options list. |
| 0 | 2214 |
* |
| 16 | 2215 |
* @since 5.5.0 |
| 0 | 2216 |
* |
| 16 | 2217 |
* @global array $allowed_options |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2218 |
* |
| 5 | 2219 |
* @param array $del_options |
2220 |
* @param string|array $options |
|
2221 |
* @return array |
|
| 0 | 2222 |
*/ |
| 16 | 2223 |
function remove_allowed_options( $del_options, $options = '' ) { |
2224 |
if ( '' === $options ) { |
|
2225 |
global $allowed_options; |
|
| 9 | 2226 |
} else { |
| 16 | 2227 |
$allowed_options = $options; |
| 9 | 2228 |
} |
| 0 | 2229 |
|
2230 |
foreach ( $del_options as $page => $keys ) { |
|
2231 |
foreach ( $keys as $key ) { |
|
| 16 | 2232 |
if ( isset( $allowed_options[ $page ] ) && is_array( $allowed_options[ $page ] ) ) { |
2233 |
$pos = array_search( $key, $allowed_options[ $page ], true ); |
|
2234 |
if ( false !== $pos ) { |
|
2235 |
unset( $allowed_options[ $page ][ $pos ] ); |
|
| 9 | 2236 |
} |
| 0 | 2237 |
} |
2238 |
} |
|
2239 |
} |
|
2240 |
||
| 16 | 2241 |
return $allowed_options; |
| 0 | 2242 |
} |
2243 |
||
2244 |
/** |
|
2245 |
* Output nonce, action, and option_page fields for a settings page. |
|
2246 |
* |
|
2247 |
* @since 2.7.0 |
|
2248 |
* |
|
| 16 | 2249 |
* @param string $option_group A settings group name. This should match the group name |
2250 |
* used in register_setting(). |
|
| 0 | 2251 |
*/ |
| 9 | 2252 |
function settings_fields( $option_group ) { |
2253 |
echo "<input type='hidden' name='option_page' value='" . esc_attr( $option_group ) . "' />"; |
|
| 0 | 2254 |
echo '<input type="hidden" name="action" value="update" />'; |
| 9 | 2255 |
wp_nonce_field( "$option_group-options" ); |
| 0 | 2256 |
} |
2257 |
||
2258 |
/** |
|
| 16 | 2259 |
* Clears the plugins cache used by get_plugins() and by default, the plugin updates cache. |
| 0 | 2260 |
* |
2261 |
* @since 3.7.0 |
|
2262 |
* |
|
| 16 | 2263 |
* @param bool $clear_update_cache Whether to clear the plugin updates cache. Default true. |
| 0 | 2264 |
*/ |
2265 |
function wp_clean_plugins_cache( $clear_update_cache = true ) { |
|
| 9 | 2266 |
if ( $clear_update_cache ) { |
| 0 | 2267 |
delete_site_transient( 'update_plugins' ); |
| 9 | 2268 |
} |
| 0 | 2269 |
wp_cache_delete( 'plugins', 'plugins' ); |
2270 |
} |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2271 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2272 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2273 |
* Load a given plugin attempt to generate errors. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2274 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2275 |
* @since 3.0.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2276 |
* @since 4.4.0 Function was moved into the `wp-admin/includes/plugin.php` file. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2277 |
* |
| 9 | 2278 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2279 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2280 |
function plugin_sandbox_scrape( $plugin ) { |
| 9 | 2281 |
if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) { |
2282 |
define( 'WP_SANDBOX_SCRAPING', true ); |
|
2283 |
} |
|
| 16 | 2284 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2285 |
wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin ); |
| 18 | 2286 |
include_once WP_PLUGIN_DIR . '/' . $plugin; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2287 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2288 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2289 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2290 |
* Helper function for adding content to the Privacy Policy Guide. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2291 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2292 |
* Plugins and themes should suggest text for inclusion in the site's privacy policy. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2293 |
* The suggested text should contain information about any functionality that affects user privacy, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2294 |
* and will be shown on the Privacy Policy Guide screen. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2295 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2296 |
* A plugin or theme can use this function multiple times as long as it will help to better present |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2297 |
* the suggested policy content. For example modular plugins such as WooCommerse or Jetpack |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2298 |
* can add or remove suggested content depending on the modules/extensions that are enabled. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2299 |
* For more information see the Plugin Handbook: |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2300 |
* https://developer.wordpress.org/plugins/privacy/suggesting-text-for-the-site-privacy-policy/. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2301 |
* |
| 16 | 2302 |
* The HTML contents of the `$policy_text` supports use of a specialized `.privacy-policy-tutorial` |
2303 |
* CSS class which can be used to provide supplemental information. Any content contained within |
|
2304 |
* HTML elements that have the `.privacy-policy-tutorial` CSS class applied will be omitted |
|
2305 |
* from the clipboard when the section content is copied. |
|
2306 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2307 |
* Intended for use with the `'admin_init'` action. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2308 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2309 |
* @since 4.9.6 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2310 |
* |
| 16 | 2311 |
* @param string $plugin_name The name of the plugin or theme that is suggesting content |
2312 |
* for the site's privacy policy. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2313 |
* @param string $policy_text The suggested content for inclusion in the policy. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2314 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2315 |
function wp_add_privacy_policy_content( $plugin_name, $policy_text ) { |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2316 |
if ( ! is_admin() ) { |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2317 |
_doing_it_wrong( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2318 |
__FUNCTION__, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2319 |
sprintf( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2320 |
/* translators: %s: admin_init */ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2321 |
__( 'The suggested privacy policy content should be added only in wp-admin by using the %s (or later) action.' ), |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2322 |
'<code>admin_init</code>' |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2323 |
), |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2324 |
'4.9.7' |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2325 |
); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2326 |
return; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2327 |
} elseif ( ! doing_action( 'admin_init' ) && ! did_action( 'admin_init' ) ) { |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2328 |
_doing_it_wrong( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2329 |
__FUNCTION__, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2330 |
sprintf( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2331 |
/* translators: %s: admin_init */ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2332 |
__( 'The suggested privacy policy content should be added by using the %s (or later) action. Please see the inline documentation.' ), |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2333 |
'<code>admin_init</code>' |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2334 |
), |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2335 |
'4.9.7' |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2336 |
); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2337 |
return; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2338 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2339 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2340 |
if ( ! class_exists( 'WP_Privacy_Policy_Content' ) ) { |
| 16 | 2341 |
require_once ABSPATH . 'wp-admin/includes/class-wp-privacy-policy-content.php'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2342 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2343 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2344 |
WP_Privacy_Policy_Content::add( $plugin_name, $policy_text ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2345 |
} |
| 9 | 2346 |
|
2347 |
/** |
|
2348 |
* Determines whether a plugin is technically active but was paused while |
|
2349 |
* loading. |
|
2350 |
* |
|
2351 |
* For more information on this and similar theme functions, check out |
|
2352 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
2353 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
2354 |
* |
|
2355 |
* @since 5.2.0 |
|
2356 |
* |
|
2357 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
|
| 16 | 2358 |
* @return bool True, if in the list of paused plugins. False, if not in the list. |
| 9 | 2359 |
*/ |
2360 |
function is_plugin_paused( $plugin ) { |
|
2361 |
if ( ! isset( $GLOBALS['_paused_plugins'] ) ) { |
|
2362 |
return false; |
|
2363 |
} |
|
2364 |
||
2365 |
if ( ! is_plugin_active( $plugin ) ) { |
|
2366 |
return false; |
|
2367 |
} |
|
2368 |
||
2369 |
list( $plugin ) = explode( '/', $plugin ); |
|
2370 |
||
2371 |
return array_key_exists( $plugin, $GLOBALS['_paused_plugins'] ); |
|
2372 |
} |
|
2373 |
||
2374 |
/** |
|
2375 |
* Gets the error that was recorded for a paused plugin. |
|
2376 |
* |
|
2377 |
* @since 5.2.0 |
|
2378 |
* |
|
| 16 | 2379 |
* @param string $plugin Path to the plugin file relative to the plugins directory. |
2380 |
* @return array|false Array of error information as returned by `error_get_last()`, |
|
2381 |
* or false if none was recorded. |
|
| 9 | 2382 |
*/ |
2383 |
function wp_get_plugin_error( $plugin ) { |
|
2384 |
if ( ! isset( $GLOBALS['_paused_plugins'] ) ) { |
|
2385 |
return false; |
|
2386 |
} |
|
2387 |
||
2388 |
list( $plugin ) = explode( '/', $plugin ); |
|
2389 |
||
2390 |
if ( ! array_key_exists( $plugin, $GLOBALS['_paused_plugins'] ) ) { |
|
2391 |
return false; |
|
2392 |
} |
|
2393 |
||
2394 |
return $GLOBALS['_paused_plugins'][ $plugin ]; |
|
2395 |
} |
|
2396 |
||
2397 |
/** |
|
2398 |
* Tries to resume a single plugin. |
|
2399 |
* |
|
2400 |
* If a redirect was provided, we first ensure the plugin does not throw fatal |
|
2401 |
* errors anymore. |
|
2402 |
* |
|
2403 |
* The way it works is by setting the redirection to the error before trying to |
|
2404 |
* include the plugin file. If the plugin fails, then the redirection will not |
|
2405 |
* be overwritten with the success message and the plugin will not be resumed. |
|
2406 |
* |
|
2407 |
* @since 5.2.0 |
|
2408 |
* |
|
| 16 | 2409 |
* @param string $plugin Single plugin to resume. |
2410 |
* @param string $redirect Optional. URL to redirect to. Default empty string. |
|
| 9 | 2411 |
* @return bool|WP_Error True on success, false if `$plugin` was not paused, |
2412 |
* `WP_Error` on failure. |
|
2413 |
*/ |
|
2414 |
function resume_plugin( $plugin, $redirect = '' ) { |
|
2415 |
/* |
|
2416 |
* We'll override this later if the plugin could be resumed without |
|
2417 |
* creating a fatal error. |
|
2418 |
*/ |
|
2419 |
if ( ! empty( $redirect ) ) { |
|
2420 |
wp_redirect( |
|
2421 |
add_query_arg( |
|
2422 |
'_error_nonce', |
|
2423 |
wp_create_nonce( 'plugin-resume-error_' . $plugin ), |
|
2424 |
$redirect |
|
2425 |
) |
|
2426 |
); |
|
2427 |
||
2428 |
// Load the plugin to test whether it throws a fatal error. |
|
2429 |
ob_start(); |
|
2430 |
plugin_sandbox_scrape( $plugin ); |
|
2431 |
ob_clean(); |
|
2432 |
} |
|
2433 |
||
2434 |
list( $extension ) = explode( '/', $plugin ); |
|
2435 |
||
2436 |
$result = wp_paused_plugins()->delete( $extension ); |
|
2437 |
||
2438 |
if ( ! $result ) { |
|
2439 |
return new WP_Error( |
|
2440 |
'could_not_resume_plugin', |
|
2441 |
__( 'Could not resume the plugin.' ) |
|
2442 |
); |
|
2443 |
} |
|
2444 |
||
2445 |
return true; |
|
2446 |
} |
|
2447 |
||
2448 |
/** |
|
2449 |
* Renders an admin notice in case some plugins have been paused due to errors. |
|
2450 |
* |
|
2451 |
* @since 5.2.0 |
|
| 18 | 2452 |
* |
2453 |
* @global string $pagenow |
|
| 9 | 2454 |
*/ |
2455 |
function paused_plugins_notice() { |
|
2456 |
if ( 'plugins.php' === $GLOBALS['pagenow'] ) { |
|
2457 |
return; |
|
2458 |
} |
|
2459 |
||
2460 |
if ( ! current_user_can( 'resume_plugins' ) ) { |
|
2461 |
return; |
|
2462 |
} |
|
2463 |
||
2464 |
if ( ! isset( $GLOBALS['_paused_plugins'] ) || empty( $GLOBALS['_paused_plugins'] ) ) { |
|
2465 |
return; |
|
2466 |
} |
|
2467 |
||
2468 |
printf( |
|
2469 |
'<div class="notice notice-error"><p><strong>%s</strong><br>%s</p><p><a href="%s">%s</a></p></div>', |
|
2470 |
__( 'One or more plugins failed to load properly.' ), |
|
2471 |
__( 'You can find more details and make changes on the Plugins screen.' ), |
|
2472 |
esc_url( admin_url( 'plugins.php?plugin_status=paused' ) ), |
|
2473 |
__( 'Go to the Plugins screen' ) |
|
2474 |
); |
|
2475 |
} |
|
| 18 | 2476 |
|
2477 |
/** |
|
2478 |
* Renders an admin notice when a plugin was deactivated during an update. |
|
2479 |
* |
|
2480 |
* Displays an admin notice in case a plugin has been deactivated during an |
|
2481 |
* upgrade due to incompatibility with the current version of WordPress. |
|
2482 |
* |
|
2483 |
* @since 5.8.0 |
|
2484 |
* @access private |
|
2485 |
* |
|
2486 |
* @global string $pagenow |
|
2487 |
* @global string $wp_version |
|
2488 |
*/ |
|
2489 |
function deactivated_plugins_notice() { |
|
2490 |
if ( 'plugins.php' === $GLOBALS['pagenow'] ) { |
|
2491 |
return; |
|
2492 |
} |
|
2493 |
||
2494 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
|
2495 |
return; |
|
2496 |
} |
|
2497 |
||
2498 |
$blog_deactivated_plugins = get_option( 'wp_force_deactivated_plugins' ); |
|
2499 |
$site_deactivated_plugins = array(); |
|
2500 |
||
2501 |
if ( false === $blog_deactivated_plugins ) { |
|
2502 |
// Option not in database, add an empty array to avoid extra DB queries on subsequent loads. |
|
2503 |
update_option( 'wp_force_deactivated_plugins', array() ); |
|
2504 |
} |
|
2505 |
||
2506 |
if ( is_multisite() ) { |
|
2507 |
$site_deactivated_plugins = get_site_option( 'wp_force_deactivated_plugins' ); |
|
2508 |
if ( false === $site_deactivated_plugins ) { |
|
2509 |
// Option not in database, add an empty array to avoid extra DB queries on subsequent loads. |
|
2510 |
update_site_option( 'wp_force_deactivated_plugins', array() ); |
|
2511 |
} |
|
2512 |
} |
|
2513 |
||
2514 |
if ( empty( $blog_deactivated_plugins ) && empty( $site_deactivated_plugins ) ) { |
|
2515 |
// No deactivated plugins. |
|
2516 |
return; |
|
2517 |
} |
|
2518 |
||
2519 |
$deactivated_plugins = array_merge( $blog_deactivated_plugins, $site_deactivated_plugins ); |
|
2520 |
||
2521 |
foreach ( $deactivated_plugins as $plugin ) { |
|
2522 |
if ( ! empty( $plugin['version_compatible'] ) && ! empty( $plugin['version_deactivated'] ) ) { |
|
2523 |
$explanation = sprintf( |
|
2524 |
/* translators: 1: Name of deactivated plugin, 2: Plugin version deactivated, 3: Current WP version, 4: Compatible plugin version */ |
|
2525 |
__( '%1$s %2$s was deactivated due to incompatibility with WordPress %3$s, please upgrade to %1$s %4$s or later.' ), |
|
2526 |
$plugin['plugin_name'], |
|
2527 |
$plugin['version_deactivated'], |
|
2528 |
$GLOBALS['wp_version'], |
|
2529 |
$plugin['version_compatible'] |
|
2530 |
); |
|
2531 |
} else { |
|
2532 |
$explanation = sprintf( |
|
2533 |
/* translators: 1: Name of deactivated plugin, 2: Plugin version deactivated, 3: Current WP version */ |
|
2534 |
__( '%1$s %2$s was deactivated due to incompatibility with WordPress %3$s.' ), |
|
2535 |
$plugin['plugin_name'], |
|
2536 |
! empty( $plugin['version_deactivated'] ) ? $plugin['version_deactivated'] : '', |
|
2537 |
$GLOBALS['wp_version'], |
|
2538 |
$plugin['version_compatible'] |
|
2539 |
); |
|
2540 |
} |
|
2541 |
||
2542 |
printf( |
|
2543 |
'<div class="notice notice-warning"><p><strong>%s</strong><br>%s</p><p><a href="%s">%s</a></p></div>', |
|
2544 |
sprintf( |
|
2545 |
/* translators: %s: Name of deactivated plugin */ |
|
2546 |
__( '%s plugin deactivated during WordPress upgrade.' ), |
|
2547 |
$plugin['plugin_name'] |
|
2548 |
), |
|
2549 |
$explanation, |
|
2550 |
esc_url( admin_url( 'plugins.php?plugin_status=inactive' ) ), |
|
2551 |
__( 'Go to the Plugins screen' ) |
|
2552 |
); |
|
2553 |
} |
|
2554 |
||
2555 |
// Empty the options. |
|
2556 |
update_option( 'wp_force_deactivated_plugins', array() ); |
|
2557 |
if ( is_multisite() ) { |
|
2558 |
update_site_option( 'wp_force_deactivated_plugins', array() ); |
|
2559 |
} |
|
2560 |
} |