Keine Beschreibung

plugin.php 86KB

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