plugin.php 81 KB

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