plugin.php 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  1. <?php
  2. /**
  3. * WordPress Plugin Administration API
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Parses the plugin contents to retrieve plugin's metadata.
  10. *
  11. * The metadata of the plugin's data searches for the following in the plugin's
  12. * header. All plugin data must be on its own line. For plugin description, it
  13. * must not have any newlines or only parts of the description will be displayed
  14. * and the same goes for the plugin data. The below is formatted for printing.
  15. *
  16. * /*
  17. * Plugin Name: Name of Plugin
  18. * Plugin URI: Link to plugin information
  19. * Description: Plugin Description
  20. * Author: Plugin author's name
  21. * Author URI: Link to the author's web site
  22. * Version: Must be set in the plugin for WordPress 2.3+
  23. * Text Domain: Optional. Unique identifier, should be same as the one used in
  24. * load_plugin_textdomain()
  25. * Domain Path: Optional. Only useful if the translations are located in a
  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.
  30. * Network: Optional. Specify "Network: true" to require that a plugin is activated
  31. * across all sites in an installation. This will prevent a plugin from being
  32. * activated on a single site when Multisite is enabled.
  33. * * / # Remove the space to close comment
  34. *
  35. * Some users have issues with opening large files and manipulating the contents
  36. * for want is usually the first 1kiB or 2kiB. This function stops pulling in
  37. * the plugin contents when it has all of the required plugin data.
  38. *
  39. * The first 8kiB of the file will be pulled in and if the plugin data is not
  40. * within that first 8kiB, then the plugin author should correct their plugin
  41. * and move the plugin data headers to the top.
  42. *
  43. * The plugin file is assumed to have permissions to allow for scripts to read
  44. * the file. This is not checked however and the file is only opened for
  45. * reading.
  46. *
  47. * @since 1.5.0
  48. *
  49. * @param string $plugin_file Path to the main plugin file.
  50. * @param bool $markup Optional. If the returned data should have HTML markup applied.
  51. * Default true.
  52. * @param bool $translate Optional. If the returned data should be translated. Default true.
  53. * @return array {
  54. * Plugin data. Values will be empty if not supplied by the plugin.
  55. *
  56. * @type string $Name Name of the plugin. Should be unique.
  57. * @type string $Title Title of the plugin and link to the plugin's site (if set).
  58. * @type string $Description Plugin description.
  59. * @type string $Author Author's name.
  60. * @type string $AuthorURI Author's website address (if set).
  61. * @type string $Version Plugin version.
  62. * @type string $TextDomain Plugin textdomain.
  63. * @type string $DomainPath Plugins relative directory path to .mo files.
  64. * @type bool $Network Whether the plugin can only be activated network-wide.
  65. * }
  66. */
  67. function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
  68. $default_headers = array(
  69. 'Name' => 'Plugin Name',
  70. 'PluginURI' => 'Plugin URI',
  71. 'Version' => 'Version',
  72. 'Description' => 'Description',
  73. 'Author' => 'Author',
  74. 'AuthorURI' => 'Author URI',
  75. 'TextDomain' => 'Text Domain',
  76. 'DomainPath' => 'Domain Path',
  77. 'Network' => 'Network',
  78. // Site Wide Only is deprecated in favor of Network.
  79. '_sitewide' => 'Site Wide Only',
  80. );
  81. $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
  82. // Site Wide Only is the old header for Network
  83. if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) {
  84. /* translators: 1: Site Wide Only: true, 2: Network: true */
  85. _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>' ) );
  86. $plugin_data['Network'] = $plugin_data['_sitewide'];
  87. }
  88. $plugin_data['Network'] = ( 'true' == strtolower( $plugin_data['Network'] ) );
  89. unset( $plugin_data['_sitewide'] );
  90. // If no text domain is defined fall back to the plugin slug.
  91. if ( ! $plugin_data['TextDomain'] ) {
  92. $plugin_slug = dirname( plugin_basename( $plugin_file ) );
  93. if ( '.' !== $plugin_slug && false === strpos( $plugin_slug, '/' ) ) {
  94. $plugin_data['TextDomain'] = $plugin_slug;
  95. }
  96. }
  97. if ( $markup || $translate ) {
  98. $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
  99. } else {
  100. $plugin_data['Title'] = $plugin_data['Name'];
  101. $plugin_data['AuthorName'] = $plugin_data['Author'];
  102. }
  103. return $plugin_data;
  104. }
  105. /**
  106. * Sanitizes plugin data, optionally adds markup, optionally translates.
  107. *
  108. * @since 2.7.0
  109. * @access private
  110. * @see get_plugin_data()
  111. */
  112. function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) {
  113. // Sanitize the plugin filename to a WP_PLUGIN_DIR relative path
  114. $plugin_file = plugin_basename( $plugin_file );
  115. // Translate fields
  116. if ( $translate ) {
  117. if ( $textdomain = $plugin_data['TextDomain'] ) {
  118. if ( ! is_textdomain_loaded( $textdomain ) ) {
  119. if ( $plugin_data['DomainPath'] ) {
  120. load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] );
  121. } else {
  122. load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) );
  123. }
  124. }
  125. } elseif ( 'hello.php' == basename( $plugin_file ) ) {
  126. $textdomain = 'default';
  127. }
  128. if ( $textdomain ) {
  129. foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field )
  130. $plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain );
  131. }
  132. }
  133. // Sanitize fields
  134. $allowed_tags = $allowed_tags_in_links = array(
  135. 'abbr' => array( 'title' => true ),
  136. 'acronym' => array( 'title' => true ),
  137. 'code' => true,
  138. 'em' => true,
  139. 'strong' => true,
  140. );
  141. $allowed_tags['a'] = array( 'href' => true, 'title' => true );
  142. // Name is marked up inside <a> tags. Don't allow these.
  143. // Author is too, but some plugins have used <a> here (omitting Author URI).
  144. $plugin_data['Name'] = wp_kses( $plugin_data['Name'], $allowed_tags_in_links );
  145. $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags );
  146. $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags );
  147. $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $allowed_tags );
  148. $plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] );
  149. $plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] );
  150. $plugin_data['Title'] = $plugin_data['Name'];
  151. $plugin_data['AuthorName'] = $plugin_data['Author'];
  152. // Apply markup
  153. if ( $markup ) {
  154. if ( $plugin_data['PluginURI'] && $plugin_data['Name'] )
  155. $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '">' . $plugin_data['Name'] . '</a>';
  156. if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] )
  157. $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '">' . $plugin_data['Author'] . '</a>';
  158. $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
  159. if ( $plugin_data['Author'] )
  160. $plugin_data['Description'] .= ' <cite>' . sprintf( __('By %s.'), $plugin_data['Author'] ) . '</cite>';
  161. }
  162. return $plugin_data;
  163. }
  164. /**
  165. * Get a list of a plugin's files.
  166. *
  167. * @since 2.8.0
  168. *
  169. * @param string $plugin Path to the main plugin file from plugins directory.
  170. * @return array List of files relative to the plugin root.
  171. */
  172. function get_plugin_files( $plugin ) {
  173. $plugin_file = WP_PLUGIN_DIR . '/' . $plugin;
  174. $dir = dirname( $plugin_file );
  175. $plugin_files = array( plugin_basename( $plugin_file ) );
  176. if ( is_dir( $dir ) && WP_PLUGIN_DIR !== $dir ) {
  177. /**
  178. * Filters the array of excluded directories and files while scanning the folder.
  179. *
  180. * @since 4.9.0
  181. *
  182. * @param array $exclusions Array of excluded directories and files.
  183. */
  184. $exclusions = (array) apply_filters( 'plugin_files_exclusions', array( 'CVS', 'node_modules', 'vendor', 'bower_components' ) );
  185. $list_files = list_files( $dir, 100, $exclusions );
  186. $list_files = array_map( 'plugin_basename', $list_files );
  187. $plugin_files = array_merge( $plugin_files, $list_files );
  188. $plugin_files = array_values( array_unique( $plugin_files ) );
  189. }
  190. return $plugin_files;
  191. }
  192. /**
  193. * Check the plugins directory and retrieve all plugin files with plugin data.
  194. *
  195. * WordPress only supports plugin files in the base plugins directory
  196. * (wp-content/plugins) and in one directory above the plugins directory
  197. * (wp-content/plugins/my-plugin). The file it looks for has the plugin data
  198. * and must be found in those two locations. It is recommended to keep your
  199. * plugin files in their own directories.
  200. *
  201. * The file with the plugin data is the file that will be included and therefore
  202. * needs to have the main execution for the plugin. This does not mean
  203. * everything must be contained in the file and it is recommended that the file
  204. * be split for maintainability. Keep everything in one file for extreme
  205. * optimization purposes.
  206. *
  207. * @since 1.5.0
  208. *
  209. * @param string $plugin_folder Optional. Relative path to single plugin folder.
  210. * @return array Key is the plugin file path and the value is an array of the plugin data.
  211. */
  212. function get_plugins($plugin_folder = '') {
  213. if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
  214. $cache_plugins = array();
  215. if ( isset($cache_plugins[ $plugin_folder ]) )
  216. return $cache_plugins[ $plugin_folder ];
  217. $wp_plugins = array ();
  218. $plugin_root = WP_PLUGIN_DIR;
  219. if ( !empty($plugin_folder) )
  220. $plugin_root .= $plugin_folder;
  221. // Files in wp-content/plugins directory
  222. $plugins_dir = @ opendir( $plugin_root);
  223. $plugin_files = array();
  224. if ( $plugins_dir ) {
  225. while (($file = readdir( $plugins_dir ) ) !== false ) {
  226. if ( substr($file, 0, 1) == '.' )
  227. continue;
  228. if ( is_dir( $plugin_root.'/'.$file ) ) {
  229. $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
  230. if ( $plugins_subdir ) {
  231. while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
  232. if ( substr($subfile, 0, 1) == '.' )
  233. continue;
  234. if ( substr($subfile, -4) == '.php' )
  235. $plugin_files[] = "$file/$subfile";
  236. }
  237. closedir( $plugins_subdir );
  238. }
  239. } else {
  240. if ( substr($file, -4) == '.php' )
  241. $plugin_files[] = $file;
  242. }
  243. }
  244. closedir( $plugins_dir );
  245. }
  246. if ( empty($plugin_files) )
  247. return $wp_plugins;
  248. foreach ( $plugin_files as $plugin_file ) {
  249. if ( !is_readable( "$plugin_root/$plugin_file" ) )
  250. continue;
  251. $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
  252. if ( empty ( $plugin_data['Name'] ) )
  253. continue;
  254. $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
  255. }
  256. uasort( $wp_plugins, '_sort_uname_callback' );
  257. $cache_plugins[ $plugin_folder ] = $wp_plugins;
  258. wp_cache_set('plugins', $cache_plugins, 'plugins');
  259. return $wp_plugins;
  260. }
  261. /**
  262. * Check the mu-plugins directory and retrieve all mu-plugin files with any plugin data.
  263. *
  264. * WordPress only includes mu-plugin files in the base mu-plugins directory (wp-content/mu-plugins).
  265. *
  266. * @since 3.0.0
  267. * @return array Key is the mu-plugin file path and the value is an array of the mu-plugin data.
  268. */
  269. function get_mu_plugins() {
  270. $wp_plugins = array();
  271. // Files in wp-content/mu-plugins directory
  272. $plugin_files = array();
  273. if ( ! is_dir( WPMU_PLUGIN_DIR ) )
  274. return $wp_plugins;
  275. if ( $plugins_dir = @ opendir( WPMU_PLUGIN_DIR ) ) {
  276. while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
  277. if ( substr( $file, -4 ) == '.php' )
  278. $plugin_files[] = $file;
  279. }
  280. } else {
  281. return $wp_plugins;
  282. }
  283. @closedir( $plugins_dir );
  284. if ( empty($plugin_files) )
  285. return $wp_plugins;
  286. foreach ( $plugin_files as $plugin_file ) {
  287. if ( !is_readable( WPMU_PLUGIN_DIR . "/$plugin_file" ) )
  288. continue;
  289. $plugin_data = get_plugin_data( WPMU_PLUGIN_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
  290. if ( empty ( $plugin_data['Name'] ) )
  291. $plugin_data['Name'] = $plugin_file;
  292. $wp_plugins[ $plugin_file ] = $plugin_data;
  293. }
  294. if ( isset( $wp_plugins['index.php'] ) && filesize( WPMU_PLUGIN_DIR . '/index.php') <= 30 ) // silence is golden
  295. unset( $wp_plugins['index.php'] );
  296. uasort( $wp_plugins, '_sort_uname_callback' );
  297. return $wp_plugins;
  298. }
  299. /**
  300. * Callback to sort array by a 'Name' key.
  301. *
  302. * @since 3.1.0
  303. * @access private
  304. */
  305. function _sort_uname_callback( $a, $b ) {
  306. return strnatcasecmp( $a['Name'], $b['Name'] );
  307. }
  308. /**
  309. * Check the wp-content directory and retrieve all drop-ins with any plugin data.
  310. *
  311. * @since 3.0.0
  312. * @return array Key is the file path and the value is an array of the plugin data.
  313. */
  314. function get_dropins() {
  315. $dropins = array();
  316. $plugin_files = array();
  317. $_dropins = _get_dropins();
  318. // These exist in the wp-content directory
  319. if ( $plugins_dir = @ opendir( WP_CONTENT_DIR ) ) {
  320. while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
  321. if ( isset( $_dropins[ $file ] ) )
  322. $plugin_files[] = $file;
  323. }
  324. } else {
  325. return $dropins;
  326. }
  327. @closedir( $plugins_dir );
  328. if ( empty($plugin_files) )
  329. return $dropins;
  330. foreach ( $plugin_files as $plugin_file ) {
  331. if ( !is_readable( WP_CONTENT_DIR . "/$plugin_file" ) )
  332. continue;
  333. $plugin_data = get_plugin_data( WP_CONTENT_DIR . "/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
  334. if ( empty( $plugin_data['Name'] ) )
  335. $plugin_data['Name'] = $plugin_file;
  336. $dropins[ $plugin_file ] = $plugin_data;
  337. }
  338. uksort( $dropins, 'strnatcasecmp' );
  339. return $dropins;
  340. }
  341. /**
  342. * Returns drop-ins that WordPress uses.
  343. *
  344. * Includes Multisite drop-ins only when is_multisite()
  345. *
  346. * @since 3.0.0
  347. * @return array Key is file name. The value is an array, with the first value the
  348. * purpose of the drop-in and the second value the name of the constant that must be
  349. * true for the drop-in to be used, or true if no constant is required.
  350. */
  351. function _get_dropins() {
  352. $dropins = array(
  353. 'advanced-cache.php' => array( __( 'Advanced caching plugin.' ), 'WP_CACHE' ), // WP_CACHE
  354. 'db.php' => array( __( 'Custom database class.' ), true ), // auto on load
  355. 'db-error.php' => array( __( 'Custom database error message.' ), true ), // auto on error
  356. 'install.php' => array( __( 'Custom installation script.' ), true ), // auto on installation
  357. 'maintenance.php' => array( __( 'Custom maintenance message.' ), true ), // auto on maintenance
  358. 'object-cache.php' => array( __( 'External object cache.' ), true ), // auto on load
  359. );
  360. if ( is_multisite() ) {
  361. $dropins['sunrise.php' ] = array( __( 'Executed before Multisite is loaded.' ), 'SUNRISE' ); // SUNRISE
  362. $dropins['blog-deleted.php' ] = array( __( 'Custom site deleted message.' ), true ); // auto on deleted blog
  363. $dropins['blog-inactive.php' ] = array( __( 'Custom site inactive message.' ), true ); // auto on inactive blog
  364. $dropins['blog-suspended.php'] = array( __( 'Custom site suspended message.' ), true ); // auto on archived or spammed blog
  365. }
  366. return $dropins;
  367. }
  368. /**
  369. * Check whether a plugin is active.
  370. *
  371. * Only plugins installed in the plugins/ folder can be active.
  372. *
  373. * Plugins in the mu-plugins/ folder can't be "activated," so this function will
  374. * return false for those plugins.
  375. *
  376. * @since 2.5.0
  377. *
  378. * @param string $plugin Path to the main plugin file from plugins directory.
  379. * @return bool True, if in the active plugins list. False, not in the list.
  380. */
  381. function is_plugin_active( $plugin ) {
  382. return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
  383. }
  384. /**
  385. * Check whether the plugin is inactive.
  386. *
  387. * Reverse of is_plugin_active(). Used as a callback.
  388. *
  389. * @since 3.1.0
  390. * @see is_plugin_active()
  391. *
  392. * @param string $plugin Path to the main plugin file from plugins directory.
  393. * @return bool True if inactive. False if active.
  394. */
  395. function is_plugin_inactive( $plugin ) {
  396. return ! is_plugin_active( $plugin );
  397. }
  398. /**
  399. * Check whether the plugin is active for the entire network.
  400. *
  401. * Only plugins installed in the plugins/ folder can be active.
  402. *
  403. * Plugins in the mu-plugins/ folder can't be "activated," so this function will
  404. * return false for those plugins.
  405. *
  406. * @since 3.0.0
  407. *
  408. * @param string $plugin Path to the main plugin file from plugins directory.
  409. * @return bool True, if active for the network, otherwise false.
  410. */
  411. function is_plugin_active_for_network( $plugin ) {
  412. if ( !is_multisite() )
  413. return false;
  414. $plugins = get_site_option( 'active_sitewide_plugins');
  415. if ( isset($plugins[$plugin]) )
  416. return true;
  417. return false;
  418. }
  419. /**
  420. * Checks for "Network: true" in the plugin header to see if this should
  421. * be activated only as a network wide plugin. The plugin would also work
  422. * when Multisite is not enabled.
  423. *
  424. * Checks for "Site Wide Only: true" for backward compatibility.
  425. *
  426. * @since 3.0.0
  427. *
  428. * @param string $plugin Path to the main plugin file from plugins directory.
  429. * @return bool True if plugin is network only, false otherwise.
  430. */
  431. function is_network_only_plugin( $plugin ) {
  432. $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
  433. if ( $plugin_data )
  434. return $plugin_data['Network'];
  435. return false;
  436. }
  437. /**
  438. * Attempts activation of plugin in a "sandbox" and redirects on success.
  439. *
  440. * A plugin that is already activated will not attempt to be activated again.
  441. *
  442. * The way it works is by setting the redirection to the error before trying to
  443. * include the plugin file. If the plugin fails, then the redirection will not
  444. * be overwritten with the success message. Also, the options will not be
  445. * updated and the activation hook will not be called on plugin error.
  446. *
  447. * It should be noted that in no way the below code will actually prevent errors
  448. * within the file. The code should not be used elsewhere to replicate the
  449. * "sandbox", which uses redirection to work.
  450. * {@source 13 1}
  451. *
  452. * If any errors are found or text is outputted, then it will be captured to
  453. * ensure that the success redirection will update the error redirection.
  454. *
  455. * @since 2.5.0
  456. *
  457. * @param string $plugin Path to the main plugin file from plugins directory.
  458. * @param string $redirect Optional. URL to redirect to.
  459. * @param bool $network_wide Optional. Whether to enable the plugin for all sites in the network
  460. * or just the current site. Multisite only. Default false.
  461. * @param bool $silent Optional. Whether to prevent calling activation hooks. Default false.
  462. * @return WP_Error|null WP_Error on invalid file or null on success.
  463. */
  464. function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
  465. $plugin = plugin_basename( trim( $plugin ) );
  466. if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) {
  467. $network_wide = true;
  468. $current = get_site_option( 'active_sitewide_plugins', array() );
  469. $_GET['networkwide'] = 1; // Back compat for plugins looking for this value.
  470. } else {
  471. $current = get_option( 'active_plugins', array() );
  472. }
  473. $valid = validate_plugin($plugin);
  474. if ( is_wp_error($valid) )
  475. return $valid;
  476. if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current ) ) ) {
  477. if ( !empty($redirect) )
  478. wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
  479. ob_start();
  480. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
  481. $_wp_plugin_file = $plugin;
  482. include_once( WP_PLUGIN_DIR . '/' . $plugin );
  483. $plugin = $_wp_plugin_file; // Avoid stomping of the $plugin variable in a plugin.
  484. if ( ! $silent ) {
  485. /**
  486. * Fires before a plugin is activated.
  487. *
  488. * If a plugin is silently activated (such as during an update),
  489. * this hook does not fire.
  490. *
  491. * @since 2.9.0
  492. *
  493. * @param string $plugin Path to the main plugin file from plugins directory.
  494. * @param bool $network_wide Whether to enable the plugin for all sites in the network
  495. * or just the current site. Multisite only. Default is false.
  496. */
  497. do_action( 'activate_plugin', $plugin, $network_wide );
  498. /**
  499. * Fires as a specific plugin is being activated.
  500. *
  501. * This hook is the "activation" hook used internally by register_activation_hook().
  502. * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
  503. *
  504. * If a plugin is silently activated (such as during an update), this hook does not fire.
  505. *
  506. * @since 2.0.0
  507. *
  508. * @param bool $network_wide Whether to enable the plugin for all sites in the network
  509. * or just the current site. Multisite only. Default is false.
  510. */
  511. do_action( "activate_{$plugin}", $network_wide );
  512. }
  513. if ( $network_wide ) {
  514. $current = get_site_option( 'active_sitewide_plugins', array() );
  515. $current[$plugin] = time();
  516. update_site_option( 'active_sitewide_plugins', $current );
  517. } else {
  518. $current = get_option( 'active_plugins', array() );
  519. $current[] = $plugin;
  520. sort($current);
  521. update_option('active_plugins', $current);
  522. }
  523. if ( ! $silent ) {
  524. /**
  525. * Fires after a plugin has been activated.
  526. *
  527. * If a plugin is silently activated (such as during an update),
  528. * this hook does not fire.
  529. *
  530. * @since 2.9.0
  531. *
  532. * @param string $plugin Path to the main plugin file from plugins directory.
  533. * @param bool $network_wide Whether to enable the plugin for all sites in the network
  534. * or just the current site. Multisite only. Default is false.
  535. */
  536. do_action( 'activated_plugin', $plugin, $network_wide );
  537. }
  538. if ( ob_get_length() > 0 ) {
  539. $output = ob_get_clean();
  540. return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
  541. }
  542. ob_end_clean();
  543. }
  544. return null;
  545. }
  546. /**
  547. * Deactivate a single plugin or multiple plugins.
  548. *
  549. * The deactivation hook is disabled by the plugin upgrader by using the $silent
  550. * parameter.
  551. *
  552. * @since 2.5.0
  553. *
  554. * @param string|array $plugins Single plugin or list of plugins to deactivate.
  555. * @param bool $silent Prevent calling deactivation hooks. Default is false.
  556. * @param mixed $network_wide Whether to deactivate the plugin for all sites in the network.
  557. * A value of null (the default) will deactivate plugins for both the site and the network.
  558. */
  559. function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) {
  560. if ( is_multisite() )
  561. $network_current = get_site_option( 'active_sitewide_plugins', array() );
  562. $current = get_option( 'active_plugins', array() );
  563. $do_blog = $do_network = false;
  564. foreach ( (array) $plugins as $plugin ) {
  565. $plugin = plugin_basename( trim( $plugin ) );
  566. if ( ! is_plugin_active($plugin) )
  567. continue;
  568. $network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin );
  569. if ( ! $silent ) {
  570. /**
  571. * Fires before a plugin is deactivated.
  572. *
  573. * If a plugin is silently deactivated (such as during an update),
  574. * this hook does not fire.
  575. *
  576. * @since 2.9.0
  577. *
  578. * @param string $plugin Path to the main plugin file from plugins directory.
  579. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
  580. * or just the current site. Multisite only. Default is false.
  581. */
  582. do_action( 'deactivate_plugin', $plugin, $network_deactivating );
  583. }
  584. if ( false !== $network_wide ) {
  585. if ( is_plugin_active_for_network( $plugin ) ) {
  586. $do_network = true;
  587. unset( $network_current[ $plugin ] );
  588. } elseif ( $network_wide ) {
  589. continue;
  590. }
  591. }
  592. if ( true !== $network_wide ) {
  593. $key = array_search( $plugin, $current );
  594. if ( false !== $key ) {
  595. $do_blog = true;
  596. unset( $current[ $key ] );
  597. }
  598. }
  599. if ( ! $silent ) {
  600. /**
  601. * Fires as a specific plugin is being deactivated.
  602. *
  603. * This hook is the "deactivation" hook used internally by register_deactivation_hook().
  604. * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
  605. *
  606. * If a plugin is silently deactivated (such as during an update), this hook does not fire.
  607. *
  608. * @since 2.0.0
  609. *
  610. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
  611. * or just the current site. Multisite only. Default is false.
  612. */
  613. do_action( "deactivate_{$plugin}", $network_deactivating );
  614. /**
  615. * Fires after a plugin is deactivated.
  616. *
  617. * If a plugin is silently deactivated (such as during an update),
  618. * this hook does not fire.
  619. *
  620. * @since 2.9.0
  621. *
  622. * @param string $plugin Path to the main plugin file from plugins directory.
  623. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network.
  624. * or just the current site. Multisite only. Default false.
  625. */
  626. do_action( 'deactivated_plugin', $plugin, $network_deactivating );
  627. }
  628. }
  629. if ( $do_blog )
  630. update_option('active_plugins', $current);
  631. if ( $do_network )
  632. update_site_option( 'active_sitewide_plugins', $network_current );
  633. }
  634. /**
  635. * Activate multiple plugins.
  636. *
  637. * When WP_Error is returned, it does not mean that one of the plugins had
  638. * errors. It means that one or more of the plugins file path was invalid.
  639. *
  640. * The execution will be halted as soon as one of the plugins has an error.
  641. *
  642. * @since 2.6.0
  643. *
  644. * @param string|array $plugins Single plugin or list of plugins to activate.
  645. * @param string $redirect Redirect to page after successful activation.
  646. * @param bool $network_wide Whether to enable the plugin for all sites in the network.
  647. * @param bool $silent Prevent calling activation hooks. Default is false.
  648. * @return bool|WP_Error True when finished or WP_Error if there were errors during a plugin activation.
  649. */
  650. function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
  651. if ( !is_array($plugins) )
  652. $plugins = array($plugins);
  653. $errors = array();
  654. foreach ( $plugins as $plugin ) {
  655. if ( !empty($redirect) )
  656. $redirect = add_query_arg('plugin', $plugin, $redirect);
  657. $result = activate_plugin($plugin, $redirect, $network_wide, $silent);
  658. if ( is_wp_error($result) )
  659. $errors[$plugin] = $result;
  660. }
  661. if ( !empty($errors) )
  662. return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
  663. return true;
  664. }
  665. /**
  666. * Remove directory and files of a plugin for a list of plugins.
  667. *
  668. * @since 2.6.0
  669. *
  670. * @global WP_Filesystem_Base $wp_filesystem
  671. *
  672. * @param array $plugins List of plugins to delete.
  673. * @param string $deprecated Deprecated.
  674. * @return bool|null|WP_Error True on success, false is $plugins is empty, WP_Error on failure.
  675. * Null if filesystem credentials are required to proceed.
  676. */
  677. function delete_plugins( $plugins, $deprecated = '' ) {
  678. global $wp_filesystem;
  679. if ( empty($plugins) )
  680. return false;
  681. $checked = array();
  682. foreach ( $plugins as $plugin )
  683. $checked[] = 'checked[]=' . $plugin;
  684. $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins');
  685. ob_start();
  686. $credentials = request_filesystem_credentials( $url );
  687. $data = ob_get_clean();
  688. if ( false === $credentials ) {
  689. if ( ! empty($data) ){
  690. include_once( ABSPATH . 'wp-admin/admin-header.php');
  691. echo $data;
  692. include( ABSPATH . 'wp-admin/admin-footer.php');
  693. exit;
  694. }
  695. return;
  696. }
  697. if ( ! WP_Filesystem( $credentials ) ) {
  698. ob_start();
  699. request_filesystem_credentials( $url, '', true ); // Failed to connect, Error and request again.
  700. $data = ob_get_clean();
  701. if ( ! empty($data) ){
  702. include_once( ABSPATH . 'wp-admin/admin-header.php');
  703. echo $data;
  704. include( ABSPATH . 'wp-admin/admin-footer.php');
  705. exit;
  706. }
  707. return;
  708. }
  709. if ( ! is_object($wp_filesystem) )
  710. return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
  711. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
  712. return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
  713. // Get the base plugin folder.
  714. $plugins_dir = $wp_filesystem->wp_plugins_dir();
  715. if ( empty( $plugins_dir ) ) {
  716. return new WP_Error( 'fs_no_plugins_dir', __( 'Unable to locate WordPress plugin directory.' ) );
  717. }
  718. $plugins_dir = trailingslashit( $plugins_dir );
  719. $plugin_translations = wp_get_installed_translations( 'plugins' );
  720. $errors = array();
  721. foreach ( $plugins as $plugin_file ) {
  722. // Run Uninstall hook.
  723. if ( is_uninstallable_plugin( $plugin_file ) ) {
  724. uninstall_plugin($plugin_file);
  725. }
  726. /**
  727. * Fires immediately before a plugin deletion attempt.
  728. *
  729. * @since 4.4.0
  730. *
  731. * @param string $plugin_file Plugin file name.
  732. */
  733. do_action( 'delete_plugin', $plugin_file );
  734. $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin_file ) );
  735. // If plugin is in its own directory, recursively delete the directory.
  736. if ( strpos( $plugin_file, '/' ) && $this_plugin_dir != $plugins_dir ) { //base check on if plugin includes directory separator AND that it's not the root plugin folder
  737. $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
  738. } else {
  739. $deleted = $wp_filesystem->delete( $plugins_dir . $plugin_file );
  740. }
  741. /**
  742. * Fires immediately after a plugin deletion attempt.
  743. *
  744. * @since 4.4.0
  745. *
  746. * @param string $plugin_file Plugin file name.
  747. * @param bool $deleted Whether the plugin deletion was successful.
  748. */
  749. do_action( 'deleted_plugin', $plugin_file, $deleted );
  750. if ( ! $deleted ) {
  751. $errors[] = $plugin_file;
  752. continue;
  753. }
  754. // Remove language files, silently.
  755. $plugin_slug = dirname( $plugin_file );
  756. if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) {
  757. $translations = $plugin_translations[ $plugin_slug ];
  758. foreach ( $translations as $translation => $data ) {
  759. $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.po' );
  760. $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.mo' );
  761. }
  762. }
  763. }
  764. // Remove deleted plugins from the plugin updates list.
  765. if ( $current = get_site_transient('update_plugins') ) {
  766. // Don't remove the plugins that weren't deleted.
  767. $deleted = array_diff( $plugins, $errors );
  768. foreach ( $deleted as $plugin_file ) {
  769. unset( $current->response[ $plugin_file ] );
  770. }
  771. set_site_transient( 'update_plugins', $current );
  772. }
  773. if ( ! empty( $errors ) ) {
  774. if ( 1 === count( $errors ) ) {
  775. /* translators: %s: plugin filename */
  776. $message = __( 'Could not fully remove the plugin %s.' );
  777. } else {
  778. /* translators: %s: comma-separated list of plugin filenames */
  779. $message = __( 'Could not fully remove the plugins %s.' );
  780. }
  781. return new WP_Error( 'could_not_remove_plugin', sprintf( $message, implode( ', ', $errors ) ) );
  782. }
  783. return true;
  784. }
  785. /**
  786. * Validate active plugins
  787. *
  788. * Validate all active plugins, deactivates invalid and
  789. * returns an array of deactivated ones.
  790. *
  791. * @since 2.5.0
  792. * @return array invalid plugins, plugin as key, error as value
  793. */
  794. function validate_active_plugins() {
  795. $plugins = get_option( 'active_plugins', array() );
  796. // Validate vartype: array.
  797. if ( ! is_array( $plugins ) ) {
  798. update_option( 'active_plugins', array() );
  799. $plugins = array();
  800. }
  801. if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
  802. $network_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
  803. $plugins = array_merge( $plugins, array_keys( $network_plugins ) );
  804. }
  805. if ( empty( $plugins ) )
  806. return array();
  807. $invalid = array();
  808. // Invalid plugins get deactivated.
  809. foreach ( $plugins as $plugin ) {
  810. $result = validate_plugin( $plugin );
  811. if ( is_wp_error( $result ) ) {
  812. $invalid[$plugin] = $result;
  813. deactivate_plugins( $plugin, true );
  814. }
  815. }
  816. return $invalid;
  817. }
  818. /**
  819. * Validate the plugin path.
  820. *
  821. * Checks that the main plugin file exists and is a valid plugin. See validate_file().
  822. *
  823. * @since 2.5.0
  824. *
  825. * @param string $plugin Path to the main plugin file from plugins directory.
  826. * @return WP_Error|int 0 on success, WP_Error on failure.
  827. */
  828. function validate_plugin($plugin) {
  829. if ( validate_file($plugin) )
  830. return new WP_Error('plugin_invalid', __('Invalid plugin path.'));
  831. if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
  832. return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
  833. $installed_plugins = get_plugins();
  834. if ( ! isset($installed_plugins[$plugin]) )
  835. return new WP_Error('no_plugin_header', __('The plugin does not have a valid header.'));
  836. return 0;
  837. }
  838. /**
  839. * Whether the plugin can be uninstalled.
  840. *
  841. * @since 2.7.0
  842. *
  843. * @param string $plugin Path to the main plugin file from plugins directory.
  844. * @return bool Whether plugin can be uninstalled.
  845. */
  846. function is_uninstallable_plugin($plugin) {
  847. $file = plugin_basename($plugin);
  848. $uninstallable_plugins = (array) get_option('uninstall_plugins');
  849. if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
  850. return true;
  851. return false;
  852. }
  853. /**
  854. * Uninstall a single plugin.
  855. *
  856. * Calls the uninstall hook, if it is available.
  857. *
  858. * @since 2.7.0
  859. *
  860. * @param string $plugin Path to the main plugin file from plugins directory.
  861. * @return true True if a plugin's uninstall.php file has been found and included.
  862. */
  863. function uninstall_plugin($plugin) {
  864. $file = plugin_basename($plugin);
  865. $uninstallable_plugins = (array) get_option('uninstall_plugins');
  866. /**
  867. * Fires in uninstall_plugin() immediately before the plugin is uninstalled.
  868. *
  869. * @since 4.5.0
  870. *
  871. * @param string $plugin Path to the main plugin file from plugins directory.
  872. * @param array $uninstallable_plugins Uninstallable plugins.
  873. */
  874. do_action( 'pre_uninstall_plugin', $plugin, $uninstallable_plugins );
  875. if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
  876. if ( isset( $uninstallable_plugins[$file] ) ) {
  877. unset($uninstallable_plugins[$file]);
  878. update_option('uninstall_plugins', $uninstallable_plugins);
  879. }
  880. unset($uninstallable_plugins);
  881. define('WP_UNINSTALL_PLUGIN', $file);
  882. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
  883. include( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' );
  884. return true;
  885. }
  886. if ( isset( $uninstallable_plugins[$file] ) ) {
  887. $callable = $uninstallable_plugins[$file];
  888. unset($uninstallable_plugins[$file]);
  889. update_option('uninstall_plugins', $uninstallable_plugins);
  890. unset($uninstallable_plugins);
  891. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
  892. include( WP_PLUGIN_DIR . '/' . $file );
  893. add_action( "uninstall_{$file}", $callable );
  894. /**
  895. * Fires in uninstall_plugin() once the plugin has been uninstalled.
  896. *
  897. * The action concatenates the 'uninstall_' prefix with the basename of the
  898. * plugin passed to uninstall_plugin() to create a dynamically-named action.
  899. *
  900. * @since 2.7.0
  901. */
  902. do_action( "uninstall_{$file}" );
  903. }
  904. }
  905. //
  906. // Menu
  907. //
  908. /**
  909. * Add a top-level menu page.
  910. *
  911. * This function takes a capability which will be used to determine whether
  912. * or not a page is included in the menu.
  913. *
  914. * The function which is hooked in to handle the output of the page must check
  915. * that the user has the required capability as well.
  916. *
  917. * @global array $menu
  918. * @global array $admin_page_hooks
  919. * @global array $_registered_pages
  920. * @global array $_parent_pages
  921. *
  922. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  923. * @param string $menu_title The text to be used for the menu.
  924. * @param string $capability The capability required for this menu to be displayed to the user.
  925. * @param string $menu_slug The slug name to refer to this menu by. Should be unique for this menu page and only
  926. * include lowercase alphanumeric, dashes, and underscores characters to be compatible
  927. * with sanitize_key().
  928. * @param callable $function The function to be called to output the content for this page.
  929. * @param string $icon_url The URL to the icon to be used for this menu.
  930. * * Pass a base64-encoded SVG using a data URI, which will be colored to match
  931. * the color scheme. This should begin with 'data:image/svg+xml;base64,'.
  932. * * Pass the name of a Dashicons helper class to use a font icon,
  933. * e.g. 'dashicons-chart-pie'.
  934. * * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
  935. * @param int $position The position in the menu order this one should appear.
  936. * @return string The resulting page's hook_suffix.
  937. */
  938. function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) {
  939. global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages;
  940. $menu_slug = plugin_basename( $menu_slug );
  941. $admin_page_hooks[$menu_slug] = sanitize_title( $menu_title );
  942. $hookname = get_plugin_page_hookname( $menu_slug, '' );
  943. if ( !empty( $function ) && !empty( $hookname ) && current_user_can( $capability ) )
  944. add_action( $hookname, $function );
  945. if ( empty($icon_url) ) {
  946. $icon_url = 'dashicons-admin-generic';
  947. $icon_class = 'menu-icon-generic ';
  948. } else {
  949. $icon_url = set_url_scheme( $icon_url );
  950. $icon_class = '';
  951. }
  952. $new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url );
  953. if ( null === $position ) {
  954. $menu[] = $new_menu;
  955. } elseif ( isset( $menu[ "$position" ] ) ) {
  956. $position = $position + substr( base_convert( md5( $menu_slug . $menu_title ), 16, 10 ) , -5 ) * 0.00001;
  957. $menu[ "$position" ] = $new_menu;
  958. } else {
  959. $menu[ $position ] = $new_menu;
  960. }
  961. $_registered_pages[$hookname] = true;
  962. // No parent as top level
  963. $_parent_pages[$menu_slug] = false;
  964. return $hookname;
  965. }
  966. /**
  967. * Add a submenu page.
  968. *
  969. * This function takes a capability which will be used to determine whether
  970. * or not a page is included in the menu.
  971. *
  972. * The function which is hooked in to handle the output of the page must check
  973. * that the user has the required capability as well.
  974. *
  975. * @global array $submenu
  976. * @global array $menu
  977. * @global array $_wp_real_parent_file
  978. * @global bool $_wp_submenu_nopriv
  979. * @global array $_registered_pages
  980. * @global array $_parent_pages
  981. *
  982. * @param string $parent_slug The slug name for the parent menu (or the file name of a standard
  983. * WordPress admin page).
  984. * @param string $page_title The text to be displayed in the title tags of the page when the menu
  985. * is selected.
  986. * @param string $menu_title The text to be used for the menu.
  987. * @param string $capability The capability required for this menu to be displayed to the user.
  988. * @param string $menu_slug The slug name to refer to this menu by. Should be unique for this menu
  989. * and only include lowercase alphanumeric, dashes, and underscores characters
  990. * to be compatible with sanitize_key().
  991. * @param callable $function The function to be called to output the content for this page.
  992. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  993. */
  994. function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  995. global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv,
  996. $_registered_pages, $_parent_pages;
  997. $menu_slug = plugin_basename( $menu_slug );
  998. $parent_slug = plugin_basename( $parent_slug);
  999. if ( isset( $_wp_real_parent_file[$parent_slug] ) )
  1000. $parent_slug = $_wp_real_parent_file[$parent_slug];
  1001. if ( !current_user_can( $capability ) ) {
  1002. $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true;
  1003. return false;
  1004. }
  1005. /*
  1006. * If the parent doesn't already have a submenu, add a link to the parent
  1007. * as the first item in the submenu. If the submenu file is the same as the
  1008. * parent file someone is trying to link back to the parent manually. In
  1009. * this case, don't automatically add a link back to avoid duplication.
  1010. */
  1011. if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) {
  1012. foreach ( (array)$menu as $parent_menu ) {
  1013. if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) )
  1014. $submenu[$parent_slug][] = array_slice( $parent_menu, 0, 4 );
  1015. }
  1016. }
  1017. $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title );
  1018. $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
  1019. if (!empty ( $function ) && !empty ( $hookname ))
  1020. add_action( $hookname, $function );
  1021. $_registered_pages[$hookname] = true;
  1022. /*
  1023. * Backward-compatibility for plugins using add_management page.
  1024. * See wp-admin/admin.php for redirect from edit.php to tools.php
  1025. */
  1026. if ( 'tools.php' == $parent_slug )
  1027. $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true;
  1028. // No parent as top level.
  1029. $_parent_pages[$menu_slug] = $parent_slug;
  1030. return $hookname;
  1031. }
  1032. /**
  1033. * Add submenu page to the Tools main menu.
  1034. *
  1035. * This function takes a capability which will be used to determine whether
  1036. * or not a page is included in the menu.
  1037. *
  1038. * The function which is hooked in to handle the output of the page must check
  1039. * that the user has the required capability as well.
  1040. *
  1041. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1042. * @param string $menu_title The text to be used for the menu.
  1043. * @param string $capability The capability required for this menu to be displayed to the user.
  1044. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1045. * @param callable $function The function to be called to output the content for this page.
  1046. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1047. */
  1048. function add_management_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1049. return add_submenu_page( 'tools.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1050. }
  1051. /**
  1052. * Add submenu page to the Settings main menu.
  1053. *
  1054. * This function takes a capability which will be used to determine whether
  1055. * or not a page is included in the menu.
  1056. *
  1057. * The function which is hooked in to handle the output of the page must check
  1058. * that the user has the required capability as well.
  1059. *
  1060. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1061. * @param string $menu_title The text to be used for the menu.
  1062. * @param string $capability The capability required for this menu to be displayed to the user.
  1063. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1064. * @param callable $function The function to be called to output the content for this page.
  1065. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1066. */
  1067. function add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1068. return add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1069. }
  1070. /**
  1071. * Add submenu page to the Appearance main menu.
  1072. *
  1073. * This function takes a capability which will be used to determine whether
  1074. * or not a page is included in the menu.
  1075. *
  1076. * The function which is hooked in to handle the output of the page must check
  1077. * that the user has the required capability as well.
  1078. *
  1079. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1080. * @param string $menu_title The text to be used for the menu.
  1081. * @param string $capability The capability required for this menu to be displayed to the user.
  1082. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1083. * @param callable $function The function to be called to output the content for this page.
  1084. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1085. */
  1086. function add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1087. return add_submenu_page( 'themes.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1088. }
  1089. /**
  1090. * Add submenu page to the Plugins main menu.
  1091. *
  1092. * This function takes a capability which will be used to determine whether
  1093. * or not a page is included in the menu.
  1094. *
  1095. * The function which is hooked in to handle the output of the page must check
  1096. * that the user has the required capability as well.
  1097. *
  1098. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1099. * @param string $menu_title The text to be used for the menu.
  1100. * @param string $capability The capability required for this menu to be displayed to the user.
  1101. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1102. * @param callable $function The function to be called to output the content for this page.
  1103. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1104. */
  1105. function add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1106. return add_submenu_page( 'plugins.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1107. }
  1108. /**
  1109. * Add submenu page to the Users/Profile main menu.
  1110. *
  1111. * This function takes a capability which will be used to determine whether
  1112. * or not a page is included in the menu.
  1113. *
  1114. * The function which is hooked in to handle the output of the page must check
  1115. * that the user has the required capability as well.
  1116. *
  1117. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1118. * @param string $menu_title The text to be used for the menu.
  1119. * @param string $capability The capability required for this menu to be displayed to the user.
  1120. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1121. * @param callable $function The function to be called to output the content for this page.
  1122. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1123. */
  1124. function add_users_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1125. if ( current_user_can('edit_users') )
  1126. $parent = 'users.php';
  1127. else
  1128. $parent = 'profile.php';
  1129. return add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function );
  1130. }
  1131. /**
  1132. * Add submenu page to the Dashboard main menu.
  1133. *
  1134. * This function takes a capability which will be used to determine whether
  1135. * or not a page is included in the menu.
  1136. *
  1137. * The function which is hooked in to handle the output of the page must check
  1138. * that the user has the required capability as well.
  1139. *
  1140. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1141. * @param string $menu_title The text to be used for the menu.
  1142. * @param string $capability The capability required for this menu to be displayed to the user.
  1143. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1144. * @param callable $function The function to be called to output the content for this page.
  1145. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1146. */
  1147. function add_dashboard_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1148. return add_submenu_page( 'index.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1149. }
  1150. /**
  1151. * Add submenu page to the Posts main menu.
  1152. *
  1153. * This function takes a capability which will be used to determine whether
  1154. * or not a page is included in the menu.
  1155. *
  1156. * The function which is hooked in to handle the output of the page must check
  1157. * that the user has the required capability as well.
  1158. *
  1159. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1160. * @param string $menu_title The text to be used for the menu.
  1161. * @param string $capability The capability required for this menu to be displayed to the user.
  1162. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1163. * @param callable $function The function to be called to output the content for this page.
  1164. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1165. */
  1166. function add_posts_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1167. return add_submenu_page( 'edit.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1168. }
  1169. /**
  1170. * Add submenu page to the Media main menu.
  1171. *
  1172. * This function takes a capability which will be used to determine whether
  1173. * or not a page is included in the menu.
  1174. *
  1175. * The function which is hooked in to handle the output of the page must check
  1176. * that the user has the required capability as well.
  1177. *
  1178. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1179. * @param string $menu_title The text to be used for the menu.
  1180. * @param string $capability The capability required for this menu to be displayed to the user.
  1181. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1182. * @param callable $function The function to be called to output the content for this page.
  1183. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1184. */
  1185. function add_media_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1186. return add_submenu_page( 'upload.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1187. }
  1188. /**
  1189. * Add submenu page to the Links main menu.
  1190. *
  1191. * This function takes a capability which will be used to determine whether
  1192. * or not a page is included in the menu.
  1193. *
  1194. * The function which is hooked in to handle the output of the page must check
  1195. * that the user has the required capability as well.
  1196. *
  1197. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1198. * @param string $menu_title The text to be used for the menu.
  1199. * @param string $capability The capability required for this menu to be displayed to the user.
  1200. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1201. * @param callable $function The function to be called to output the content for this page.
  1202. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1203. */
  1204. function add_links_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1205. return add_submenu_page( 'link-manager.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1206. }
  1207. /**
  1208. * Add submenu page to the Pages main menu.
  1209. *
  1210. * This function takes a capability which will be used to determine whether
  1211. * or not a page is included in the menu.
  1212. *
  1213. * The function which is hooked in to handle the output of the page must check
  1214. * that the user has the required capability as well.
  1215. *
  1216. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1217. * @param string $menu_title The text to be used for the menu.
  1218. * @param string $capability The capability required for this menu to be displayed to the user.
  1219. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1220. * @param callable $function The function to be called to output the content for this page.
  1221. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1222. */
  1223. function add_pages_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1224. return add_submenu_page( 'edit.php?post_type=page', $page_title, $menu_title, $capability, $menu_slug, $function );
  1225. }
  1226. /**
  1227. * Add submenu page to the Comments main menu.
  1228. *
  1229. * This function takes a capability which will be used to determine whether
  1230. * or not a page is included in the menu.
  1231. *
  1232. * The function which is hooked in to handle the output of the page must check
  1233. * that the user has the required capability as well.
  1234. *
  1235. * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
  1236. * @param string $menu_title The text to be used for the menu.
  1237. * @param string $capability The capability required for this menu to be displayed to the user.
  1238. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
  1239. * @param callable $function The function to be called to output the content for this page.
  1240. * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
  1241. */
  1242. function add_comments_page( $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {
  1243. return add_submenu_page( 'edit-comments.php', $page_title, $menu_title, $capability, $menu_slug, $function );
  1244. }
  1245. /**
  1246. * Remove a top-level admin menu.
  1247. *
  1248. * @since 3.1.0
  1249. *
  1250. * @global array $menu
  1251. *
  1252. * @param string $menu_slug The slug of the menu.
  1253. * @return array|bool The removed menu on success, false if not found.
  1254. */
  1255. function remove_menu_page( $menu_slug ) {
  1256. global $menu;
  1257. foreach ( $menu as $i => $item ) {
  1258. if ( $menu_slug == $item[2] ) {
  1259. unset( $menu[$i] );
  1260. return $item;
  1261. }
  1262. }
  1263. return false;
  1264. }
  1265. /**
  1266. * Remove an admin submenu.
  1267. *
  1268. * @since 3.1.0
  1269. *
  1270. * @global array $submenu
  1271. *
  1272. * @param string $menu_slug The slug for the parent menu.
  1273. * @param string $submenu_slug The slug of the submenu.
  1274. * @return array|bool The removed submenu on success, false if not found.
  1275. */
  1276. function remove_submenu_page( $menu_slug, $submenu_slug ) {
  1277. global $submenu;
  1278. if ( !isset( $submenu[$menu_slug] ) )
  1279. return false;
  1280. foreach ( $submenu[$menu_slug] as $i => $item ) {
  1281. if ( $submenu_slug == $item[2] ) {
  1282. unset( $submenu[$menu_slug][$i] );
  1283. return $item;
  1284. }
  1285. }
  1286. return false;
  1287. }
  1288. /**
  1289. * Get the url to access a particular menu page based on the slug it was registered with.
  1290. *
  1291. * If the slug hasn't been registered properly no url will be returned
  1292. *
  1293. * @since 3.0.0
  1294. *
  1295. * @global array $_parent_pages
  1296. *
  1297. * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
  1298. * @param bool $echo Whether or not to echo the url - default is true
  1299. * @return string the url
  1300. */
  1301. function menu_page_url($menu_slug, $echo = true) {
  1302. global $_parent_pages;
  1303. if ( isset( $_parent_pages[$menu_slug] ) ) {
  1304. $parent_slug = $_parent_pages[$menu_slug];
  1305. if ( $parent_slug && ! isset( $_parent_pages[$parent_slug] ) ) {
  1306. $url = admin_url( add_query_arg( 'page', $menu_slug, $parent_slug ) );
  1307. } else {
  1308. $url = admin_url( 'admin.php?page=' . $menu_slug );
  1309. }
  1310. } else {
  1311. $url = '';
  1312. }
  1313. $url = esc_url($url);
  1314. if ( $echo )
  1315. echo $url;
  1316. return $url;
  1317. }
  1318. //
  1319. // Pluggable Menu Support -- Private
  1320. //
  1321. /**
  1322. *
  1323. * @global string $parent_file
  1324. * @global array $menu
  1325. * @global array $submenu
  1326. * @global string $pagenow
  1327. * @global string $typenow
  1328. * @global string $plugin_page
  1329. * @global array $_wp_real_parent_file
  1330. * @global array $_wp_menu_nopriv
  1331. * @global array $_wp_submenu_nopriv
  1332. */
  1333. function get_admin_page_parent( $parent = '' ) {
  1334. global $parent_file, $menu, $submenu, $pagenow, $typenow,
  1335. $plugin_page, $_wp_real_parent_file, $_wp_menu_nopriv, $_wp_submenu_nopriv;
  1336. if ( !empty ( $parent ) && 'admin.php' != $parent ) {
  1337. if ( isset( $_wp_real_parent_file[$parent] ) )
  1338. $parent = $_wp_real_parent_file[$parent];
  1339. return $parent;
  1340. }
  1341. if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
  1342. foreach ( (array)$menu as $parent_menu ) {
  1343. if ( $parent_menu[2] == $plugin_page ) {
  1344. $parent_file = $plugin_page;
  1345. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  1346. $parent_file = $_wp_real_parent_file[$parent_file];
  1347. return $parent_file;
  1348. }
  1349. }
  1350. if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
  1351. $parent_file = $plugin_page;
  1352. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  1353. $parent_file = $_wp_real_parent_file[$parent_file];
  1354. return $parent_file;
  1355. }
  1356. }
  1357. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
  1358. $parent_file = $pagenow;
  1359. if ( isset( $_wp_real_parent_file[$parent_file] ) )
  1360. $parent_file = $_wp_real_parent_file[$parent_file];
  1361. return $parent_file;
  1362. }
  1363. foreach (array_keys( (array)$submenu ) as $parent) {
  1364. foreach ( $submenu[$parent] as $submenu_array ) {
  1365. if ( isset( $_wp_real_parent_file[$parent] ) )
  1366. $parent = $_wp_real_parent_file[$parent];
  1367. if ( !empty($typenow) && ($submenu_array[2] == "$pagenow?post_type=$typenow") ) {
  1368. $parent_file = $parent;
  1369. return $parent;
  1370. } elseif ( $submenu_array[2] == $pagenow && empty($typenow) && ( empty($parent_file) || false === strpos($parent_file, '?') ) ) {
  1371. $parent_file = $parent;
  1372. return $parent;
  1373. } elseif ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
  1374. $parent_file = $parent;
  1375. return $parent;
  1376. }
  1377. }
  1378. }
  1379. if ( empty($parent_file) )
  1380. $parent_file = '';
  1381. return '';
  1382. }
  1383. /**
  1384. *
  1385. * @global string $title
  1386. * @global array $menu
  1387. * @global array $submenu
  1388. * @global string $pagenow
  1389. * @global string $plugin_page
  1390. * @global string $typenow
  1391. */
  1392. function get_admin_page_title() {
  1393. global $title, $menu, $submenu, $pagenow, $plugin_page, $typenow;
  1394. if ( ! empty ( $title ) )
  1395. return $title;
  1396. $hook = get_plugin_page_hook( $plugin_page, $pagenow );
  1397. $parent = $parent1 = get_admin_page_parent();
  1398. if ( empty ( $parent) ) {
  1399. foreach ( (array)$menu as $menu_array ) {
  1400. if ( isset( $menu_array[3] ) ) {
  1401. if ( $menu_array[2] == $pagenow ) {
  1402. $title = $menu_array[3];
  1403. return $menu_array[3];
  1404. } elseif ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
  1405. $title = $menu_array[3];
  1406. return $menu_array[3];
  1407. }
  1408. } else {
  1409. $title = $menu_array[0];
  1410. return $title;
  1411. }
  1412. }
  1413. } else {
  1414. foreach ( array_keys( $submenu ) as $parent ) {
  1415. foreach ( $submenu[$parent] as $submenu_array ) {
  1416. if ( isset( $plugin_page ) &&
  1417. ( $plugin_page == $submenu_array[2] ) &&
  1418. (
  1419. ( $parent == $pagenow ) ||
  1420. ( $parent == $plugin_page ) ||
  1421. ( $plugin_page == $hook ) ||
  1422. ( $pagenow == 'admin.php' && $parent1 != $submenu_array[2] ) ||
  1423. ( !empty($typenow) && $parent == $pagenow . '?post_type=' . $typenow)
  1424. )
  1425. ) {
  1426. $title = $submenu_array[3];
  1427. return $submenu_array[3];
  1428. }
  1429. if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
  1430. continue;
  1431. if ( isset( $submenu_array[3] ) ) {
  1432. $title = $submenu_array[3];
  1433. return $submenu_array[3];
  1434. } else {
  1435. $title = $submenu_array[0];
  1436. return $title;
  1437. }
  1438. }
  1439. }
  1440. if ( empty ( $title ) ) {
  1441. foreach ( $menu as $menu_array ) {
  1442. if ( isset( $plugin_page ) &&
  1443. ( $plugin_page == $menu_array[2] ) &&
  1444. ( $pagenow == 'admin.php' ) &&
  1445. ( $parent1 == $menu_array[2] ) )
  1446. {
  1447. $title = $menu_array[3];
  1448. return $menu_array[3];
  1449. }
  1450. }
  1451. }
  1452. }
  1453. return $title;
  1454. }
  1455. /**
  1456. * @since 2.3.0
  1457. *
  1458. * @param string $plugin_page
  1459. * @param string $parent_page
  1460. * @return string|null
  1461. */
  1462. function get_plugin_page_hook( $plugin_page, $parent_page ) {
  1463. $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
  1464. if ( has_action($hook) )
  1465. return $hook;
  1466. else
  1467. return null;
  1468. }
  1469. /**
  1470. *
  1471. * @global array $admin_page_hooks
  1472. * @param string $plugin_page
  1473. * @param string $parent_page
  1474. */
  1475. function get_plugin_page_hookname( $plugin_page, $parent_page ) {
  1476. global $admin_page_hooks;
  1477. $parent = get_admin_page_parent( $parent_page );
  1478. $page_type = 'admin';
  1479. if ( empty ( $parent_page ) || 'admin.php' == $parent_page || isset( $admin_page_hooks[$plugin_page] ) ) {
  1480. if ( isset( $admin_page_hooks[$plugin_page] ) ) {
  1481. $page_type = 'toplevel';
  1482. } elseif ( isset( $admin_page_hooks[$parent] )) {
  1483. $page_type = $admin_page_hooks[$parent];
  1484. }
  1485. } elseif ( isset( $admin_page_hooks[$parent] ) ) {
  1486. $page_type = $admin_page_hooks[$parent];
  1487. }
  1488. $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
  1489. return $page_type . '_page_' . $plugin_name;
  1490. }
  1491. /**
  1492. *
  1493. * @global string $pagenow
  1494. * @global array $menu
  1495. * @global array $submenu
  1496. * @global array $_wp_menu_nopriv
  1497. * @global array $_wp_submenu_nopriv
  1498. * @global string $plugin_page
  1499. * @global array $_registered_pages
  1500. */
  1501. function user_can_access_admin_page() {
  1502. global $pagenow, $menu, $submenu, $_wp_menu_nopriv, $_wp_submenu_nopriv,
  1503. $plugin_page, $_registered_pages;
  1504. $parent = get_admin_page_parent();
  1505. if ( !isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
  1506. return false;
  1507. if ( isset( $plugin_page ) ) {
  1508. if ( isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
  1509. return false;
  1510. $hookname = get_plugin_page_hookname($plugin_page, $parent);
  1511. if ( !isset($_registered_pages[$hookname]) )
  1512. return false;
  1513. }
  1514. if ( empty( $parent) ) {
  1515. if ( isset( $_wp_menu_nopriv[$pagenow] ) )
  1516. return false;
  1517. if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
  1518. return false;
  1519. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
  1520. return false;
  1521. if ( isset( $plugin_page ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
  1522. return false;
  1523. foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
  1524. if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
  1525. return false;
  1526. if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
  1527. return false;
  1528. }
  1529. return true;
  1530. }
  1531. if ( isset( $plugin_page ) && ( $plugin_page == $parent ) && isset( $_wp_menu_nopriv[$plugin_page] ) )
  1532. return false;
  1533. if ( isset( $submenu[$parent] ) ) {
  1534. foreach ( $submenu[$parent] as $submenu_array ) {
  1535. if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
  1536. if ( current_user_can( $submenu_array[1] ))
  1537. return true;
  1538. else
  1539. return false;
  1540. } elseif ( $submenu_array[2] == $pagenow ) {
  1541. if ( current_user_can( $submenu_array[1] ))
  1542. return true;
  1543. else
  1544. return false;
  1545. }
  1546. }
  1547. }
  1548. foreach ( $menu as $menu_array ) {
  1549. if ( $menu_array[2] == $parent) {
  1550. if ( current_user_can( $menu_array[1] ))
  1551. return true;
  1552. else
  1553. return false;
  1554. }
  1555. }
  1556. return true;
  1557. }
  1558. /* Whitelist functions */
  1559. /**
  1560. * Refreshes the value of the options whitelist available via the 'whitelist_options' hook.
  1561. *
  1562. * See the {@see 'whitelist_options'} filter.
  1563. *
  1564. * @since 2.7.0
  1565. *
  1566. * @global array $new_whitelist_options
  1567. *
  1568. * @param array $options
  1569. * @return array
  1570. */
  1571. function option_update_filter( $options ) {
  1572. global $new_whitelist_options;
  1573. if ( is_array( $new_whitelist_options ) )
  1574. $options = add_option_whitelist( $new_whitelist_options, $options );
  1575. return $options;
  1576. }
  1577. /**
  1578. * Adds an array of options to the options whitelist.
  1579. *
  1580. * @since 2.7.0
  1581. *
  1582. * @global array $whitelist_options
  1583. *
  1584. * @param array $new_options
  1585. * @param string|array $options
  1586. * @return array
  1587. */
  1588. function add_option_whitelist( $new_options, $options = '' ) {
  1589. if ( $options == '' )
  1590. global $whitelist_options;
  1591. else
  1592. $whitelist_options = $options;
  1593. foreach ( $new_options as $page => $keys ) {
  1594. foreach ( $keys as $key ) {
  1595. if ( !isset($whitelist_options[ $page ]) || !is_array($whitelist_options[ $page ]) ) {
  1596. $whitelist_options[ $page ] = array();
  1597. $whitelist_options[ $page ][] = $key;
  1598. } else {
  1599. $pos = array_search( $key, $whitelist_options[ $page ] );
  1600. if ( $pos === false )
  1601. $whitelist_options[ $page ][] = $key;
  1602. }
  1603. }
  1604. }
  1605. return $whitelist_options;
  1606. }
  1607. /**
  1608. * Removes a list of options from the options whitelist.
  1609. *
  1610. * @since 2.7.0
  1611. *
  1612. * @global array $whitelist_options
  1613. *
  1614. * @param array $del_options
  1615. * @param string|array $options
  1616. * @return array
  1617. */
  1618. function remove_option_whitelist( $del_options, $options = '' ) {
  1619. if ( $options == '' )
  1620. global $whitelist_options;
  1621. else
  1622. $whitelist_options = $options;
  1623. foreach ( $del_options as $page => $keys ) {
  1624. foreach ( $keys as $key ) {
  1625. if ( isset($whitelist_options[ $page ]) && is_array($whitelist_options[ $page ]) ) {
  1626. $pos = array_search( $key, $whitelist_options[ $page ] );
  1627. if ( $pos !== false )
  1628. unset( $whitelist_options[ $page ][ $pos ] );
  1629. }
  1630. }
  1631. }
  1632. return $whitelist_options;
  1633. }
  1634. /**
  1635. * Output nonce, action, and option_page fields for a settings page.
  1636. *
  1637. * @since 2.7.0
  1638. *
  1639. * @param string $option_group A settings group name. This should match the group name used in register_setting().
  1640. */
  1641. function settings_fields($option_group) {
  1642. echo "<input type='hidden' name='option_page' value='" . esc_attr($option_group) . "' />";
  1643. echo '<input type="hidden" name="action" value="update" />';
  1644. wp_nonce_field("$option_group-options");
  1645. }
  1646. /**
  1647. * Clears the Plugins cache used by get_plugins() and by default, the Plugin Update cache.
  1648. *
  1649. * @since 3.7.0
  1650. *
  1651. * @param bool $clear_update_cache Whether to clear the Plugin updates cache
  1652. */
  1653. function wp_clean_plugins_cache( $clear_update_cache = true ) {
  1654. if ( $clear_update_cache )
  1655. delete_site_transient( 'update_plugins' );
  1656. wp_cache_delete( 'plugins', 'plugins' );
  1657. }
  1658. /**
  1659. * Load a given plugin attempt to generate errors.
  1660. *
  1661. * @since 3.0.0
  1662. * @since 4.4.0 Function was moved into the `wp-admin/includes/plugin.php` file.
  1663. *
  1664. * @param string $plugin Plugin file to load.
  1665. */
  1666. function plugin_sandbox_scrape( $plugin ) {
  1667. wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
  1668. include( WP_PLUGIN_DIR . '/' . $plugin );
  1669. }
  1670. /**
  1671. * Helper function for adding content to the Privacy Policy Guide.
  1672. *
  1673. * Plugins and themes should suggest text for inclusion in the site's privacy policy.
  1674. * The suggested text should contain information about any functionality that affects user privacy,
  1675. * and will be shown on the Privacy Policy Guide screen.
  1676. *
  1677. * A plugin or theme can use this function multiple times as long as it will help to better present
  1678. * the suggested policy content. For example modular plugins such as WooCommerse or Jetpack
  1679. * can add or remove suggested content depending on the modules/extensions that are enabled.
  1680. * For more information see the Plugin Handbook:
  1681. * https://developer.wordpress.org/plugins/privacy/suggesting-text-for-the-site-privacy-policy/.
  1682. *
  1683. * Intended for use with the `'admin_init'` action.
  1684. *
  1685. * @since 4.9.6
  1686. *
  1687. * @param string $plugin_name The name of the plugin or theme that is suggesting content for the site's privacy policy.
  1688. * @param string $policy_text The suggested content for inclusion in the policy.
  1689. */
  1690. function wp_add_privacy_policy_content( $plugin_name, $policy_text ) {
  1691. if ( ! is_admin() ) {
  1692. _doing_it_wrong(
  1693. __FUNCTION__,
  1694. sprintf(
  1695. /* translators: %s: admin_init */
  1696. __( 'The suggested privacy policy content should be added only in wp-admin by using the %s (or later) action.' ),
  1697. '<code>admin_init</code>'
  1698. ),
  1699. '4.9.7'
  1700. );
  1701. return;
  1702. } elseif ( ! doing_action( 'admin_init' ) && ! did_action( 'admin_init' ) ) {
  1703. _doing_it_wrong(
  1704. __FUNCTION__,
  1705. sprintf(
  1706. /* translators: %s: admin_init */
  1707. __( 'The suggested privacy policy content should be added by using the %s (or later) action. Please see the inline documentation.' ),
  1708. '<code>admin_init</code>'
  1709. ),
  1710. '4.9.7'
  1711. );
  1712. return;
  1713. }
  1714. if ( ! class_exists( 'WP_Privacy_Policy_Content' ) ) {
  1715. require_once( ABSPATH . 'wp-admin/includes/misc.php' );
  1716. }
  1717. WP_Privacy_Policy_Content::add( $plugin_name, $policy_text );
  1718. }