misc.php 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. <?php
  2. /**
  3. * Misc WordPress Administration API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Returns whether the server is running Apache with the mod_rewrite module loaded.
  10. *
  11. * @since 2.0.0
  12. *
  13. * @return bool Whether the server is running Apache with the mod_rewrite module loaded.
  14. */
  15. function got_mod_rewrite() {
  16. $got_rewrite = apache_mod_loaded( 'mod_rewrite', true );
  17. /**
  18. * Filters whether Apache and mod_rewrite are present.
  19. *
  20. * This filter was previously used to force URL rewriting for other servers,
  21. * like nginx. Use the {@see 'got_url_rewrite'} filter in got_url_rewrite() instead.
  22. *
  23. * @since 2.5.0
  24. *
  25. * @see got_url_rewrite()
  26. *
  27. * @param bool $got_rewrite Whether Apache and mod_rewrite are present.
  28. */
  29. return apply_filters( 'got_rewrite', $got_rewrite );
  30. }
  31. /**
  32. * Returns whether the server supports URL rewriting.
  33. *
  34. * Detects Apache's mod_rewrite, IIS 7.0+ permalink support, and nginx.
  35. *
  36. * @since 3.7.0
  37. *
  38. * @global bool $is_nginx
  39. *
  40. * @return bool Whether the server supports URL rewriting.
  41. */
  42. function got_url_rewrite() {
  43. $got_url_rewrite = ( got_mod_rewrite() || $GLOBALS['is_nginx'] || iis7_supports_permalinks() );
  44. /**
  45. * Filters whether URL rewriting is available.
  46. *
  47. * @since 3.7.0
  48. *
  49. * @param bool $got_url_rewrite Whether URL rewriting is available.
  50. */
  51. return apply_filters( 'got_url_rewrite', $got_url_rewrite );
  52. }
  53. /**
  54. * Extracts strings from between the BEGIN and END markers in the .htaccess file.
  55. *
  56. * @since 1.5.0
  57. *
  58. * @param string $filename Filename to extract the strings from.
  59. * @param string $marker The marker to extract the strings from.
  60. * @return string[] An array of strings from a file (.htaccess) from between BEGIN and END markers.
  61. */
  62. function extract_from_markers( $filename, $marker ) {
  63. $result = array();
  64. if ( ! file_exists( $filename ) ) {
  65. return $result;
  66. }
  67. $markerdata = explode( "\n", implode( '', file( $filename ) ) );
  68. $state = false;
  69. foreach ( $markerdata as $markerline ) {
  70. if ( false !== strpos( $markerline, '# END ' . $marker ) ) {
  71. $state = false;
  72. }
  73. if ( $state ) {
  74. if ( '#' === substr( $markerline, 0, 1 ) ) {
  75. continue;
  76. }
  77. $result[] = $markerline;
  78. }
  79. if ( false !== strpos( $markerline, '# BEGIN ' . $marker ) ) {
  80. $state = true;
  81. }
  82. }
  83. return $result;
  84. }
  85. /**
  86. * Inserts an array of strings into a file (.htaccess), placing it between
  87. * BEGIN and END markers.
  88. *
  89. * Replaces existing marked info. Retains surrounding
  90. * data. Creates file if none exists.
  91. *
  92. * @since 1.5.0
  93. *
  94. * @param string $filename Filename to alter.
  95. * @param string $marker The marker to alter.
  96. * @param array|string $insertion The new content to insert.
  97. * @return bool True on write success, false on failure.
  98. */
  99. function insert_with_markers( $filename, $marker, $insertion ) {
  100. if ( ! file_exists( $filename ) ) {
  101. if ( ! is_writable( dirname( $filename ) ) ) {
  102. return false;
  103. }
  104. if ( ! touch( $filename ) ) {
  105. return false;
  106. }
  107. // Make sure the file is created with a minimum set of permissions.
  108. $perms = fileperms( $filename );
  109. if ( $perms ) {
  110. chmod( $filename, $perms | 0644 );
  111. }
  112. } elseif ( ! is_writable( $filename ) ) {
  113. return false;
  114. }
  115. if ( ! is_array( $insertion ) ) {
  116. $insertion = explode( "\n", $insertion );
  117. }
  118. $switched_locale = switch_to_locale( get_locale() );
  119. $instructions = sprintf(
  120. /* translators: 1: Marker. */
  121. __(
  122. 'The directives (lines) between "BEGIN %1$s" and "END %1$s" are
  123. dynamically generated, and should only be modified via WordPress filters.
  124. Any changes to the directives between these markers will be overwritten.'
  125. ),
  126. $marker
  127. );
  128. $instructions = explode( "\n", $instructions );
  129. foreach ( $instructions as $line => $text ) {
  130. $instructions[ $line ] = '# ' . $text;
  131. }
  132. /**
  133. * Filters the inline instructions inserted before the dynamically generated content.
  134. *
  135. * @since 5.3.0
  136. *
  137. * @param string[] $instructions Array of lines with inline instructions.
  138. * @param string $marker The marker being inserted.
  139. */
  140. $instructions = apply_filters( 'insert_with_markers_inline_instructions', $instructions, $marker );
  141. if ( $switched_locale ) {
  142. restore_previous_locale();
  143. }
  144. $insertion = array_merge( $instructions, $insertion );
  145. $start_marker = "# BEGIN {$marker}";
  146. $end_marker = "# END {$marker}";
  147. $fp = fopen( $filename, 'r+' );
  148. if ( ! $fp ) {
  149. return false;
  150. }
  151. // Attempt to get a lock. If the filesystem supports locking, this will block until the lock is acquired.
  152. flock( $fp, LOCK_EX );
  153. $lines = array();
  154. while ( ! feof( $fp ) ) {
  155. $lines[] = rtrim( fgets( $fp ), "\r\n" );
  156. }
  157. // Split out the existing file into the preceding lines, and those that appear after the marker.
  158. $pre_lines = array();
  159. $post_lines = array();
  160. $existing_lines = array();
  161. $found_marker = false;
  162. $found_end_marker = false;
  163. foreach ( $lines as $line ) {
  164. if ( ! $found_marker && false !== strpos( $line, $start_marker ) ) {
  165. $found_marker = true;
  166. continue;
  167. } elseif ( ! $found_end_marker && false !== strpos( $line, $end_marker ) ) {
  168. $found_end_marker = true;
  169. continue;
  170. }
  171. if ( ! $found_marker ) {
  172. $pre_lines[] = $line;
  173. } elseif ( $found_marker && $found_end_marker ) {
  174. $post_lines[] = $line;
  175. } else {
  176. $existing_lines[] = $line;
  177. }
  178. }
  179. // Check to see if there was a change.
  180. if ( $existing_lines === $insertion ) {
  181. flock( $fp, LOCK_UN );
  182. fclose( $fp );
  183. return true;
  184. }
  185. // Generate the new file data.
  186. $new_file_data = implode(
  187. "\n",
  188. array_merge(
  189. $pre_lines,
  190. array( $start_marker ),
  191. $insertion,
  192. array( $end_marker ),
  193. $post_lines
  194. )
  195. );
  196. // Write to the start of the file, and truncate it to that length.
  197. fseek( $fp, 0 );
  198. $bytes = fwrite( $fp, $new_file_data );
  199. if ( $bytes ) {
  200. ftruncate( $fp, ftell( $fp ) );
  201. }
  202. fflush( $fp );
  203. flock( $fp, LOCK_UN );
  204. fclose( $fp );
  205. return (bool) $bytes;
  206. }
  207. /**
  208. * Updates the htaccess file with the current rules if it is writable.
  209. *
  210. * Always writes to the file if it exists and is writable to ensure that we
  211. * blank out old rules.
  212. *
  213. * @since 1.5.0
  214. *
  215. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  216. *
  217. * @return bool|null True on write success, false on failure. Null in multisite.
  218. */
  219. function save_mod_rewrite_rules() {
  220. global $wp_rewrite;
  221. if ( is_multisite() ) {
  222. return;
  223. }
  224. // Ensure get_home_path() is declared.
  225. require_once ABSPATH . 'wp-admin/includes/file.php';
  226. $home_path = get_home_path();
  227. $htaccess_file = $home_path . '.htaccess';
  228. /*
  229. * If the file doesn't already exist check for write access to the directory
  230. * and whether we have some rules. Else check for write access to the file.
  231. */
  232. if ( ! file_exists( $htaccess_file ) && is_writable( $home_path ) && $wp_rewrite->using_mod_rewrite_permalinks()
  233. || is_writable( $htaccess_file )
  234. ) {
  235. if ( got_mod_rewrite() ) {
  236. $rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
  237. return insert_with_markers( $htaccess_file, 'WordPress', $rules );
  238. }
  239. }
  240. return false;
  241. }
  242. /**
  243. * Updates the IIS web.config file with the current rules if it is writable.
  244. * If the permalinks do not require rewrite rules then the rules are deleted from the web.config file.
  245. *
  246. * @since 2.8.0
  247. *
  248. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  249. *
  250. * @return bool|null True on write success, false on failure. Null in multisite.
  251. */
  252. function iis7_save_url_rewrite_rules() {
  253. global $wp_rewrite;
  254. if ( is_multisite() ) {
  255. return;
  256. }
  257. // Ensure get_home_path() is declared.
  258. require_once ABSPATH . 'wp-admin/includes/file.php';
  259. $home_path = get_home_path();
  260. $web_config_file = $home_path . 'web.config';
  261. // Using win_is_writable() instead of is_writable() because of a bug in Windows PHP.
  262. if ( iis7_supports_permalinks()
  263. && ( ! file_exists( $web_config_file ) && win_is_writable( $home_path ) && $wp_rewrite->using_mod_rewrite_permalinks()
  264. || win_is_writable( $web_config_file ) )
  265. ) {
  266. $rule = $wp_rewrite->iis7_url_rewrite_rules( false );
  267. if ( ! empty( $rule ) ) {
  268. return iis7_add_rewrite_rule( $web_config_file, $rule );
  269. } else {
  270. return iis7_delete_rewrite_rule( $web_config_file );
  271. }
  272. }
  273. return false;
  274. }
  275. /**
  276. * Updates the "recently-edited" file for the plugin or theme file editor.
  277. *
  278. * @since 1.5.0
  279. *
  280. * @param string $file
  281. */
  282. function update_recently_edited( $file ) {
  283. $oldfiles = (array) get_option( 'recently_edited' );
  284. if ( $oldfiles ) {
  285. $oldfiles = array_reverse( $oldfiles );
  286. $oldfiles[] = $file;
  287. $oldfiles = array_reverse( $oldfiles );
  288. $oldfiles = array_unique( $oldfiles );
  289. if ( 5 < count( $oldfiles ) ) {
  290. array_pop( $oldfiles );
  291. }
  292. } else {
  293. $oldfiles[] = $file;
  294. }
  295. update_option( 'recently_edited', $oldfiles );
  296. }
  297. /**
  298. * Makes a tree structure for the theme file editor's file list.
  299. *
  300. * @since 4.9.0
  301. * @access private
  302. *
  303. * @param array $allowed_files List of theme file paths.
  304. * @return array Tree structure for listing theme files.
  305. */
  306. function wp_make_theme_file_tree( $allowed_files ) {
  307. $tree_list = array();
  308. foreach ( $allowed_files as $file_name => $absolute_filename ) {
  309. $list = explode( '/', $file_name );
  310. $last_dir = &$tree_list;
  311. foreach ( $list as $dir ) {
  312. $last_dir =& $last_dir[ $dir ];
  313. }
  314. $last_dir = $file_name;
  315. }
  316. return $tree_list;
  317. }
  318. /**
  319. * Outputs the formatted file list for the theme file editor.
  320. *
  321. * @since 4.9.0
  322. * @access private
  323. *
  324. * @global string $relative_file Name of the file being edited relative to the
  325. * theme directory.
  326. * @global string $stylesheet The stylesheet name of the theme being edited.
  327. *
  328. * @param array|string $tree List of file/folder paths, or filename.
  329. * @param int $level The aria-level for the current iteration.
  330. * @param int $size The aria-setsize for the current iteration.
  331. * @param int $index The aria-posinset for the current iteration.
  332. */
  333. function wp_print_theme_file_tree( $tree, $level = 2, $size = 1, $index = 1 ) {
  334. global $relative_file, $stylesheet;
  335. if ( is_array( $tree ) ) {
  336. $index = 0;
  337. $size = count( $tree );
  338. foreach ( $tree as $label => $theme_file ) :
  339. $index++;
  340. if ( ! is_array( $theme_file ) ) {
  341. wp_print_theme_file_tree( $theme_file, $level, $index, $size );
  342. continue;
  343. }
  344. ?>
  345. <li role="treeitem" aria-expanded="true" tabindex="-1"
  346. aria-level="<?php echo esc_attr( $level ); ?>"
  347. aria-setsize="<?php echo esc_attr( $size ); ?>"
  348. aria-posinset="<?php echo esc_attr( $index ); ?>">
  349. <span class="folder-label"><?php echo esc_html( $label ); ?> <span class="screen-reader-text"><?php _e( 'folder' ); ?></span><span aria-hidden="true" class="icon"></span></span>
  350. <ul role="group" class="tree-folder"><?php wp_print_theme_file_tree( $theme_file, $level + 1, $index, $size ); ?></ul>
  351. </li>
  352. <?php
  353. endforeach;
  354. } else {
  355. $filename = $tree;
  356. $url = add_query_arg(
  357. array(
  358. 'file' => rawurlencode( $tree ),
  359. 'theme' => rawurlencode( $stylesheet ),
  360. ),
  361. self_admin_url( 'theme-editor.php' )
  362. );
  363. ?>
  364. <li role="none" class="<?php echo esc_attr( $relative_file === $filename ? 'current-file' : '' ); ?>">
  365. <a role="treeitem" tabindex="<?php echo esc_attr( $relative_file === $filename ? '0' : '-1' ); ?>"
  366. href="<?php echo esc_url( $url ); ?>"
  367. aria-level="<?php echo esc_attr( $level ); ?>"
  368. aria-setsize="<?php echo esc_attr( $size ); ?>"
  369. aria-posinset="<?php echo esc_attr( $index ); ?>">
  370. <?php
  371. $file_description = esc_html( get_file_description( $filename ) );
  372. if ( $file_description !== $filename && wp_basename( $filename ) !== $file_description ) {
  373. $file_description .= '<br /><span class="nonessential">(' . esc_html( $filename ) . ')</span>';
  374. }
  375. if ( $relative_file === $filename ) {
  376. echo '<span class="notice notice-info">' . $file_description . '</span>';
  377. } else {
  378. echo $file_description;
  379. }
  380. ?>
  381. </a>
  382. </li>
  383. <?php
  384. }
  385. }
  386. /**
  387. * Makes a tree structure for the plugin file editor's file list.
  388. *
  389. * @since 4.9.0
  390. * @access private
  391. *
  392. * @param array $plugin_editable_files List of plugin file paths.
  393. * @return array Tree structure for listing plugin files.
  394. */
  395. function wp_make_plugin_file_tree( $plugin_editable_files ) {
  396. $tree_list = array();
  397. foreach ( $plugin_editable_files as $plugin_file ) {
  398. $list = explode( '/', preg_replace( '#^.+?/#', '', $plugin_file ) );
  399. $last_dir = &$tree_list;
  400. foreach ( $list as $dir ) {
  401. $last_dir =& $last_dir[ $dir ];
  402. }
  403. $last_dir = $plugin_file;
  404. }
  405. return $tree_list;
  406. }
  407. /**
  408. * Outputs the formatted file list for the plugin file editor.
  409. *
  410. * @since 4.9.0
  411. * @access private
  412. *
  413. * @param array|string $tree List of file/folder paths, or filename.
  414. * @param string $label Name of file or folder to print.
  415. * @param int $level The aria-level for the current iteration.
  416. * @param int $size The aria-setsize for the current iteration.
  417. * @param int $index The aria-posinset for the current iteration.
  418. */
  419. function wp_print_plugin_file_tree( $tree, $label = '', $level = 2, $size = 1, $index = 1 ) {
  420. global $file, $plugin;
  421. if ( is_array( $tree ) ) {
  422. $index = 0;
  423. $size = count( $tree );
  424. foreach ( $tree as $label => $plugin_file ) :
  425. $index++;
  426. if ( ! is_array( $plugin_file ) ) {
  427. wp_print_plugin_file_tree( $plugin_file, $label, $level, $index, $size );
  428. continue;
  429. }
  430. ?>
  431. <li role="treeitem" aria-expanded="true" tabindex="-1"
  432. aria-level="<?php echo esc_attr( $level ); ?>"
  433. aria-setsize="<?php echo esc_attr( $size ); ?>"
  434. aria-posinset="<?php echo esc_attr( $index ); ?>">
  435. <span class="folder-label"><?php echo esc_html( $label ); ?> <span class="screen-reader-text"><?php _e( 'folder' ); ?></span><span aria-hidden="true" class="icon"></span></span>
  436. <ul role="group" class="tree-folder"><?php wp_print_plugin_file_tree( $plugin_file, '', $level + 1, $index, $size ); ?></ul>
  437. </li>
  438. <?php
  439. endforeach;
  440. } else {
  441. $url = add_query_arg(
  442. array(
  443. 'file' => rawurlencode( $tree ),
  444. 'plugin' => rawurlencode( $plugin ),
  445. ),
  446. self_admin_url( 'plugin-editor.php' )
  447. );
  448. ?>
  449. <li role="none" class="<?php echo esc_attr( $file === $tree ? 'current-file' : '' ); ?>">
  450. <a role="treeitem" tabindex="<?php echo esc_attr( $file === $tree ? '0' : '-1' ); ?>"
  451. href="<?php echo esc_url( $url ); ?>"
  452. aria-level="<?php echo esc_attr( $level ); ?>"
  453. aria-setsize="<?php echo esc_attr( $size ); ?>"
  454. aria-posinset="<?php echo esc_attr( $index ); ?>">
  455. <?php
  456. if ( $file === $tree ) {
  457. echo '<span class="notice notice-info">' . esc_html( $label ) . '</span>';
  458. } else {
  459. echo esc_html( $label );
  460. }
  461. ?>
  462. </a>
  463. </li>
  464. <?php
  465. }
  466. }
  467. /**
  468. * Flushes rewrite rules if siteurl, home or page_on_front changed.
  469. *
  470. * @since 2.1.0
  471. *
  472. * @param string $old_value
  473. * @param string $value
  474. */
  475. function update_home_siteurl( $old_value, $value ) {
  476. if ( wp_installing() ) {
  477. return;
  478. }
  479. if ( is_multisite() && ms_is_switched() ) {
  480. delete_option( 'rewrite_rules' );
  481. } else {
  482. flush_rewrite_rules();
  483. }
  484. }
  485. /**
  486. * Resets global variables based on $_GET and $_POST.
  487. *
  488. * This function resets global variables based on the names passed
  489. * in the $vars array to the value of $_POST[$var] or $_GET[$var] or ''
  490. * if neither is defined.
  491. *
  492. * @since 2.0.0
  493. *
  494. * @param array $vars An array of globals to reset.
  495. */
  496. function wp_reset_vars( $vars ) {
  497. foreach ( $vars as $var ) {
  498. if ( empty( $_POST[ $var ] ) ) {
  499. if ( empty( $_GET[ $var ] ) ) {
  500. $GLOBALS[ $var ] = '';
  501. } else {
  502. $GLOBALS[ $var ] = $_GET[ $var ];
  503. }
  504. } else {
  505. $GLOBALS[ $var ] = $_POST[ $var ];
  506. }
  507. }
  508. }
  509. /**
  510. * Displays the given administration message.
  511. *
  512. * @since 2.1.0
  513. *
  514. * @param string|WP_Error $message
  515. */
  516. function show_message( $message ) {
  517. if ( is_wp_error( $message ) ) {
  518. if ( $message->get_error_data() && is_string( $message->get_error_data() ) ) {
  519. $message = $message->get_error_message() . ': ' . $message->get_error_data();
  520. } else {
  521. $message = $message->get_error_message();
  522. }
  523. }
  524. echo "<p>$message</p>\n";
  525. wp_ob_end_flush_all();
  526. flush();
  527. }
  528. /**
  529. * @since 2.8.0
  530. *
  531. * @param string $content
  532. * @return array
  533. */
  534. function wp_doc_link_parse( $content ) {
  535. if ( ! is_string( $content ) || empty( $content ) ) {
  536. return array();
  537. }
  538. if ( ! function_exists( 'token_get_all' ) ) {
  539. return array();
  540. }
  541. $tokens = token_get_all( $content );
  542. $count = count( $tokens );
  543. $functions = array();
  544. $ignore_functions = array();
  545. for ( $t = 0; $t < $count - 2; $t++ ) {
  546. if ( ! is_array( $tokens[ $t ] ) ) {
  547. continue;
  548. }
  549. if ( T_STRING === $tokens[ $t ][0] && ( '(' === $tokens[ $t + 1 ] || '(' === $tokens[ $t + 2 ] ) ) {
  550. // If it's a function or class defined locally, there's not going to be any docs available.
  551. if ( ( isset( $tokens[ $t - 2 ][1] ) && in_array( $tokens[ $t - 2 ][1], array( 'function', 'class' ), true ) )
  552. || ( isset( $tokens[ $t - 2 ][0] ) && T_OBJECT_OPERATOR === $tokens[ $t - 1 ][0] )
  553. ) {
  554. $ignore_functions[] = $tokens[ $t ][1];
  555. }
  556. // Add this to our stack of unique references.
  557. $functions[] = $tokens[ $t ][1];
  558. }
  559. }
  560. $functions = array_unique( $functions );
  561. sort( $functions );
  562. /**
  563. * Filters the list of functions and classes to be ignored from the documentation lookup.
  564. *
  565. * @since 2.8.0
  566. *
  567. * @param string[] $ignore_functions Array of names of functions and classes to be ignored.
  568. */
  569. $ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions );
  570. $ignore_functions = array_unique( $ignore_functions );
  571. $output = array();
  572. foreach ( $functions as $function ) {
  573. if ( in_array( $function, $ignore_functions, true ) ) {
  574. continue;
  575. }
  576. $output[] = $function;
  577. }
  578. return $output;
  579. }
  580. /**
  581. * Saves option for number of rows when listing posts, pages, comments, etc.
  582. *
  583. * @since 2.8.0
  584. */
  585. function set_screen_options() {
  586. if ( ! isset( $_POST['wp_screen_options'] ) || ! is_array( $_POST['wp_screen_options'] ) ) {
  587. return;
  588. }
  589. check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
  590. $user = wp_get_current_user();
  591. if ( ! $user ) {
  592. return;
  593. }
  594. $option = $_POST['wp_screen_options']['option'];
  595. $value = $_POST['wp_screen_options']['value'];
  596. if ( sanitize_key( $option ) !== $option ) {
  597. return;
  598. }
  599. $map_option = $option;
  600. $type = str_replace( 'edit_', '', $map_option );
  601. $type = str_replace( '_per_page', '', $type );
  602. if ( in_array( $type, get_taxonomies(), true ) ) {
  603. $map_option = 'edit_tags_per_page';
  604. } elseif ( in_array( $type, get_post_types(), true ) ) {
  605. $map_option = 'edit_per_page';
  606. } else {
  607. $option = str_replace( '-', '_', $option );
  608. }
  609. switch ( $map_option ) {
  610. case 'edit_per_page':
  611. case 'users_per_page':
  612. case 'edit_comments_per_page':
  613. case 'upload_per_page':
  614. case 'edit_tags_per_page':
  615. case 'plugins_per_page':
  616. case 'export_personal_data_requests_per_page':
  617. case 'remove_personal_data_requests_per_page':
  618. // Network admin.
  619. case 'sites_network_per_page':
  620. case 'users_network_per_page':
  621. case 'site_users_network_per_page':
  622. case 'plugins_network_per_page':
  623. case 'themes_network_per_page':
  624. case 'site_themes_network_per_page':
  625. $value = (int) $value;
  626. if ( $value < 1 || $value > 999 ) {
  627. return;
  628. }
  629. break;
  630. default:
  631. $screen_option = false;
  632. if ( '_page' === substr( $option, -5 ) || 'layout_columns' === $option ) {
  633. /**
  634. * Filters a screen option value before it is set.
  635. *
  636. * The filter can also be used to modify non-standard [items]_per_page
  637. * settings. See the parent function for a full list of standard options.
  638. *
  639. * Returning false from the filter will skip saving the current option.
  640. *
  641. * @since 2.8.0
  642. * @since 5.4.2 Only applied to options ending with '_page',
  643. * or the 'layout_columns' option.
  644. *
  645. * @see set_screen_options()
  646. *
  647. * @param mixed $screen_option The value to save instead of the option value.
  648. * Default false (to skip saving the current option).
  649. * @param string $option The option name.
  650. * @param int $value The option value.
  651. */
  652. $screen_option = apply_filters( 'set-screen-option', $screen_option, $option, $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  653. }
  654. /**
  655. * Filters a screen option value before it is set.
  656. *
  657. * The dynamic portion of the hook name, `$option`, refers to the option name.
  658. *
  659. * Returning false from the filter will skip saving the current option.
  660. *
  661. * @since 5.4.2
  662. *
  663. * @see set_screen_options()
  664. *
  665. * @param mixed $screen_option The value to save instead of the option value.
  666. * Default false (to skip saving the current option).
  667. * @param string $option The option name.
  668. * @param int $value The option value.
  669. */
  670. $value = apply_filters( "set_screen_option_{$option}", $screen_option, $option, $value );
  671. if ( false === $value ) {
  672. return;
  673. }
  674. break;
  675. }
  676. update_user_meta( $user->ID, $option, $value );
  677. $url = remove_query_arg( array( 'pagenum', 'apage', 'paged' ), wp_get_referer() );
  678. if ( isset( $_POST['mode'] ) ) {
  679. $url = add_query_arg( array( 'mode' => $_POST['mode'] ), $url );
  680. }
  681. wp_safe_redirect( $url );
  682. exit;
  683. }
  684. /**
  685. * Checks if rewrite rule for WordPress already exists in the IIS 7+ configuration file.
  686. *
  687. * @since 2.8.0
  688. *
  689. * @param string $filename The file path to the configuration file.
  690. * @return bool
  691. */
  692. function iis7_rewrite_rule_exists( $filename ) {
  693. if ( ! file_exists( $filename ) ) {
  694. return false;
  695. }
  696. if ( ! class_exists( 'DOMDocument', false ) ) {
  697. return false;
  698. }
  699. $doc = new DOMDocument();
  700. if ( $doc->load( $filename ) === false ) {
  701. return false;
  702. }
  703. $xpath = new DOMXPath( $doc );
  704. $rules = $xpath->query( '/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]' );
  705. if ( 0 === $rules->length ) {
  706. return false;
  707. }
  708. return true;
  709. }
  710. /**
  711. * Deletes WordPress rewrite rule from web.config file if it exists there.
  712. *
  713. * @since 2.8.0
  714. *
  715. * @param string $filename Name of the configuration file.
  716. * @return bool
  717. */
  718. function iis7_delete_rewrite_rule( $filename ) {
  719. // If configuration file does not exist then rules also do not exist, so there is nothing to delete.
  720. if ( ! file_exists( $filename ) ) {
  721. return true;
  722. }
  723. if ( ! class_exists( 'DOMDocument', false ) ) {
  724. return false;
  725. }
  726. $doc = new DOMDocument();
  727. $doc->preserveWhiteSpace = false;
  728. if ( $doc->load( $filename ) === false ) {
  729. return false;
  730. }
  731. $xpath = new DOMXPath( $doc );
  732. $rules = $xpath->query( '/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]' );
  733. if ( $rules->length > 0 ) {
  734. $child = $rules->item( 0 );
  735. $parent = $child->parentNode;
  736. $parent->removeChild( $child );
  737. $doc->formatOutput = true;
  738. saveDomDocument( $doc, $filename );
  739. }
  740. return true;
  741. }
  742. /**
  743. * Adds WordPress rewrite rule to the IIS 7+ configuration file.
  744. *
  745. * @since 2.8.0
  746. *
  747. * @param string $filename The file path to the configuration file.
  748. * @param string $rewrite_rule The XML fragment with URL Rewrite rule.
  749. * @return bool
  750. */
  751. function iis7_add_rewrite_rule( $filename, $rewrite_rule ) {
  752. if ( ! class_exists( 'DOMDocument', false ) ) {
  753. return false;
  754. }
  755. // If configuration file does not exist then we create one.
  756. if ( ! file_exists( $filename ) ) {
  757. $fp = fopen( $filename, 'w' );
  758. fwrite( $fp, '<configuration/>' );
  759. fclose( $fp );
  760. }
  761. $doc = new DOMDocument();
  762. $doc->preserveWhiteSpace = false;
  763. if ( $doc->load( $filename ) === false ) {
  764. return false;
  765. }
  766. $xpath = new DOMXPath( $doc );
  767. // First check if the rule already exists as in that case there is no need to re-add it.
  768. $wordpress_rules = $xpath->query( '/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]' );
  769. if ( $wordpress_rules->length > 0 ) {
  770. return true;
  771. }
  772. // Check the XPath to the rewrite rule and create XML nodes if they do not exist.
  773. $xml_nodes = $xpath->query( '/configuration/system.webServer/rewrite/rules' );
  774. if ( $xml_nodes->length > 0 ) {
  775. $rules_node = $xml_nodes->item( 0 );
  776. } else {
  777. $rules_node = $doc->createElement( 'rules' );
  778. $xml_nodes = $xpath->query( '/configuration/system.webServer/rewrite' );
  779. if ( $xml_nodes->length > 0 ) {
  780. $rewrite_node = $xml_nodes->item( 0 );
  781. $rewrite_node->appendChild( $rules_node );
  782. } else {
  783. $rewrite_node = $doc->createElement( 'rewrite' );
  784. $rewrite_node->appendChild( $rules_node );
  785. $xml_nodes = $xpath->query( '/configuration/system.webServer' );
  786. if ( $xml_nodes->length > 0 ) {
  787. $system_web_server_node = $xml_nodes->item( 0 );
  788. $system_web_server_node->appendChild( $rewrite_node );
  789. } else {
  790. $system_web_server_node = $doc->createElement( 'system.webServer' );
  791. $system_web_server_node->appendChild( $rewrite_node );
  792. $xml_nodes = $xpath->query( '/configuration' );
  793. if ( $xml_nodes->length > 0 ) {
  794. $config_node = $xml_nodes->item( 0 );
  795. $config_node->appendChild( $system_web_server_node );
  796. } else {
  797. $config_node = $doc->createElement( 'configuration' );
  798. $doc->appendChild( $config_node );
  799. $config_node->appendChild( $system_web_server_node );
  800. }
  801. }
  802. }
  803. }
  804. $rule_fragment = $doc->createDocumentFragment();
  805. $rule_fragment->appendXML( $rewrite_rule );
  806. $rules_node->appendChild( $rule_fragment );
  807. $doc->encoding = 'UTF-8';
  808. $doc->formatOutput = true;
  809. saveDomDocument( $doc, $filename );
  810. return true;
  811. }
  812. /**
  813. * Saves the XML document into a file.
  814. *
  815. * @since 2.8.0
  816. *
  817. * @param DOMDocument $doc
  818. * @param string $filename
  819. */
  820. function saveDomDocument( $doc, $filename ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  821. $config = $doc->saveXML();
  822. $config = preg_replace( "/([^\r])\n/", "$1\r\n", $config );
  823. $fp = fopen( $filename, 'w' );
  824. fwrite( $fp, $config );
  825. fclose( $fp );
  826. }
  827. /**
  828. * Displays the default admin color scheme picker (Used in user-edit.php).
  829. *
  830. * @since 3.0.0
  831. *
  832. * @global array $_wp_admin_css_colors
  833. *
  834. * @param int $user_id User ID.
  835. */
  836. function admin_color_scheme_picker( $user_id ) {
  837. global $_wp_admin_css_colors;
  838. ksort( $_wp_admin_css_colors );
  839. if ( isset( $_wp_admin_css_colors['fresh'] ) ) {
  840. // Set Default ('fresh') and Light should go first.
  841. $_wp_admin_css_colors = array_filter(
  842. array_merge(
  843. array(
  844. 'fresh' => '',
  845. 'light' => '',
  846. 'modern' => '',
  847. ),
  848. $_wp_admin_css_colors
  849. )
  850. );
  851. }
  852. $current_color = get_user_option( 'admin_color', $user_id );
  853. if ( empty( $current_color ) || ! isset( $_wp_admin_css_colors[ $current_color ] ) ) {
  854. $current_color = 'fresh';
  855. }
  856. ?>
  857. <fieldset id="color-picker" class="scheme-list">
  858. <legend class="screen-reader-text"><span><?php _e( 'Admin Color Scheme' ); ?></span></legend>
  859. <?php
  860. wp_nonce_field( 'save-color-scheme', 'color-nonce', false );
  861. foreach ( $_wp_admin_css_colors as $color => $color_info ) :
  862. ?>
  863. <div class="color-option <?php echo ( $color === $current_color ) ? 'selected' : ''; ?>">
  864. <input name="admin_color" id="admin_color_<?php echo esc_attr( $color ); ?>" type="radio" value="<?php echo esc_attr( $color ); ?>" class="tog" <?php checked( $color, $current_color ); ?> />
  865. <input type="hidden" class="css_url" value="<?php echo esc_url( $color_info->url ); ?>" />
  866. <input type="hidden" class="icon_colors" value="<?php echo esc_attr( wp_json_encode( array( 'icons' => $color_info->icon_colors ) ) ); ?>" />
  867. <label for="admin_color_<?php echo esc_attr( $color ); ?>"><?php echo esc_html( $color_info->name ); ?></label>
  868. <table class="color-palette">
  869. <tr>
  870. <?php
  871. foreach ( $color_info->colors as $html_color ) {
  872. ?>
  873. <td style="background-color: <?php echo esc_attr( $html_color ); ?>">&nbsp;</td>
  874. <?php
  875. }
  876. ?>
  877. </tr>
  878. </table>
  879. </div>
  880. <?php
  881. endforeach;
  882. ?>
  883. </fieldset>
  884. <?php
  885. }
  886. /**
  887. *
  888. * @global array $_wp_admin_css_colors
  889. */
  890. function wp_color_scheme_settings() {
  891. global $_wp_admin_css_colors;
  892. $color_scheme = get_user_option( 'admin_color' );
  893. // It's possible to have a color scheme set that is no longer registered.
  894. if ( empty( $_wp_admin_css_colors[ $color_scheme ] ) ) {
  895. $color_scheme = 'fresh';
  896. }
  897. if ( ! empty( $_wp_admin_css_colors[ $color_scheme ]->icon_colors ) ) {
  898. $icon_colors = $_wp_admin_css_colors[ $color_scheme ]->icon_colors;
  899. } elseif ( ! empty( $_wp_admin_css_colors['fresh']->icon_colors ) ) {
  900. $icon_colors = $_wp_admin_css_colors['fresh']->icon_colors;
  901. } else {
  902. // Fall back to the default set of icon colors if the default scheme is missing.
  903. $icon_colors = array(
  904. 'base' => '#a7aaad',
  905. 'focus' => '#72aee6',
  906. 'current' => '#fff',
  907. );
  908. }
  909. echo '<script type="text/javascript">var _wpColorScheme = ' . wp_json_encode( array( 'icons' => $icon_colors ) ) . ";</script>\n";
  910. }
  911. /**
  912. * Displays the viewport meta in the admin.
  913. *
  914. * @since 5.5.0
  915. */
  916. function wp_admin_viewport_meta() {
  917. /**
  918. * Filters the viewport meta in the admin.
  919. *
  920. * @since 5.5.0
  921. *
  922. * @param string $viewport_meta The viewport meta.
  923. */
  924. $viewport_meta = apply_filters( 'admin_viewport_meta', 'width=device-width,initial-scale=1.0' );
  925. if ( empty( $viewport_meta ) ) {
  926. return;
  927. }
  928. echo '<meta name="viewport" content="' . esc_attr( $viewport_meta ) . '">';
  929. }
  930. /**
  931. * Adds viewport meta for mobile in Customizer.
  932. *
  933. * Hooked to the {@see 'admin_viewport_meta'} filter.
  934. *
  935. * @since 5.5.0
  936. *
  937. * @param string $viewport_meta The viewport meta.
  938. * @return string Filtered viewport meta.
  939. */
  940. function _customizer_mobile_viewport_meta( $viewport_meta ) {
  941. return trim( $viewport_meta, ',' ) . ',minimum-scale=0.5,maximum-scale=1.2';
  942. }
  943. /**
  944. * Checks lock status for posts displayed on the Posts screen.
  945. *
  946. * @since 3.6.0
  947. *
  948. * @param array $response The Heartbeat response.
  949. * @param array $data The $_POST data sent.
  950. * @param string $screen_id The screen ID.
  951. * @return array The Heartbeat response.
  952. */
  953. function wp_check_locked_posts( $response, $data, $screen_id ) {
  954. $checked = array();
  955. if ( array_key_exists( 'wp-check-locked-posts', $data ) && is_array( $data['wp-check-locked-posts'] ) ) {
  956. foreach ( $data['wp-check-locked-posts'] as $key ) {
  957. $post_id = absint( substr( $key, 5 ) );
  958. if ( ! $post_id ) {
  959. continue;
  960. }
  961. $user_id = wp_check_post_lock( $post_id );
  962. if ( $user_id ) {
  963. $user = get_userdata( $user_id );
  964. if ( $user && current_user_can( 'edit_post', $post_id ) ) {
  965. $send = array(
  966. 'name' => $user->display_name,
  967. /* translators: %s: User's display name. */
  968. 'text' => sprintf( __( '%s is currently editing' ), $user->display_name ),
  969. );
  970. if ( get_option( 'show_avatars' ) ) {
  971. $send['avatar_src'] = get_avatar_url( $user->ID, array( 'size' => 18 ) );
  972. $send['avatar_src_2x'] = get_avatar_url( $user->ID, array( 'size' => 36 ) );
  973. }
  974. $checked[ $key ] = $send;
  975. }
  976. }
  977. }
  978. }
  979. if ( ! empty( $checked ) ) {
  980. $response['wp-check-locked-posts'] = $checked;
  981. }
  982. return $response;
  983. }
  984. /**
  985. * Checks lock status on the New/Edit Post screen and refresh the lock.
  986. *
  987. * @since 3.6.0
  988. *
  989. * @param array $response The Heartbeat response.
  990. * @param array $data The $_POST data sent.
  991. * @param string $screen_id The screen ID.
  992. * @return array The Heartbeat response.
  993. */
  994. function wp_refresh_post_lock( $response, $data, $screen_id ) {
  995. if ( array_key_exists( 'wp-refresh-post-lock', $data ) ) {
  996. $received = $data['wp-refresh-post-lock'];
  997. $send = array();
  998. $post_id = absint( $received['post_id'] );
  999. if ( ! $post_id ) {
  1000. return $response;
  1001. }
  1002. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  1003. return $response;
  1004. }
  1005. $user_id = wp_check_post_lock( $post_id );
  1006. $user = get_userdata( $user_id );
  1007. if ( $user ) {
  1008. $error = array(
  1009. 'name' => $user->display_name,
  1010. /* translators: %s: User's display name. */
  1011. 'text' => sprintf( __( '%s has taken over and is currently editing.' ), $user->display_name ),
  1012. );
  1013. if ( get_option( 'show_avatars' ) ) {
  1014. $error['avatar_src'] = get_avatar_url( $user->ID, array( 'size' => 64 ) );
  1015. $error['avatar_src_2x'] = get_avatar_url( $user->ID, array( 'size' => 128 ) );
  1016. }
  1017. $send['lock_error'] = $error;
  1018. } else {
  1019. $new_lock = wp_set_post_lock( $post_id );
  1020. if ( $new_lock ) {
  1021. $send['new_lock'] = implode( ':', $new_lock );
  1022. }
  1023. }
  1024. $response['wp-refresh-post-lock'] = $send;
  1025. }
  1026. return $response;
  1027. }
  1028. /**
  1029. * Checks nonce expiration on the New/Edit Post screen and refresh if needed.
  1030. *
  1031. * @since 3.6.0
  1032. *
  1033. * @param array $response The Heartbeat response.
  1034. * @param array $data The $_POST data sent.
  1035. * @param string $screen_id The screen ID.
  1036. * @return array The Heartbeat response.
  1037. */
  1038. function wp_refresh_post_nonces( $response, $data, $screen_id ) {
  1039. if ( array_key_exists( 'wp-refresh-post-nonces', $data ) ) {
  1040. $received = $data['wp-refresh-post-nonces'];
  1041. $response['wp-refresh-post-nonces'] = array( 'check' => 1 );
  1042. $post_id = absint( $received['post_id'] );
  1043. if ( ! $post_id ) {
  1044. return $response;
  1045. }
  1046. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  1047. return $response;
  1048. }
  1049. $response['wp-refresh-post-nonces'] = array(
  1050. 'replace' => array(
  1051. 'getpermalinknonce' => wp_create_nonce( 'getpermalink' ),
  1052. 'samplepermalinknonce' => wp_create_nonce( 'samplepermalink' ),
  1053. 'closedpostboxesnonce' => wp_create_nonce( 'closedpostboxes' ),
  1054. '_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ),
  1055. '_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
  1056. ),
  1057. );
  1058. }
  1059. return $response;
  1060. }
  1061. /**
  1062. * Refresh nonces used with meta boxes in the block editor.
  1063. *
  1064. * @since 6.1.0
  1065. *
  1066. * @param array $response The Heartbeat response.
  1067. * @param array $data The $_POST data sent.
  1068. * @return array The Heartbeat response.
  1069. */
  1070. function wp_refresh_metabox_loader_nonces( $response, $data ) {
  1071. if ( empty( $data['wp-refresh-metabox-loader-nonces'] ) ) {
  1072. return $response;
  1073. }
  1074. $received = $data['wp-refresh-metabox-loader-nonces'];
  1075. $post_id = (int) $received['post_id'];
  1076. if ( ! $post_id ) {
  1077. return $response;
  1078. }
  1079. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  1080. return $response;
  1081. }
  1082. $response['wp-refresh-metabox-loader-nonces'] = array(
  1083. 'replace' => array(
  1084. 'metabox_loader_nonce' => wp_create_nonce( 'meta-box-loader' ),
  1085. '_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
  1086. ),
  1087. );
  1088. return $response;
  1089. }
  1090. /**
  1091. * Adds the latest Heartbeat and REST-API nonce to the Heartbeat response.
  1092. *
  1093. * @since 5.0.0
  1094. *
  1095. * @param array $response The Heartbeat response.
  1096. * @return array The Heartbeat response.
  1097. */
  1098. function wp_refresh_heartbeat_nonces( $response ) {
  1099. // Refresh the Rest API nonce.
  1100. $response['rest_nonce'] = wp_create_nonce( 'wp_rest' );
  1101. // Refresh the Heartbeat nonce.
  1102. $response['heartbeat_nonce'] = wp_create_nonce( 'heartbeat-nonce' );
  1103. return $response;
  1104. }
  1105. /**
  1106. * Disables suspension of Heartbeat on the Add/Edit Post screens.
  1107. *
  1108. * @since 3.8.0
  1109. *
  1110. * @global string $pagenow The filename of the current screen.
  1111. *
  1112. * @param array $settings An array of Heartbeat settings.
  1113. * @return array Filtered Heartbeat settings.
  1114. */
  1115. function wp_heartbeat_set_suspension( $settings ) {
  1116. global $pagenow;
  1117. if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
  1118. $settings['suspension'] = 'disable';
  1119. }
  1120. return $settings;
  1121. }
  1122. /**
  1123. * Performs autosave with heartbeat.
  1124. *
  1125. * @since 3.9.0
  1126. *
  1127. * @param array $response The Heartbeat response.
  1128. * @param array $data The $_POST data sent.
  1129. * @return array The Heartbeat response.
  1130. */
  1131. function heartbeat_autosave( $response, $data ) {
  1132. if ( ! empty( $data['wp_autosave'] ) ) {
  1133. $saved = wp_autosave( $data['wp_autosave'] );
  1134. if ( is_wp_error( $saved ) ) {
  1135. $response['wp_autosave'] = array(
  1136. 'success' => false,
  1137. 'message' => $saved->get_error_message(),
  1138. );
  1139. } elseif ( empty( $saved ) ) {
  1140. $response['wp_autosave'] = array(
  1141. 'success' => false,
  1142. 'message' => __( 'Error while saving.' ),
  1143. );
  1144. } else {
  1145. /* translators: Draft saved date format, see https://www.php.net/manual/datetime.format.php */
  1146. $draft_saved_date_format = __( 'g:i:s a' );
  1147. $response['wp_autosave'] = array(
  1148. 'success' => true,
  1149. /* translators: %s: Date and time. */
  1150. 'message' => sprintf( __( 'Draft saved at %s.' ), date_i18n( $draft_saved_date_format ) ),
  1151. );
  1152. }
  1153. }
  1154. return $response;
  1155. }
  1156. /**
  1157. * Removes single-use URL parameters and create canonical link based on new URL.
  1158. *
  1159. * Removes specific query string parameters from a URL, create the canonical link,
  1160. * put it in the admin header, and change the current URL to match.
  1161. *
  1162. * @since 4.2.0
  1163. */
  1164. function wp_admin_canonical_url() {
  1165. $removable_query_args = wp_removable_query_args();
  1166. if ( empty( $removable_query_args ) ) {
  1167. return;
  1168. }
  1169. // Ensure we're using an absolute URL.
  1170. $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  1171. $filtered_url = remove_query_arg( $removable_query_args, $current_url );
  1172. ?>
  1173. <link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" />
  1174. <script>
  1175. if ( window.history.replaceState ) {
  1176. window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash );
  1177. }
  1178. </script>
  1179. <?php
  1180. }
  1181. /**
  1182. * Sends a referrer policy header so referrers are not sent externally from administration screens.
  1183. *
  1184. * @since 4.9.0
  1185. */
  1186. function wp_admin_headers() {
  1187. $policy = 'strict-origin-when-cross-origin';
  1188. /**
  1189. * Filters the admin referrer policy header value.
  1190. *
  1191. * @since 4.9.0
  1192. * @since 4.9.5 The default value was changed to 'strict-origin-when-cross-origin'.
  1193. *
  1194. * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
  1195. *
  1196. * @param string $policy The admin referrer policy header value. Default 'strict-origin-when-cross-origin'.
  1197. */
  1198. $policy = apply_filters( 'admin_referrer_policy', $policy );
  1199. header( sprintf( 'Referrer-Policy: %s', $policy ) );
  1200. }
  1201. /**
  1202. * Outputs JS that reloads the page if the user navigated to it with the Back or Forward button.
  1203. *
  1204. * Used on the Edit Post and Add New Post screens. Needed to ensure the page is not loaded from browser cache,
  1205. * so the post title and editor content are the last saved versions. Ideally this script should run first in the head.
  1206. *
  1207. * @since 4.6.0
  1208. */
  1209. function wp_page_reload_on_back_button_js() {
  1210. ?>
  1211. <script>
  1212. if ( typeof performance !== 'undefined' && performance.navigation && performance.navigation.type === 2 ) {
  1213. document.location.reload( true );
  1214. }
  1215. </script>
  1216. <?php
  1217. }
  1218. /**
  1219. * Sends a confirmation request email when a change of site admin email address is attempted.
  1220. *
  1221. * The new site admin address will not become active until confirmed.
  1222. *
  1223. * @since 3.0.0
  1224. * @since 4.9.0 This function was moved from wp-admin/includes/ms.php so it's no longer Multisite specific.
  1225. *
  1226. * @param string $old_value The old site admin email address.
  1227. * @param string $value The proposed new site admin email address.
  1228. */
  1229. function update_option_new_admin_email( $old_value, $value ) {
  1230. if ( get_option( 'admin_email' ) === $value || ! is_email( $value ) ) {
  1231. return;
  1232. }
  1233. $hash = md5( $value . time() . wp_rand() );
  1234. $new_admin_email = array(
  1235. 'hash' => $hash,
  1236. 'newemail' => $value,
  1237. );
  1238. update_option( 'adminhash', $new_admin_email );
  1239. $switched_locale = switch_to_locale( get_user_locale() );
  1240. /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
  1241. $email_text = __(
  1242. 'Howdy ###USERNAME###,
  1243. Someone with administrator capabilities recently requested to have the
  1244. administration email address changed on this site:
  1245. ###SITEURL###
  1246. To confirm this change, please click on the following link:
  1247. ###ADMIN_URL###
  1248. You can safely ignore and delete this email if you do not want to
  1249. take this action.
  1250. This email has been sent to ###EMAIL###
  1251. Regards,
  1252. All at ###SITENAME###
  1253. ###SITEURL###'
  1254. );
  1255. /**
  1256. * Filters the text of the email sent when a change of site admin email address is attempted.
  1257. *
  1258. * The following strings have a special meaning and will get replaced dynamically:
  1259. * ###USERNAME### The current user's username.
  1260. * ###ADMIN_URL### The link to click on to confirm the email change.
  1261. * ###EMAIL### The proposed new site admin email address.
  1262. * ###SITENAME### The name of the site.
  1263. * ###SITEURL### The URL to the site.
  1264. *
  1265. * @since MU (3.0.0)
  1266. * @since 4.9.0 This filter is no longer Multisite specific.
  1267. *
  1268. * @param string $email_text Text in the email.
  1269. * @param array $new_admin_email {
  1270. * Data relating to the new site admin email address.
  1271. *
  1272. * @type string $hash The secure hash used in the confirmation link URL.
  1273. * @type string $newemail The proposed new site admin email address.
  1274. * }
  1275. */
  1276. $content = apply_filters( 'new_admin_email_content', $email_text, $new_admin_email );
  1277. $current_user = wp_get_current_user();
  1278. $content = str_replace( '###USERNAME###', $current_user->user_login, $content );
  1279. $content = str_replace( '###ADMIN_URL###', esc_url( self_admin_url( 'options.php?adminhash=' . $hash ) ), $content );
  1280. $content = str_replace( '###EMAIL###', $value, $content );
  1281. $content = str_replace( '###SITENAME###', wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $content );
  1282. $content = str_replace( '###SITEURL###', home_url(), $content );
  1283. if ( '' !== get_option( 'blogname' ) ) {
  1284. $site_title = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
  1285. } else {
  1286. $site_title = parse_url( home_url(), PHP_URL_HOST );
  1287. }
  1288. wp_mail(
  1289. $value,
  1290. sprintf(
  1291. /* translators: New admin email address notification email subject. %s: Site title. */
  1292. __( '[%s] New Admin Email Address' ),
  1293. $site_title
  1294. ),
  1295. $content
  1296. );
  1297. if ( $switched_locale ) {
  1298. restore_previous_locale();
  1299. }
  1300. }
  1301. /**
  1302. * Appends '(Draft)' to draft page titles in the privacy page dropdown
  1303. * so that unpublished content is obvious.
  1304. *
  1305. * @since 4.9.8
  1306. * @access private
  1307. *
  1308. * @param string $title Page title.
  1309. * @param WP_Post $page Page data object.
  1310. * @return string Page title.
  1311. */
  1312. function _wp_privacy_settings_filter_draft_page_titles( $title, $page ) {
  1313. if ( 'draft' === $page->post_status && 'privacy' === get_current_screen()->id ) {
  1314. /* translators: %s: Page title. */
  1315. $title = sprintf( __( '%s (Draft)' ), $title );
  1316. }
  1317. return $title;
  1318. }
  1319. /**
  1320. * Checks if the user needs to update PHP.
  1321. *
  1322. * @since 5.1.0
  1323. * @since 5.1.1 Added the {@see 'wp_is_php_version_acceptable'} filter.
  1324. *
  1325. * @return array|false Array of PHP version data. False on failure.
  1326. */
  1327. function wp_check_php_version() {
  1328. $version = PHP_VERSION;
  1329. $key = md5( $version );
  1330. $response = get_site_transient( 'php_check_' . $key );
  1331. if ( false === $response ) {
  1332. $url = 'http://api.wordpress.org/core/serve-happy/1.0/';
  1333. if ( wp_http_supports( array( 'ssl' ) ) ) {
  1334. $url = set_url_scheme( $url, 'https' );
  1335. }
  1336. $url = add_query_arg( 'php_version', $version, $url );
  1337. $response = wp_remote_get( $url );
  1338. if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
  1339. return false;
  1340. }
  1341. /**
  1342. * Response should be an array with:
  1343. * 'recommended_version' - string - The PHP version recommended by WordPress.
  1344. * 'is_supported' - boolean - Whether the PHP version is actively supported.
  1345. * 'is_secure' - boolean - Whether the PHP version receives security updates.
  1346. * 'is_acceptable' - boolean - Whether the PHP version is still acceptable or warnings
  1347. * should be shown and an update recommended.
  1348. */
  1349. $response = json_decode( wp_remote_retrieve_body( $response ), true );
  1350. if ( ! is_array( $response ) ) {
  1351. return false;
  1352. }
  1353. set_site_transient( 'php_check_' . $key, $response, WEEK_IN_SECONDS );
  1354. }
  1355. if ( isset( $response['is_acceptable'] ) && $response['is_acceptable'] ) {
  1356. /**
  1357. * Filters whether the active PHP version is considered acceptable by WordPress.
  1358. *
  1359. * Returning false will trigger a PHP version warning to show up in the admin dashboard to administrators.
  1360. *
  1361. * This filter is only run if the wordpress.org Serve Happy API considers the PHP version acceptable, ensuring
  1362. * that this filter can only make this check stricter, but not loosen it.
  1363. *
  1364. * @since 5.1.1
  1365. *
  1366. * @param bool $is_acceptable Whether the PHP version is considered acceptable. Default true.
  1367. * @param string $version PHP version checked.
  1368. */
  1369. $response['is_acceptable'] = (bool) apply_filters( 'wp_is_php_version_acceptable', true, $version );
  1370. }
  1371. $response['is_lower_than_future_minimum'] = false;
  1372. // The minimum supported PHP version will be updated to 7.2. Check if the current version is lower.
  1373. if ( version_compare( $version, '7.2', '<' ) ) {
  1374. $response['is_lower_than_future_minimum'] = true;
  1375. // Force showing of warnings.
  1376. $response['is_acceptable'] = false;
  1377. }
  1378. return $response;
  1379. }