暫無描述

post.php 73KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437
  1. <?php
  2. /**
  3. * WordPress Post Administration API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Rename $_POST data from form names to DB post columns.
  10. *
  11. * Manipulates $_POST directly.
  12. *
  13. * @since 2.6.0
  14. *
  15. * @param bool $update Are we updating a pre-existing post?
  16. * @param array $post_data Array of post data. Defaults to the contents of $_POST.
  17. * @return array|WP_Error Array of post data on success, WP_Error on failure.
  18. */
  19. function _wp_translate_postdata( $update = false, $post_data = null ) {
  20. if ( empty( $post_data ) ) {
  21. $post_data = &$_POST;
  22. }
  23. if ( $update ) {
  24. $post_data['ID'] = (int) $post_data['post_ID'];
  25. }
  26. $ptype = get_post_type_object( $post_data['post_type'] );
  27. if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) {
  28. if ( 'page' === $post_data['post_type'] ) {
  29. return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );
  30. } else {
  31. return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );
  32. }
  33. } elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) {
  34. if ( 'page' === $post_data['post_type'] ) {
  35. return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );
  36. } else {
  37. return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );
  38. }
  39. }
  40. if ( isset( $post_data['content'] ) ) {
  41. $post_data['post_content'] = $post_data['content'];
  42. }
  43. if ( isset( $post_data['excerpt'] ) ) {
  44. $post_data['post_excerpt'] = $post_data['excerpt'];
  45. }
  46. if ( isset( $post_data['parent_id'] ) ) {
  47. $post_data['post_parent'] = (int) $post_data['parent_id'];
  48. }
  49. if ( isset( $post_data['trackback_url'] ) ) {
  50. $post_data['to_ping'] = $post_data['trackback_url'];
  51. }
  52. $post_data['user_ID'] = get_current_user_id();
  53. if ( ! empty( $post_data['post_author_override'] ) ) {
  54. $post_data['post_author'] = (int) $post_data['post_author_override'];
  55. } else {
  56. if ( ! empty( $post_data['post_author'] ) ) {
  57. $post_data['post_author'] = (int) $post_data['post_author'];
  58. } else {
  59. $post_data['post_author'] = (int) $post_data['user_ID'];
  60. }
  61. }
  62. if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] )
  63. && ! current_user_can( $ptype->cap->edit_others_posts ) ) {
  64. if ( $update ) {
  65. if ( 'page' === $post_data['post_type'] ) {
  66. return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );
  67. } else {
  68. return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );
  69. }
  70. } else {
  71. if ( 'page' === $post_data['post_type'] ) {
  72. return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );
  73. } else {
  74. return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );
  75. }
  76. }
  77. }
  78. if ( ! empty( $post_data['post_status'] ) ) {
  79. $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
  80. // No longer an auto-draft.
  81. if ( 'auto-draft' === $post_data['post_status'] ) {
  82. $post_data['post_status'] = 'draft';
  83. }
  84. if ( ! get_post_status_object( $post_data['post_status'] ) ) {
  85. unset( $post_data['post_status'] );
  86. }
  87. }
  88. // What to do based on which button they pressed.
  89. if ( isset( $post_data['saveasdraft'] ) && '' !== $post_data['saveasdraft'] ) {
  90. $post_data['post_status'] = 'draft';
  91. }
  92. if ( isset( $post_data['saveasprivate'] ) && '' !== $post_data['saveasprivate'] ) {
  93. $post_data['post_status'] = 'private';
  94. }
  95. if ( isset( $post_data['publish'] ) && ( '' !== $post_data['publish'] )
  96. && ( ! isset( $post_data['post_status'] ) || 'private' !== $post_data['post_status'] )
  97. ) {
  98. $post_data['post_status'] = 'publish';
  99. }
  100. if ( isset( $post_data['advanced'] ) && '' !== $post_data['advanced'] ) {
  101. $post_data['post_status'] = 'draft';
  102. }
  103. if ( isset( $post_data['pending'] ) && '' !== $post_data['pending'] ) {
  104. $post_data['post_status'] = 'pending';
  105. }
  106. if ( isset( $post_data['ID'] ) ) {
  107. $post_id = $post_data['ID'];
  108. } else {
  109. $post_id = false;
  110. }
  111. $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
  112. if ( isset( $post_data['post_status'] ) && 'private' === $post_data['post_status'] && ! current_user_can( $ptype->cap->publish_posts ) ) {
  113. $post_data['post_status'] = $previous_status ? $previous_status : 'pending';
  114. }
  115. $published_statuses = array( 'publish', 'future' );
  116. // Posts 'submitted for approval' are submitted to $_POST the same as if they were being published.
  117. // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
  118. if ( isset( $post_data['post_status'] )
  119. && ( in_array( $post_data['post_status'], $published_statuses, true )
  120. && ! current_user_can( $ptype->cap->publish_posts ) )
  121. ) {
  122. if ( ! in_array( $previous_status, $published_statuses, true ) || ! current_user_can( 'edit_post', $post_id ) ) {
  123. $post_data['post_status'] = 'pending';
  124. }
  125. }
  126. if ( ! isset( $post_data['post_status'] ) ) {
  127. $post_data['post_status'] = 'auto-draft' === $previous_status ? 'draft' : $previous_status;
  128. }
  129. if ( isset( $post_data['post_password'] ) && ! current_user_can( $ptype->cap->publish_posts ) ) {
  130. unset( $post_data['post_password'] );
  131. }
  132. if ( ! isset( $post_data['comment_status'] ) ) {
  133. $post_data['comment_status'] = 'closed';
  134. }
  135. if ( ! isset( $post_data['ping_status'] ) ) {
  136. $post_data['ping_status'] = 'closed';
  137. }
  138. foreach ( array( 'aa', 'mm', 'jj', 'hh', 'mn' ) as $timeunit ) {
  139. if ( ! empty( $post_data[ 'hidden_' . $timeunit ] ) && $post_data[ 'hidden_' . $timeunit ] != $post_data[ $timeunit ] ) {
  140. $post_data['edit_date'] = '1';
  141. break;
  142. }
  143. }
  144. if ( ! empty( $post_data['edit_date'] ) ) {
  145. $aa = $post_data['aa'];
  146. $mm = $post_data['mm'];
  147. $jj = $post_data['jj'];
  148. $hh = $post_data['hh'];
  149. $mn = $post_data['mn'];
  150. $ss = $post_data['ss'];
  151. $aa = ( $aa <= 0 ) ? gmdate( 'Y' ) : $aa;
  152. $mm = ( $mm <= 0 ) ? gmdate( 'n' ) : $mm;
  153. $jj = ( $jj > 31 ) ? 31 : $jj;
  154. $jj = ( $jj <= 0 ) ? gmdate( 'j' ) : $jj;
  155. $hh = ( $hh > 23 ) ? $hh - 24 : $hh;
  156. $mn = ( $mn > 59 ) ? $mn - 60 : $mn;
  157. $ss = ( $ss > 59 ) ? $ss - 60 : $ss;
  158. $post_data['post_date'] = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $aa, $mm, $jj, $hh, $mn, $ss );
  159. $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
  160. if ( ! $valid_date ) {
  161. return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
  162. }
  163. $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
  164. }
  165. if ( isset( $post_data['post_category'] ) ) {
  166. $category_object = get_taxonomy( 'category' );
  167. if ( ! current_user_can( $category_object->cap->assign_terms ) ) {
  168. unset( $post_data['post_category'] );
  169. }
  170. }
  171. return $post_data;
  172. }
  173. /**
  174. * Returns only allowed post data fields
  175. *
  176. * @since 5.0.1
  177. *
  178. * @param array $post_data Array of post data. Defaults to the contents of $_POST.
  179. * @return array|WP_Error Array of post data on success, WP_Error on failure.
  180. */
  181. function _wp_get_allowed_postdata( $post_data = null ) {
  182. if ( empty( $post_data ) ) {
  183. $post_data = $_POST;
  184. }
  185. // Pass through errors.
  186. if ( is_wp_error( $post_data ) ) {
  187. return $post_data;
  188. }
  189. return array_diff_key( $post_data, array_flip( array( 'meta_input', 'file', 'guid' ) ) );
  190. }
  191. /**
  192. * Update an existing post with values provided in $_POST.
  193. *
  194. * If post data is passed as an argument, it is treated as an array of data
  195. * keyed appropriately for turning into a post object.
  196. *
  197. * If post data is not passed, the $_POST global variable is used instead.
  198. *
  199. * @since 1.5.0
  200. *
  201. * @global wpdb $wpdb WordPress database abstraction object.
  202. *
  203. * @param array $post_data Optional. Defaults to the $_POST global.
  204. * @return int Post ID.
  205. */
  206. function edit_post( $post_data = null ) {
  207. global $wpdb;
  208. if ( empty( $post_data ) ) {
  209. $post_data = &$_POST;
  210. }
  211. // Clear out any data in internal vars.
  212. unset( $post_data['filter'] );
  213. $post_ID = (int) $post_data['post_ID'];
  214. $post = get_post( $post_ID );
  215. $post_data['post_type'] = $post->post_type;
  216. $post_data['post_mime_type'] = $post->post_mime_type;
  217. if ( ! empty( $post_data['post_status'] ) ) {
  218. $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
  219. if ( 'inherit' === $post_data['post_status'] ) {
  220. unset( $post_data['post_status'] );
  221. }
  222. }
  223. $ptype = get_post_type_object( $post_data['post_type'] );
  224. if ( ! current_user_can( 'edit_post', $post_ID ) ) {
  225. if ( 'page' === $post_data['post_type'] ) {
  226. wp_die( __( 'Sorry, you are not allowed to edit this page.' ) );
  227. } else {
  228. wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
  229. }
  230. }
  231. if ( post_type_supports( $ptype->name, 'revisions' ) ) {
  232. $revisions = wp_get_post_revisions(
  233. $post_ID,
  234. array(
  235. 'order' => 'ASC',
  236. 'posts_per_page' => 1,
  237. )
  238. );
  239. $revision = current( $revisions );
  240. // Check if the revisions have been upgraded.
  241. if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 ) {
  242. _wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_ID ) );
  243. }
  244. }
  245. if ( isset( $post_data['visibility'] ) ) {
  246. switch ( $post_data['visibility'] ) {
  247. case 'public':
  248. $post_data['post_password'] = '';
  249. break;
  250. case 'password':
  251. unset( $post_data['sticky'] );
  252. break;
  253. case 'private':
  254. $post_data['post_status'] = 'private';
  255. $post_data['post_password'] = '';
  256. unset( $post_data['sticky'] );
  257. break;
  258. }
  259. }
  260. $post_data = _wp_translate_postdata( true, $post_data );
  261. if ( is_wp_error( $post_data ) ) {
  262. wp_die( $post_data->get_error_message() );
  263. }
  264. $translated = _wp_get_allowed_postdata( $post_data );
  265. // Post formats.
  266. if ( isset( $post_data['post_format'] ) ) {
  267. set_post_format( $post_ID, $post_data['post_format'] );
  268. }
  269. $format_meta_urls = array( 'url', 'link_url', 'quote_source_url' );
  270. foreach ( $format_meta_urls as $format_meta_url ) {
  271. $keyed = '_format_' . $format_meta_url;
  272. if ( isset( $post_data[ $keyed ] ) ) {
  273. update_post_meta( $post_ID, $keyed, wp_slash( esc_url_raw( wp_unslash( $post_data[ $keyed ] ) ) ) );
  274. }
  275. }
  276. $format_keys = array( 'quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed' );
  277. foreach ( $format_keys as $key ) {
  278. $keyed = '_format_' . $key;
  279. if ( isset( $post_data[ $keyed ] ) ) {
  280. if ( current_user_can( 'unfiltered_html' ) ) {
  281. update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] );
  282. } else {
  283. update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
  284. }
  285. }
  286. }
  287. if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) {
  288. $id3data = wp_get_attachment_metadata( $post_ID );
  289. if ( ! is_array( $id3data ) ) {
  290. $id3data = array();
  291. }
  292. foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) {
  293. if ( isset( $post_data[ 'id3_' . $key ] ) ) {
  294. $id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) );
  295. }
  296. }
  297. wp_update_attachment_metadata( $post_ID, $id3data );
  298. }
  299. // Meta stuff.
  300. if ( isset( $post_data['meta'] ) && $post_data['meta'] ) {
  301. foreach ( $post_data['meta'] as $key => $value ) {
  302. $meta = get_post_meta_by_id( $key );
  303. if ( ! $meta ) {
  304. continue;
  305. }
  306. if ( $meta->post_id != $post_ID ) {
  307. continue;
  308. }
  309. if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $meta->meta_key ) ) {
  310. continue;
  311. }
  312. if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) ) {
  313. continue;
  314. }
  315. update_meta( $key, $value['key'], $value['value'] );
  316. }
  317. }
  318. if ( isset( $post_data['deletemeta'] ) && $post_data['deletemeta'] ) {
  319. foreach ( $post_data['deletemeta'] as $key => $value ) {
  320. $meta = get_post_meta_by_id( $key );
  321. if ( ! $meta ) {
  322. continue;
  323. }
  324. if ( $meta->post_id != $post_ID ) {
  325. continue;
  326. }
  327. if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) ) {
  328. continue;
  329. }
  330. delete_meta( $key );
  331. }
  332. }
  333. // Attachment stuff.
  334. if ( 'attachment' === $post_data['post_type'] ) {
  335. if ( isset( $post_data['_wp_attachment_image_alt'] ) ) {
  336. $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] );
  337. if ( get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) !== $image_alt ) {
  338. $image_alt = wp_strip_all_tags( $image_alt, true );
  339. // update_post_meta() expects slashed.
  340. update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
  341. }
  342. }
  343. $attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array();
  344. /** This filter is documented in wp-admin/includes/media.php */
  345. $translated = apply_filters( 'attachment_fields_to_save', $translated, $attachment_data );
  346. }
  347. // Convert taxonomy input to term IDs, to avoid ambiguity.
  348. if ( isset( $post_data['tax_input'] ) ) {
  349. foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) {
  350. $tax_object = get_taxonomy( $taxonomy );
  351. if ( $tax_object && isset( $tax_object->meta_box_sanitize_cb ) ) {
  352. $translated['tax_input'][ $taxonomy ] = call_user_func_array( $tax_object->meta_box_sanitize_cb, array( $taxonomy, $terms ) );
  353. }
  354. }
  355. }
  356. add_meta( $post_ID );
  357. update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
  358. $success = wp_update_post( $translated );
  359. // If the save failed, see if we can sanity check the main fields and try again.
  360. if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) {
  361. $fields = array( 'post_title', 'post_content', 'post_excerpt' );
  362. foreach ( $fields as $field ) {
  363. if ( isset( $translated[ $field ] ) ) {
  364. $translated[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $translated[ $field ] );
  365. }
  366. }
  367. wp_update_post( $translated );
  368. }
  369. // Now that we have an ID we can fix any attachment anchor hrefs.
  370. _fix_attachment_links( $post_ID );
  371. wp_set_post_lock( $post_ID );
  372. if ( current_user_can( $ptype->cap->edit_others_posts ) && current_user_can( $ptype->cap->publish_posts ) ) {
  373. if ( ! empty( $post_data['sticky'] ) ) {
  374. stick_post( $post_ID );
  375. } else {
  376. unstick_post( $post_ID );
  377. }
  378. }
  379. return $post_ID;
  380. }
  381. /**
  382. * Process the post data for the bulk editing of posts.
  383. *
  384. * Updates all bulk edited posts/pages, adding (but not removing) tags and
  385. * categories. Skips pages when they would be their own parent or child.
  386. *
  387. * @since 2.7.0
  388. *
  389. * @global wpdb $wpdb WordPress database abstraction object.
  390. *
  391. * @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal.
  392. * @return array
  393. */
  394. function bulk_edit_posts( $post_data = null ) {
  395. global $wpdb;
  396. if ( empty( $post_data ) ) {
  397. $post_data = &$_POST;
  398. }
  399. if ( isset( $post_data['post_type'] ) ) {
  400. $ptype = get_post_type_object( $post_data['post_type'] );
  401. } else {
  402. $ptype = get_post_type_object( 'post' );
  403. }
  404. if ( ! current_user_can( $ptype->cap->edit_posts ) ) {
  405. if ( 'page' === $ptype->name ) {
  406. wp_die( __( 'Sorry, you are not allowed to edit pages.' ) );
  407. } else {
  408. wp_die( __( 'Sorry, you are not allowed to edit posts.' ) );
  409. }
  410. }
  411. if ( -1 == $post_data['_status'] ) {
  412. $post_data['post_status'] = null;
  413. unset( $post_data['post_status'] );
  414. } else {
  415. $post_data['post_status'] = $post_data['_status'];
  416. }
  417. unset( $post_data['_status'] );
  418. if ( ! empty( $post_data['post_status'] ) ) {
  419. $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
  420. if ( 'inherit' === $post_data['post_status'] ) {
  421. unset( $post_data['post_status'] );
  422. }
  423. }
  424. $post_IDs = array_map( 'intval', (array) $post_data['post'] );
  425. $reset = array(
  426. 'post_author',
  427. 'post_status',
  428. 'post_password',
  429. 'post_parent',
  430. 'page_template',
  431. 'comment_status',
  432. 'ping_status',
  433. 'keep_private',
  434. 'tax_input',
  435. 'post_category',
  436. 'sticky',
  437. 'post_format',
  438. );
  439. foreach ( $reset as $field ) {
  440. if ( isset( $post_data[ $field ] ) && ( '' === $post_data[ $field ] || -1 == $post_data[ $field ] ) ) {
  441. unset( $post_data[ $field ] );
  442. }
  443. }
  444. if ( isset( $post_data['post_category'] ) ) {
  445. if ( is_array( $post_data['post_category'] ) && ! empty( $post_data['post_category'] ) ) {
  446. $new_cats = array_map( 'absint', $post_data['post_category'] );
  447. } else {
  448. unset( $post_data['post_category'] );
  449. }
  450. }
  451. $tax_input = array();
  452. if ( isset( $post_data['tax_input'] ) ) {
  453. foreach ( $post_data['tax_input'] as $tax_name => $terms ) {
  454. if ( empty( $terms ) ) {
  455. continue;
  456. }
  457. if ( is_taxonomy_hierarchical( $tax_name ) ) {
  458. $tax_input[ $tax_name ] = array_map( 'absint', $terms );
  459. } else {
  460. $comma = _x( ',', 'tag delimiter' );
  461. if ( ',' !== $comma ) {
  462. $terms = str_replace( $comma, ',', $terms );
  463. }
  464. $tax_input[ $tax_name ] = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
  465. }
  466. }
  467. }
  468. if ( isset( $post_data['post_parent'] ) && (int) $post_data['post_parent'] ) {
  469. $parent = (int) $post_data['post_parent'];
  470. $pages = $wpdb->get_results( "SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'" );
  471. $children = array();
  472. for ( $i = 0; $i < 50 && $parent > 0; $i++ ) {
  473. $children[] = $parent;
  474. foreach ( $pages as $page ) {
  475. if ( (int) $page->ID === $parent ) {
  476. $parent = (int) $page->post_parent;
  477. break;
  478. }
  479. }
  480. }
  481. }
  482. $updated = array();
  483. $skipped = array();
  484. $locked = array();
  485. $shared_post_data = $post_data;
  486. foreach ( $post_IDs as $post_ID ) {
  487. // Start with fresh post data with each iteration.
  488. $post_data = $shared_post_data;
  489. $post_type_object = get_post_type_object( get_post_type( $post_ID ) );
  490. if ( ! isset( $post_type_object )
  491. || ( isset( $children ) && in_array( $post_ID, $children, true ) )
  492. || ! current_user_can( 'edit_post', $post_ID )
  493. ) {
  494. $skipped[] = $post_ID;
  495. continue;
  496. }
  497. if ( wp_check_post_lock( $post_ID ) ) {
  498. $locked[] = $post_ID;
  499. continue;
  500. }
  501. $post = get_post( $post_ID );
  502. $tax_names = get_object_taxonomies( $post );
  503. foreach ( $tax_names as $tax_name ) {
  504. $taxonomy_obj = get_taxonomy( $tax_name );
  505. if ( isset( $tax_input[ $tax_name ] ) && current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
  506. $new_terms = $tax_input[ $tax_name ];
  507. } else {
  508. $new_terms = array();
  509. }
  510. if ( $taxonomy_obj->hierarchical ) {
  511. $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array( 'fields' => 'ids' ) );
  512. } else {
  513. $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array( 'fields' => 'names' ) );
  514. }
  515. $post_data['tax_input'][ $tax_name ] = array_merge( $current_terms, $new_terms );
  516. }
  517. if ( isset( $new_cats ) && in_array( 'category', $tax_names, true ) ) {
  518. $cats = (array) wp_get_post_categories( $post_ID );
  519. $post_data['post_category'] = array_unique( array_merge( $cats, $new_cats ) );
  520. unset( $post_data['tax_input']['category'] );
  521. }
  522. $post_data['post_ID'] = $post_ID;
  523. $post_data['post_type'] = $post->post_type;
  524. $post_data['post_mime_type'] = $post->post_mime_type;
  525. foreach ( array( 'comment_status', 'ping_status', 'post_author' ) as $field ) {
  526. if ( ! isset( $post_data[ $field ] ) ) {
  527. $post_data[ $field ] = $post->$field;
  528. }
  529. }
  530. $post_data = _wp_translate_postdata( true, $post_data );
  531. if ( is_wp_error( $post_data ) ) {
  532. $skipped[] = $post_ID;
  533. continue;
  534. }
  535. $post_data = _wp_get_allowed_postdata( $post_data );
  536. if ( isset( $shared_post_data['post_format'] ) ) {
  537. set_post_format( $post_ID, $shared_post_data['post_format'] );
  538. }
  539. // Prevent wp_insert_post() from overwriting post format with the old data.
  540. unset( $post_data['tax_input']['post_format'] );
  541. $updated[] = wp_update_post( $post_data );
  542. if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
  543. if ( 'sticky' === $post_data['sticky'] ) {
  544. stick_post( $post_ID );
  545. } else {
  546. unstick_post( $post_ID );
  547. }
  548. }
  549. }
  550. return array(
  551. 'updated' => $updated,
  552. 'skipped' => $skipped,
  553. 'locked' => $locked,
  554. );
  555. }
  556. /**
  557. * Default post information to use when populating the "Write Post" form.
  558. *
  559. * @since 2.0.0
  560. *
  561. * @param string $post_type Optional. A post type string. Default 'post'.
  562. * @param bool $create_in_db Optional. Whether to insert the post into database. Default false.
  563. * @return WP_Post Post object containing all the default post data as attributes
  564. */
  565. function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
  566. $post_title = '';
  567. if ( ! empty( $_REQUEST['post_title'] ) ) {
  568. $post_title = esc_html( wp_unslash( $_REQUEST['post_title'] ) );
  569. }
  570. $post_content = '';
  571. if ( ! empty( $_REQUEST['content'] ) ) {
  572. $post_content = esc_html( wp_unslash( $_REQUEST['content'] ) );
  573. }
  574. $post_excerpt = '';
  575. if ( ! empty( $_REQUEST['excerpt'] ) ) {
  576. $post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] ) );
  577. }
  578. if ( $create_in_db ) {
  579. $post_id = wp_insert_post(
  580. array(
  581. 'post_title' => __( 'Auto Draft' ),
  582. 'post_type' => $post_type,
  583. 'post_status' => 'auto-draft',
  584. ),
  585. false,
  586. false
  587. );
  588. $post = get_post( $post_id );
  589. if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) {
  590. set_post_format( $post, get_option( 'default_post_format' ) );
  591. }
  592. wp_after_insert_post( $post, false, null );
  593. // Schedule auto-draft cleanup.
  594. if ( ! wp_next_scheduled( 'wp_scheduled_auto_draft_delete' ) ) {
  595. wp_schedule_event( time(), 'daily', 'wp_scheduled_auto_draft_delete' );
  596. }
  597. } else {
  598. $post = new stdClass;
  599. $post->ID = 0;
  600. $post->post_author = '';
  601. $post->post_date = '';
  602. $post->post_date_gmt = '';
  603. $post->post_password = '';
  604. $post->post_name = '';
  605. $post->post_type = $post_type;
  606. $post->post_status = 'draft';
  607. $post->to_ping = '';
  608. $post->pinged = '';
  609. $post->comment_status = get_default_comment_status( $post_type );
  610. $post->ping_status = get_default_comment_status( $post_type, 'pingback' );
  611. $post->post_pingback = get_option( 'default_pingback_flag' );
  612. $post->post_category = get_option( 'default_category' );
  613. $post->page_template = 'default';
  614. $post->post_parent = 0;
  615. $post->menu_order = 0;
  616. $post = new WP_Post( $post );
  617. }
  618. /**
  619. * Filters the default post content initially used in the "Write Post" form.
  620. *
  621. * @since 1.5.0
  622. *
  623. * @param string $post_content Default post content.
  624. * @param WP_Post $post Post object.
  625. */
  626. $post->post_content = (string) apply_filters( 'default_content', $post_content, $post );
  627. /**
  628. * Filters the default post title initially used in the "Write Post" form.
  629. *
  630. * @since 1.5.0
  631. *
  632. * @param string $post_title Default post title.
  633. * @param WP_Post $post Post object.
  634. */
  635. $post->post_title = (string) apply_filters( 'default_title', $post_title, $post );
  636. /**
  637. * Filters the default post excerpt initially used in the "Write Post" form.
  638. *
  639. * @since 1.5.0
  640. *
  641. * @param string $post_excerpt Default post excerpt.
  642. * @param WP_Post $post Post object.
  643. */
  644. $post->post_excerpt = (string) apply_filters( 'default_excerpt', $post_excerpt, $post );
  645. return $post;
  646. }
  647. /**
  648. * Determines if a post exists based on title, content, date and type.
  649. *
  650. * @since 2.0.0
  651. * @since 5.2.0 Added the `$type` parameter.
  652. * @since 5.8.0 Added the `$status` parameter.
  653. *
  654. * @global wpdb $wpdb WordPress database abstraction object.
  655. *
  656. * @param string $title Post title.
  657. * @param string $content Optional post content.
  658. * @param string $date Optional post date.
  659. * @param string $type Optional post type.
  660. * @param string $status Optional post status.
  661. * @return int Post ID if post exists, 0 otherwise.
  662. */
  663. function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) {
  664. global $wpdb;
  665. $post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
  666. $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
  667. $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
  668. $post_type = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) );
  669. $post_status = wp_unslash( sanitize_post_field( 'post_status', $status, 0, 'db' ) );
  670. $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
  671. $args = array();
  672. if ( ! empty( $date ) ) {
  673. $query .= ' AND post_date = %s';
  674. $args[] = $post_date;
  675. }
  676. if ( ! empty( $title ) ) {
  677. $query .= ' AND post_title = %s';
  678. $args[] = $post_title;
  679. }
  680. if ( ! empty( $content ) ) {
  681. $query .= ' AND post_content = %s';
  682. $args[] = $post_content;
  683. }
  684. if ( ! empty( $type ) ) {
  685. $query .= ' AND post_type = %s';
  686. $args[] = $post_type;
  687. }
  688. if ( ! empty( $status ) ) {
  689. $query .= ' AND post_status = %s';
  690. $args[] = $post_status;
  691. }
  692. if ( ! empty( $args ) ) {
  693. return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) );
  694. }
  695. return 0;
  696. }
  697. /**
  698. * Creates a new post from the "Write Post" form using $_POST information.
  699. *
  700. * @since 2.1.0
  701. *
  702. * @global WP_User $current_user
  703. *
  704. * @return int|WP_Error Post ID on success, WP_Error on failure.
  705. */
  706. function wp_write_post() {
  707. if ( isset( $_POST['post_type'] ) ) {
  708. $ptype = get_post_type_object( $_POST['post_type'] );
  709. } else {
  710. $ptype = get_post_type_object( 'post' );
  711. }
  712. if ( ! current_user_can( $ptype->cap->edit_posts ) ) {
  713. if ( 'page' === $ptype->name ) {
  714. return new WP_Error( 'edit_pages', __( 'Sorry, you are not allowed to create pages on this site.' ) );
  715. } else {
  716. return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to create posts or drafts on this site.' ) );
  717. }
  718. }
  719. $_POST['post_mime_type'] = '';
  720. // Clear out any data in internal vars.
  721. unset( $_POST['filter'] );
  722. // Edit, don't write, if we have a post ID.
  723. if ( isset( $_POST['post_ID'] ) ) {
  724. return edit_post();
  725. }
  726. if ( isset( $_POST['visibility'] ) ) {
  727. switch ( $_POST['visibility'] ) {
  728. case 'public':
  729. $_POST['post_password'] = '';
  730. break;
  731. case 'password':
  732. unset( $_POST['sticky'] );
  733. break;
  734. case 'private':
  735. $_POST['post_status'] = 'private';
  736. $_POST['post_password'] = '';
  737. unset( $_POST['sticky'] );
  738. break;
  739. }
  740. }
  741. $translated = _wp_translate_postdata( false );
  742. if ( is_wp_error( $translated ) ) {
  743. return $translated;
  744. }
  745. $translated = _wp_get_allowed_postdata( $translated );
  746. // Create the post.
  747. $post_ID = wp_insert_post( $translated );
  748. if ( is_wp_error( $post_ID ) ) {
  749. return $post_ID;
  750. }
  751. if ( empty( $post_ID ) ) {
  752. return 0;
  753. }
  754. add_meta( $post_ID );
  755. add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
  756. // Now that we have an ID we can fix any attachment anchor hrefs.
  757. _fix_attachment_links( $post_ID );
  758. wp_set_post_lock( $post_ID );
  759. return $post_ID;
  760. }
  761. /**
  762. * Calls wp_write_post() and handles the errors.
  763. *
  764. * @since 2.0.0
  765. *
  766. * @return int|void Post ID on success, void on failure.
  767. */
  768. function write_post() {
  769. $result = wp_write_post();
  770. if ( is_wp_error( $result ) ) {
  771. wp_die( $result->get_error_message() );
  772. } else {
  773. return $result;
  774. }
  775. }
  776. //
  777. // Post Meta.
  778. //
  779. /**
  780. * Add post meta data defined in $_POST superglobal for post with given ID.
  781. *
  782. * @since 1.2.0
  783. *
  784. * @param int $post_ID
  785. * @return int|bool
  786. */
  787. function add_meta( $post_ID ) {
  788. $post_ID = (int) $post_ID;
  789. $metakeyselect = isset( $_POST['metakeyselect'] ) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';
  790. $metakeyinput = isset( $_POST['metakeyinput'] ) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : '';
  791. $metavalue = isset( $_POST['metavalue'] ) ? $_POST['metavalue'] : '';
  792. if ( is_string( $metavalue ) ) {
  793. $metavalue = trim( $metavalue );
  794. }
  795. if ( ( ( '#NONE#' !== $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput ) ) {
  796. /*
  797. * We have a key/value pair. If both the select and the input
  798. * for the key have data, the input takes precedence.
  799. */
  800. if ( '#NONE#' !== $metakeyselect ) {
  801. $metakey = $metakeyselect;
  802. }
  803. if ( $metakeyinput ) {
  804. $metakey = $metakeyinput; // Default.
  805. }
  806. if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) ) {
  807. return false;
  808. }
  809. $metakey = wp_slash( $metakey );
  810. return add_post_meta( $post_ID, $metakey, $metavalue );
  811. }
  812. return false;
  813. }
  814. /**
  815. * Delete post meta data by meta ID.
  816. *
  817. * @since 1.2.0
  818. *
  819. * @param int $mid
  820. * @return bool
  821. */
  822. function delete_meta( $mid ) {
  823. return delete_metadata_by_mid( 'post', $mid );
  824. }
  825. /**
  826. * Get a list of previously defined keys.
  827. *
  828. * @since 1.2.0
  829. *
  830. * @global wpdb $wpdb WordPress database abstraction object.
  831. *
  832. * @return mixed
  833. */
  834. function get_meta_keys() {
  835. global $wpdb;
  836. $keys = $wpdb->get_col(
  837. "
  838. SELECT meta_key
  839. FROM $wpdb->postmeta
  840. GROUP BY meta_key
  841. ORDER BY meta_key"
  842. );
  843. return $keys;
  844. }
  845. /**
  846. * Get post meta data by meta ID.
  847. *
  848. * @since 2.1.0
  849. *
  850. * @param int $mid
  851. * @return object|bool
  852. */
  853. function get_post_meta_by_id( $mid ) {
  854. return get_metadata_by_mid( 'post', $mid );
  855. }
  856. /**
  857. * Get meta data for the given post ID.
  858. *
  859. * @since 1.2.0
  860. *
  861. * @global wpdb $wpdb WordPress database abstraction object.
  862. *
  863. * @param int $postid
  864. * @return mixed
  865. */
  866. function has_meta( $postid ) {
  867. global $wpdb;
  868. return $wpdb->get_results(
  869. $wpdb->prepare(
  870. "SELECT meta_key, meta_value, meta_id, post_id
  871. FROM $wpdb->postmeta WHERE post_id = %d
  872. ORDER BY meta_key,meta_id",
  873. $postid
  874. ),
  875. ARRAY_A
  876. );
  877. }
  878. /**
  879. * Update post meta data by meta ID.
  880. *
  881. * @since 1.2.0
  882. *
  883. * @param int $meta_id
  884. * @param string $meta_key Expect Slashed
  885. * @param string $meta_value Expect Slashed
  886. * @return bool
  887. */
  888. function update_meta( $meta_id, $meta_key, $meta_value ) {
  889. $meta_key = wp_unslash( $meta_key );
  890. $meta_value = wp_unslash( $meta_value );
  891. return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key );
  892. }
  893. //
  894. // Private.
  895. //
  896. /**
  897. * Replace hrefs of attachment anchors with up-to-date permalinks.
  898. *
  899. * @since 2.3.0
  900. * @access private
  901. *
  902. * @param int|object $post Post ID or post object.
  903. * @return void|int|WP_Error Void if nothing fixed. 0 or WP_Error on update failure. The post ID on update success.
  904. */
  905. function _fix_attachment_links( $post ) {
  906. $post = get_post( $post, ARRAY_A );
  907. $content = $post['post_content'];
  908. // Don't run if no pretty permalinks or post is not published, scheduled, or privately published.
  909. if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ), true ) ) {
  910. return;
  911. }
  912. // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero).
  913. if ( ! strpos( $content, '?attachment_id=' ) || ! preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) ) {
  914. return;
  915. }
  916. $site_url = get_bloginfo( 'url' );
  917. $site_url = substr( $site_url, (int) strpos( $site_url, '://' ) ); // Remove the http(s).
  918. $replace = '';
  919. foreach ( $link_matches[1] as $key => $value ) {
  920. if ( ! strpos( $value, '?attachment_id=' ) || ! strpos( $value, 'wp-att-' )
  921. || ! preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match )
  922. || ! preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) ) {
  923. continue;
  924. }
  925. $quote = $url_match[1]; // The quote (single or double).
  926. $url_id = (int) $url_match[2];
  927. $rel_id = (int) $rel_match[1];
  928. if ( ! $url_id || ! $rel_id || $url_id != $rel_id || strpos( $url_match[0], $site_url ) === false ) {
  929. continue;
  930. }
  931. $link = $link_matches[0][ $key ];
  932. $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );
  933. $content = str_replace( $link, $replace, $content );
  934. }
  935. if ( $replace ) {
  936. $post['post_content'] = $content;
  937. // Escape data pulled from DB.
  938. $post = add_magic_quotes( $post );
  939. return wp_update_post( $post );
  940. }
  941. }
  942. /**
  943. * Get all the possible statuses for a post_type
  944. *
  945. * @since 2.5.0
  946. *
  947. * @param string $type The post_type you want the statuses for. Default 'post'.
  948. * @return string[] An array of all the statuses for the supplied post type.
  949. */
  950. function get_available_post_statuses( $type = 'post' ) {
  951. $stati = wp_count_posts( $type );
  952. return array_keys( get_object_vars( $stati ) );
  953. }
  954. /**
  955. * Run the wp query to fetch the posts for listing on the edit posts page
  956. *
  957. * @since 2.5.0
  958. *
  959. * @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal.
  960. * @return array
  961. */
  962. function wp_edit_posts_query( $q = false ) {
  963. if ( false === $q ) {
  964. $q = $_GET;
  965. }
  966. $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0;
  967. $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
  968. $post_stati = get_post_stati();
  969. if ( isset( $q['post_type'] ) && in_array( $q['post_type'], get_post_types(), true ) ) {
  970. $post_type = $q['post_type'];
  971. } else {
  972. $post_type = 'post';
  973. }
  974. $avail_post_stati = get_available_post_statuses( $post_type );
  975. $post_status = '';
  976. $perm = '';
  977. if ( isset( $q['post_status'] ) && in_array( $q['post_status'], $post_stati, true ) ) {
  978. $post_status = $q['post_status'];
  979. $perm = 'readable';
  980. }
  981. $orderby = '';
  982. if ( isset( $q['orderby'] ) ) {
  983. $orderby = $q['orderby'];
  984. } elseif ( isset( $q['post_status'] ) && in_array( $q['post_status'], array( 'pending', 'draft' ), true ) ) {
  985. $orderby = 'modified';
  986. }
  987. $order = '';
  988. if ( isset( $q['order'] ) ) {
  989. $order = $q['order'];
  990. } elseif ( isset( $q['post_status'] ) && 'pending' === $q['post_status'] ) {
  991. $order = 'ASC';
  992. }
  993. $per_page = "edit_{$post_type}_per_page";
  994. $posts_per_page = (int) get_user_option( $per_page );
  995. if ( empty( $posts_per_page ) || $posts_per_page < 1 ) {
  996. $posts_per_page = 20;
  997. }
  998. /**
  999. * Filters the number of items per page to show for a specific 'per_page' type.
  1000. *
  1001. * The dynamic portion of the hook name, `$post_type`, refers to the post type.
  1002. *
  1003. * Possible hook names include:
  1004. *
  1005. * - `edit_post_per_page`
  1006. * - `edit_page_per_page`
  1007. * - `edit_attachment_per_page`
  1008. *
  1009. * @since 3.0.0
  1010. *
  1011. * @param int $posts_per_page Number of posts to display per page for the given post
  1012. * type. Default 20.
  1013. */
  1014. $posts_per_page = apply_filters( "edit_{$post_type}_per_page", $posts_per_page );
  1015. /**
  1016. * Filters the number of posts displayed per page when specifically listing "posts".
  1017. *
  1018. * @since 2.8.0
  1019. *
  1020. * @param int $posts_per_page Number of posts to be displayed. Default 20.
  1021. * @param string $post_type The post type.
  1022. */
  1023. $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type );
  1024. $query = compact( 'post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page' );
  1025. // Hierarchical types require special args.
  1026. if ( is_post_type_hierarchical( $post_type ) && empty( $orderby ) ) {
  1027. $query['orderby'] = 'menu_order title';
  1028. $query['order'] = 'asc';
  1029. $query['posts_per_page'] = -1;
  1030. $query['posts_per_archive_page'] = -1;
  1031. $query['fields'] = 'id=>parent';
  1032. }
  1033. if ( ! empty( $q['show_sticky'] ) ) {
  1034. $query['post__in'] = (array) get_option( 'sticky_posts' );
  1035. }
  1036. wp( $query );
  1037. return $avail_post_stati;
  1038. }
  1039. /**
  1040. * Get the query variables for the current attachments request.
  1041. *
  1042. * @since 4.2.0
  1043. *
  1044. * @param array|false $q Optional. Array of query variables to use to build the query or false
  1045. * to use $_GET superglobal. Default false.
  1046. * @return array The parsed query vars.
  1047. */
  1048. function wp_edit_attachments_query_vars( $q = false ) {
  1049. if ( false === $q ) {
  1050. $q = $_GET;
  1051. }
  1052. $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0;
  1053. $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
  1054. $q['post_type'] = 'attachment';
  1055. $post_type = get_post_type_object( 'attachment' );
  1056. $states = 'inherit';
  1057. if ( current_user_can( $post_type->cap->read_private_posts ) ) {
  1058. $states .= ',private';
  1059. }
  1060. $q['post_status'] = isset( $q['status'] ) && 'trash' === $q['status'] ? 'trash' : $states;
  1061. $q['post_status'] = isset( $q['attachment-filter'] ) && 'trash' === $q['attachment-filter'] ? 'trash' : $states;
  1062. $media_per_page = (int) get_user_option( 'upload_per_page' );
  1063. if ( empty( $media_per_page ) || $media_per_page < 1 ) {
  1064. $media_per_page = 20;
  1065. }
  1066. /**
  1067. * Filters the number of items to list per page when listing media items.
  1068. *
  1069. * @since 2.9.0
  1070. *
  1071. * @param int $media_per_page Number of media to list. Default 20.
  1072. */
  1073. $q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page );
  1074. $post_mime_types = get_post_mime_types();
  1075. if ( isset( $q['post_mime_type'] ) && ! array_intersect( (array) $q['post_mime_type'], array_keys( $post_mime_types ) ) ) {
  1076. unset( $q['post_mime_type'] );
  1077. }
  1078. foreach ( array_keys( $post_mime_types ) as $type ) {
  1079. if ( isset( $q['attachment-filter'] ) && "post_mime_type:$type" === $q['attachment-filter'] ) {
  1080. $q['post_mime_type'] = $type;
  1081. break;
  1082. }
  1083. }
  1084. if ( isset( $q['detached'] ) || ( isset( $q['attachment-filter'] ) && 'detached' === $q['attachment-filter'] ) ) {
  1085. $q['post_parent'] = 0;
  1086. }
  1087. if ( isset( $q['mine'] ) || ( isset( $q['attachment-filter'] ) && 'mine' === $q['attachment-filter'] ) ) {
  1088. $q['author'] = get_current_user_id();
  1089. }
  1090. // Filter query clauses to include filenames.
  1091. if ( isset( $q['s'] ) ) {
  1092. add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
  1093. }
  1094. return $q;
  1095. }
  1096. /**
  1097. * Executes a query for attachments. An array of WP_Query arguments
  1098. * can be passed in, which will override the arguments set by this function.
  1099. *
  1100. * @since 2.5.0
  1101. *
  1102. * @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal.
  1103. * @return array
  1104. */
  1105. function wp_edit_attachments_query( $q = false ) {
  1106. wp( wp_edit_attachments_query_vars( $q ) );
  1107. $post_mime_types = get_post_mime_types();
  1108. $avail_post_mime_types = get_available_post_mime_types( 'attachment' );
  1109. return array( $post_mime_types, $avail_post_mime_types );
  1110. }
  1111. /**
  1112. * Returns the list of classes to be used by a meta box.
  1113. *
  1114. * @since 2.5.0
  1115. *
  1116. * @param string $box_id Meta box ID (used in the 'id' attribute for the meta box).
  1117. * @param string $screen_id The screen on which the meta box is shown.
  1118. * @return string Space-separated string of class names.
  1119. */
  1120. function postbox_classes( $box_id, $screen_id ) {
  1121. if ( isset( $_GET['edit'] ) && $_GET['edit'] == $box_id ) {
  1122. $classes = array( '' );
  1123. } elseif ( get_user_option( 'closedpostboxes_' . $screen_id ) ) {
  1124. $closed = get_user_option( 'closedpostboxes_' . $screen_id );
  1125. if ( ! is_array( $closed ) ) {
  1126. $classes = array( '' );
  1127. } else {
  1128. $classes = in_array( $box_id, $closed, true ) ? array( 'closed' ) : array( '' );
  1129. }
  1130. } else {
  1131. $classes = array( '' );
  1132. }
  1133. /**
  1134. * Filters the postbox classes for a specific screen and box ID combo.
  1135. *
  1136. * The dynamic portions of the hook name, `$screen_id` and `$box_id`, refer to
  1137. * the screen ID and meta box ID, respectively.
  1138. *
  1139. * @since 3.2.0
  1140. *
  1141. * @param string[] $classes An array of postbox classes.
  1142. */
  1143. $classes = apply_filters( "postbox_classes_{$screen_id}_{$box_id}", $classes );
  1144. return implode( ' ', $classes );
  1145. }
  1146. /**
  1147. * Get a sample permalink based off of the post name.
  1148. *
  1149. * @since 2.5.0
  1150. *
  1151. * @param int|WP_Post $id Post ID or post object.
  1152. * @param string $title Optional. Title to override the post's current title when generating the post name. Default null.
  1153. * @param string $name Optional. Name to override the post name. Default null.
  1154. * @return array {
  1155. * Array containing the sample permalink with placeholder for the post name, and the post name.
  1156. *
  1157. * @type string $0 The permalink with placeholder for the post name.
  1158. * @type string $1 The post name.
  1159. * }
  1160. */
  1161. function get_sample_permalink( $id, $title = null, $name = null ) {
  1162. $post = get_post( $id );
  1163. if ( ! $post ) {
  1164. return array( '', '' );
  1165. }
  1166. $ptype = get_post_type_object( $post->post_type );
  1167. $original_status = $post->post_status;
  1168. $original_date = $post->post_date;
  1169. $original_name = $post->post_name;
  1170. // Hack: get_permalink() would return plain permalink for drafts, so we will fake that our post is published.
  1171. if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ), true ) ) {
  1172. $post->post_status = 'publish';
  1173. $post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
  1174. }
  1175. // If the user wants to set a new name -- override the current one.
  1176. // Note: if empty name is supplied -- use the title instead, see #6072.
  1177. if ( ! is_null( $name ) ) {
  1178. $post->post_name = sanitize_title( $name ? $name : $title, $post->ID );
  1179. }
  1180. $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
  1181. $post->filter = 'sample';
  1182. $permalink = get_permalink( $post, true );
  1183. // Replace custom post_type token with generic pagename token for ease of use.
  1184. $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink );
  1185. // Handle page hierarchy.
  1186. if ( $ptype->hierarchical ) {
  1187. $uri = get_page_uri( $post );
  1188. if ( $uri ) {
  1189. $uri = untrailingslashit( $uri );
  1190. $uri = strrev( stristr( strrev( $uri ), '/' ) );
  1191. $uri = untrailingslashit( $uri );
  1192. }
  1193. /** This filter is documented in wp-admin/edit-tag-form.php */
  1194. $uri = apply_filters( 'editable_slug', $uri, $post );
  1195. if ( ! empty( $uri ) ) {
  1196. $uri .= '/';
  1197. }
  1198. $permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink );
  1199. }
  1200. /** This filter is documented in wp-admin/edit-tag-form.php */
  1201. $permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) );
  1202. $post->post_status = $original_status;
  1203. $post->post_date = $original_date;
  1204. $post->post_name = $original_name;
  1205. unset( $post->filter );
  1206. /**
  1207. * Filters the sample permalink.
  1208. *
  1209. * @since 4.4.0
  1210. *
  1211. * @param array $permalink {
  1212. * Array containing the sample permalink with placeholder for the post name, and the post name.
  1213. *
  1214. * @type string $0 The permalink with placeholder for the post name.
  1215. * @type string $1 The post name.
  1216. * }
  1217. * @param int $post_id Post ID.
  1218. * @param string $title Post title.
  1219. * @param string $name Post name (slug).
  1220. * @param WP_Post $post Post object.
  1221. */
  1222. return apply_filters( 'get_sample_permalink', $permalink, $post->ID, $title, $name, $post );
  1223. }
  1224. /**
  1225. * Returns the HTML of the sample permalink slug editor.
  1226. *
  1227. * @since 2.5.0
  1228. *
  1229. * @param int $id Post ID or post object.
  1230. * @param string $new_title Optional. New title. Default null.
  1231. * @param string $new_slug Optional. New slug. Default null.
  1232. * @return string The HTML of the sample permalink slug editor.
  1233. */
  1234. function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
  1235. $post = get_post( $id );
  1236. if ( ! $post ) {
  1237. return '';
  1238. }
  1239. list($permalink, $post_name) = get_sample_permalink( $post->ID, $new_title, $new_slug );
  1240. $view_link = false;
  1241. $preview_target = '';
  1242. if ( current_user_can( 'read_post', $post->ID ) ) {
  1243. if ( 'draft' === $post->post_status || empty( $post->post_name ) ) {
  1244. $view_link = get_preview_post_link( $post );
  1245. $preview_target = " target='wp-preview-{$post->ID}'";
  1246. } else {
  1247. if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
  1248. $view_link = get_permalink( $post );
  1249. } else {
  1250. // Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set.
  1251. $view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink );
  1252. }
  1253. }
  1254. }
  1255. // Permalinks without a post/page name placeholder don't have anything to edit.
  1256. if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) {
  1257. $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
  1258. if ( false !== $view_link ) {
  1259. $display_link = urldecode( $view_link );
  1260. $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>\n";
  1261. } else {
  1262. $return .= '<span id="sample-permalink">' . $permalink . "</span>\n";
  1263. }
  1264. // Encourage a pretty permalink setting.
  1265. if ( ! get_option( 'permalink_structure' ) && current_user_can( 'manage_options' )
  1266. && ! ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $id )
  1267. ) {
  1268. $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __( 'Change Permalinks' ) . "</a></span>\n";
  1269. }
  1270. } else {
  1271. if ( mb_strlen( $post_name ) > 34 ) {
  1272. $post_name_abridged = mb_substr( $post_name, 0, 16 ) . '&hellip;' . mb_substr( $post_name, -16 );
  1273. } else {
  1274. $post_name_abridged = $post_name;
  1275. }
  1276. $post_name_html = '<span id="editable-post-name">' . esc_html( $post_name_abridged ) . '</span>';
  1277. $display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) );
  1278. $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
  1279. $return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>\n";
  1280. $return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
  1281. $return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>\n";
  1282. $return .= '<span id="editable-post-name-full">' . esc_html( $post_name ) . "</span>\n";
  1283. }
  1284. /**
  1285. * Filters the sample permalink HTML markup.
  1286. *
  1287. * @since 2.9.0
  1288. * @since 4.4.0 Added `$post` parameter.
  1289. *
  1290. * @param string $return Sample permalink HTML markup.
  1291. * @param int $post_id Post ID.
  1292. * @param string $new_title New sample permalink title.
  1293. * @param string $new_slug New sample permalink slug.
  1294. * @param WP_Post $post Post object.
  1295. */
  1296. $return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post );
  1297. return $return;
  1298. }
  1299. /**
  1300. * Returns HTML for the post thumbnail meta box.
  1301. *
  1302. * @since 2.9.0
  1303. *
  1304. * @param int $thumbnail_id ID of the attachment used for thumbnail
  1305. * @param int|WP_Post $post Optional. The post ID or object associated with the thumbnail, defaults to global $post.
  1306. * @return string The post thumbnail HTML.
  1307. */
  1308. function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
  1309. $_wp_additional_image_sizes = wp_get_additional_image_sizes();
  1310. $post = get_post( $post );
  1311. $post_type_object = get_post_type_object( $post->post_type );
  1312. $set_thumbnail_link = '<p class="hide-if-no-js"><a href="%s" id="set-post-thumbnail"%s class="thickbox">%s</a></p>';
  1313. $upload_iframe_src = get_upload_iframe_src( 'image', $post->ID );
  1314. $content = sprintf(
  1315. $set_thumbnail_link,
  1316. esc_url( $upload_iframe_src ),
  1317. '', // Empty when there's no featured image set, `aria-describedby` attribute otherwise.
  1318. esc_html( $post_type_object->labels->set_featured_image )
  1319. );
  1320. if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
  1321. $size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : array( 266, 266 );
  1322. /**
  1323. * Filters the size used to display the post thumbnail image in the 'Featured image' meta box.
  1324. *
  1325. * Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail'
  1326. * image size is registered, which differs from the 'thumbnail' image size
  1327. * managed via the Settings > Media screen.
  1328. *
  1329. * @since 4.4.0
  1330. *
  1331. * @param string|int[] $size Requested image size. Can be any registered image size name, or
  1332. * an array of width and height values in pixels (in that order).
  1333. * @param int $thumbnail_id Post thumbnail attachment ID.
  1334. * @param WP_Post $post The post object associated with the thumbnail.
  1335. */
  1336. $size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post );
  1337. $thumbnail_html = wp_get_attachment_image( $thumbnail_id, $size );
  1338. if ( ! empty( $thumbnail_html ) ) {
  1339. $content = sprintf(
  1340. $set_thumbnail_link,
  1341. esc_url( $upload_iframe_src ),
  1342. ' aria-describedby="set-post-thumbnail-desc"',
  1343. $thumbnail_html
  1344. );
  1345. $content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __( 'Click the image to edit or update' ) . '</p>';
  1346. $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>';
  1347. }
  1348. }
  1349. $content .= '<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="' . esc_attr( $thumbnail_id ? $thumbnail_id : '-1' ) . '" />';
  1350. /**
  1351. * Filters the admin post thumbnail HTML markup to return.
  1352. *
  1353. * @since 2.9.0
  1354. * @since 3.5.0 Added the `$post_id` parameter.
  1355. * @since 4.6.0 Added the `$thumbnail_id` parameter.
  1356. *
  1357. * @param string $content Admin post thumbnail HTML markup.
  1358. * @param int $post_id Post ID.
  1359. * @param int|null $thumbnail_id Thumbnail attachment ID, or null if there isn't one.
  1360. */
  1361. return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id );
  1362. }
  1363. /**
  1364. * Check to see if the post is currently being edited by another user.
  1365. *
  1366. * @since 2.5.0
  1367. *
  1368. * @param int|WP_Post $post_id ID or object of the post to check for editing.
  1369. * @return int|false ID of the user with lock. False if the post does not exist, post is not locked,
  1370. * the user with lock does not exist, or the post is locked by current user.
  1371. */
  1372. function wp_check_post_lock( $post_id ) {
  1373. $post = get_post( $post_id );
  1374. if ( ! $post ) {
  1375. return false;
  1376. }
  1377. $lock = get_post_meta( $post->ID, '_edit_lock', true );
  1378. if ( ! $lock ) {
  1379. return false;
  1380. }
  1381. $lock = explode( ':', $lock );
  1382. $time = $lock[0];
  1383. $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
  1384. if ( ! get_userdata( $user ) ) {
  1385. return false;
  1386. }
  1387. /** This filter is documented in wp-admin/includes/ajax-actions.php */
  1388. $time_window = apply_filters( 'wp_check_post_lock_window', 150 );
  1389. if ( $time && $time > time() - $time_window && get_current_user_id() != $user ) {
  1390. return $user;
  1391. }
  1392. return false;
  1393. }
  1394. /**
  1395. * Mark the post as currently being edited by the current user
  1396. *
  1397. * @since 2.5.0
  1398. *
  1399. * @param int|WP_Post $post_id ID or object of the post being edited.
  1400. * @return array|false Array of the lock time and user ID. False if the post does not exist, or
  1401. * there is no current user.
  1402. */
  1403. function wp_set_post_lock( $post_id ) {
  1404. $post = get_post( $post_id );
  1405. if ( ! $post ) {
  1406. return false;
  1407. }
  1408. $user_id = get_current_user_id();
  1409. if ( 0 == $user_id ) {
  1410. return false;
  1411. }
  1412. $now = time();
  1413. $lock = "$now:$user_id";
  1414. update_post_meta( $post->ID, '_edit_lock', $lock );
  1415. return array( $now, $user_id );
  1416. }
  1417. /**
  1418. * Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.
  1419. *
  1420. * @since 2.8.5
  1421. */
  1422. function _admin_notice_post_locked() {
  1423. $post = get_post();
  1424. if ( ! $post ) {
  1425. return;
  1426. }
  1427. $user = null;
  1428. $user_id = wp_check_post_lock( $post->ID );
  1429. if ( $user_id ) {
  1430. $user = get_userdata( $user_id );
  1431. }
  1432. if ( $user ) {
  1433. /**
  1434. * Filters whether to show the post locked dialog.
  1435. *
  1436. * Returning false from the filter will prevent the dialog from being displayed.
  1437. *
  1438. * @since 3.6.0
  1439. *
  1440. * @param bool $display Whether to display the dialog. Default true.
  1441. * @param WP_Post $post Post object.
  1442. * @param WP_User $user The user with the lock for the post.
  1443. */
  1444. if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) ) {
  1445. return;
  1446. }
  1447. $locked = true;
  1448. } else {
  1449. $locked = false;
  1450. }
  1451. $sendback = wp_get_referer();
  1452. if ( $locked && $sendback && false === strpos( $sendback, 'post.php' ) && false === strpos( $sendback, 'post-new.php' ) ) {
  1453. $sendback_text = __( 'Go back' );
  1454. } else {
  1455. $sendback = admin_url( 'edit.php' );
  1456. if ( 'post' !== $post->post_type ) {
  1457. $sendback = add_query_arg( 'post_type', $post->post_type, $sendback );
  1458. }
  1459. $sendback_text = get_post_type_object( $post->post_type )->labels->all_items;
  1460. }
  1461. $hidden = $locked ? '' : ' hidden';
  1462. ?>
  1463. <div id="post-lock-dialog" class="notification-dialog-wrap<?php echo $hidden; ?>">
  1464. <div class="notification-dialog-background"></div>
  1465. <div class="notification-dialog">
  1466. <?php
  1467. if ( $locked ) {
  1468. $query_args = array();
  1469. if ( get_post_type_object( $post->post_type )->public ) {
  1470. if ( 'publish' === $post->post_status || $user->ID != $post->post_author ) {
  1471. // Latest content is in autosave.
  1472. $nonce = wp_create_nonce( 'post_preview_' . $post->ID );
  1473. $query_args['preview_id'] = $post->ID;
  1474. $query_args['preview_nonce'] = $nonce;
  1475. }
  1476. }
  1477. $preview_link = get_preview_post_link( $post->ID, $query_args );
  1478. /**
  1479. * Filters whether to allow the post lock to be overridden.
  1480. *
  1481. * Returning false from the filter will disable the ability
  1482. * to override the post lock.
  1483. *
  1484. * @since 3.6.0
  1485. *
  1486. * @param bool $override Whether to allow the post lock to be overridden. Default true.
  1487. * @param WP_Post $post Post object.
  1488. * @param WP_User $user The user with the lock for the post.
  1489. */
  1490. $override = apply_filters( 'override_post_lock', true, $post, $user );
  1491. $tab_last = $override ? '' : ' wp-tab-last';
  1492. ?>
  1493. <div class="post-locked-message">
  1494. <div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div>
  1495. <p class="currently-editing wp-tab-first" tabindex="0">
  1496. <?php
  1497. if ( $override ) {
  1498. /* translators: %s: User's display name. */
  1499. printf( __( '%s is currently editing this post. Do you want to take over?' ), esc_html( $user->display_name ) );
  1500. } else {
  1501. /* translators: %s: User's display name. */
  1502. printf( __( '%s is currently editing this post.' ), esc_html( $user->display_name ) );
  1503. }
  1504. ?>
  1505. </p>
  1506. <?php
  1507. /**
  1508. * Fires inside the post locked dialog before the buttons are displayed.
  1509. *
  1510. * @since 3.6.0
  1511. * @since 5.4.0 The $user parameter was added.
  1512. *
  1513. * @param WP_Post $post Post object.
  1514. * @param WP_User $user The user with the lock for the post.
  1515. */
  1516. do_action( 'post_locked_dialog', $post, $user );
  1517. ?>
  1518. <p>
  1519. <a class="button" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a>
  1520. <?php if ( $preview_link ) { ?>
  1521. <a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"><?php _e( 'Preview' ); ?></a>
  1522. <?php
  1523. }
  1524. // Allow plugins to prevent some users overriding the post lock.
  1525. if ( $override ) {
  1526. ?>
  1527. <a class="button button-primary wp-tab-last" href="<?php echo esc_url( add_query_arg( 'get-post-lock', '1', wp_nonce_url( get_edit_post_link( $post->ID, 'url' ), 'lock-post_' . $post->ID ) ) ); ?>"><?php _e( 'Take over' ); ?></a>
  1528. <?php
  1529. }
  1530. ?>
  1531. </p>
  1532. </div>
  1533. <?php
  1534. } else {
  1535. ?>
  1536. <div class="post-taken-over">
  1537. <div class="post-locked-avatar"></div>
  1538. <p class="wp-tab-first" tabindex="0">
  1539. <span class="currently-editing"></span><br />
  1540. <span class="locked-saving hidden"><img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" width="16" height="16" alt="" /> <?php _e( 'Saving revision&hellip;' ); ?></span>
  1541. <span class="locked-saved hidden"><?php _e( 'Your latest changes were saved as a revision.' ); ?></span>
  1542. </p>
  1543. <?php
  1544. /**
  1545. * Fires inside the dialog displayed when a user has lost the post lock.
  1546. *
  1547. * @since 3.6.0
  1548. *
  1549. * @param WP_Post $post Post object.
  1550. */
  1551. do_action( 'post_lock_lost_dialog', $post );
  1552. ?>
  1553. <p><a class="button button-primary wp-tab-last" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a></p>
  1554. </div>
  1555. <?php
  1556. }
  1557. ?>
  1558. </div>
  1559. </div>
  1560. <?php
  1561. }
  1562. /**
  1563. * Creates autosave data for the specified post from $_POST data.
  1564. *
  1565. * @since 2.6.0
  1566. *
  1567. * @param array|int $post_data Associative array containing the post data or int post ID.
  1568. * @return int|WP_Error The autosave revision ID. WP_Error or 0 on error.
  1569. */
  1570. function wp_create_post_autosave( $post_data ) {
  1571. if ( is_numeric( $post_data ) ) {
  1572. $post_id = $post_data;
  1573. $post_data = $_POST;
  1574. } else {
  1575. $post_id = (int) $post_data['post_ID'];
  1576. }
  1577. $post_data = _wp_translate_postdata( true, $post_data );
  1578. if ( is_wp_error( $post_data ) ) {
  1579. return $post_data;
  1580. }
  1581. $post_data = _wp_get_allowed_postdata( $post_data );
  1582. $post_author = get_current_user_id();
  1583. // Store one autosave per author. If there is already an autosave, overwrite it.
  1584. $old_autosave = wp_get_post_autosave( $post_id, $post_author );
  1585. if ( $old_autosave ) {
  1586. $new_autosave = _wp_post_revision_data( $post_data, true );
  1587. $new_autosave['ID'] = $old_autosave->ID;
  1588. $new_autosave['post_author'] = $post_author;
  1589. $post = get_post( $post_id );
  1590. // If the new autosave has the same content as the post, delete the autosave.
  1591. $autosave_is_different = false;
  1592. foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
  1593. if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) {
  1594. $autosave_is_different = true;
  1595. break;
  1596. }
  1597. }
  1598. if ( ! $autosave_is_different ) {
  1599. wp_delete_post_revision( $old_autosave->ID );
  1600. return 0;
  1601. }
  1602. /**
  1603. * Fires before an autosave is stored.
  1604. *
  1605. * @since 4.1.0
  1606. *
  1607. * @param array $new_autosave Post array - the autosave that is about to be saved.
  1608. */
  1609. do_action( 'wp_creating_autosave', $new_autosave );
  1610. return wp_update_post( $new_autosave );
  1611. }
  1612. // _wp_put_post_revision() expects unescaped.
  1613. $post_data = wp_unslash( $post_data );
  1614. // Otherwise create the new autosave as a special post revision.
  1615. return _wp_put_post_revision( $post_data, true );
  1616. }
  1617. /**
  1618. * Saves a draft or manually autosaves for the purpose of showing a post preview.
  1619. *
  1620. * @since 2.7.0
  1621. *
  1622. * @return string URL to redirect to show the preview.
  1623. */
  1624. function post_preview() {
  1625. $post_ID = (int) $_POST['post_ID'];
  1626. $_POST['ID'] = $post_ID;
  1627. $post = get_post( $post_ID );
  1628. if ( ! $post ) {
  1629. wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
  1630. }
  1631. if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  1632. wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
  1633. }
  1634. $is_autosave = false;
  1635. if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author
  1636. && ( 'draft' === $post->post_status || 'auto-draft' === $post->post_status )
  1637. ) {
  1638. $saved_post_id = edit_post();
  1639. } else {
  1640. $is_autosave = true;
  1641. if ( isset( $_POST['post_status'] ) && 'auto-draft' === $_POST['post_status'] ) {
  1642. $_POST['post_status'] = 'draft';
  1643. }
  1644. $saved_post_id = wp_create_post_autosave( $post->ID );
  1645. }
  1646. if ( is_wp_error( $saved_post_id ) ) {
  1647. wp_die( $saved_post_id->get_error_message() );
  1648. }
  1649. $query_args = array();
  1650. if ( $is_autosave && $saved_post_id ) {
  1651. $query_args['preview_id'] = $post->ID;
  1652. $query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
  1653. if ( isset( $_POST['post_format'] ) ) {
  1654. $query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
  1655. }
  1656. if ( isset( $_POST['_thumbnail_id'] ) ) {
  1657. $query_args['_thumbnail_id'] = ( (int) $_POST['_thumbnail_id'] <= 0 ) ? '-1' : (int) $_POST['_thumbnail_id'];
  1658. }
  1659. }
  1660. return get_preview_post_link( $post, $query_args );
  1661. }
  1662. /**
  1663. * Save a post submitted with XHR
  1664. *
  1665. * Intended for use with heartbeat and autosave.js
  1666. *
  1667. * @since 3.9.0
  1668. *
  1669. * @param array $post_data Associative array of the submitted post data.
  1670. * @return mixed The value 0 or WP_Error on failure. The saved post ID on success.
  1671. * The ID can be the draft post_id or the autosave revision post_id.
  1672. */
  1673. function wp_autosave( $post_data ) {
  1674. // Back-compat.
  1675. if ( ! defined( 'DOING_AUTOSAVE' ) ) {
  1676. define( 'DOING_AUTOSAVE', true );
  1677. }
  1678. $post_id = (int) $post_data['post_id'];
  1679. $post_data['ID'] = $post_id;
  1680. $post_data['post_ID'] = $post_id;
  1681. if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) ) {
  1682. return new WP_Error( 'invalid_nonce', __( 'Error while saving.' ) );
  1683. }
  1684. $post = get_post( $post_id );
  1685. if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  1686. return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to edit this item.' ) );
  1687. }
  1688. if ( 'auto-draft' === $post->post_status ) {
  1689. $post_data['post_status'] = 'draft';
  1690. }
  1691. if ( 'page' !== $post_data['post_type'] && ! empty( $post_data['catslist'] ) ) {
  1692. $post_data['post_category'] = explode( ',', $post_data['catslist'] );
  1693. }
  1694. if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author
  1695. && ( 'auto-draft' === $post->post_status || 'draft' === $post->post_status )
  1696. ) {
  1697. // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked.
  1698. return edit_post( wp_slash( $post_data ) );
  1699. } else {
  1700. // Non-drafts or other users' drafts are not overwritten.
  1701. // The autosave is stored in a special post revision for each user.
  1702. return wp_create_post_autosave( wp_slash( $post_data ) );
  1703. }
  1704. }
  1705. /**
  1706. * Redirect to previous page.
  1707. *
  1708. * @since 2.7.0
  1709. *
  1710. * @param int $post_id Optional. Post ID.
  1711. */
  1712. function redirect_post( $post_id = '' ) {
  1713. if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) ) {
  1714. $status = get_post_status( $post_id );
  1715. if ( isset( $_POST['publish'] ) ) {
  1716. switch ( $status ) {
  1717. case 'pending':
  1718. $message = 8;
  1719. break;
  1720. case 'future':
  1721. $message = 9;
  1722. break;
  1723. default:
  1724. $message = 6;
  1725. }
  1726. } else {
  1727. $message = 'draft' === $status ? 10 : 1;
  1728. }
  1729. $location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) );
  1730. } elseif ( isset( $_POST['addmeta'] ) && $_POST['addmeta'] ) {
  1731. $location = add_query_arg( 'message', 2, wp_get_referer() );
  1732. $location = explode( '#', $location );
  1733. $location = $location[0] . '#postcustom';
  1734. } elseif ( isset( $_POST['deletemeta'] ) && $_POST['deletemeta'] ) {
  1735. $location = add_query_arg( 'message', 3, wp_get_referer() );
  1736. $location = explode( '#', $location );
  1737. $location = $location[0] . '#postcustom';
  1738. } else {
  1739. $location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) );
  1740. }
  1741. /**
  1742. * Filters the post redirect destination URL.
  1743. *
  1744. * @since 2.9.0
  1745. *
  1746. * @param string $location The destination URL.
  1747. * @param int $post_id The post ID.
  1748. */
  1749. wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
  1750. exit;
  1751. }
  1752. /**
  1753. * Sanitizes POST values from a checkbox taxonomy metabox.
  1754. *
  1755. * @since 5.1.0
  1756. *
  1757. * @param string $taxonomy The taxonomy name.
  1758. * @param array $terms Raw term data from the 'tax_input' field.
  1759. * @return int[] Array of sanitized term IDs.
  1760. */
  1761. function taxonomy_meta_box_sanitize_cb_checkboxes( $taxonomy, $terms ) {
  1762. return array_map( 'intval', $terms );
  1763. }
  1764. /**
  1765. * Sanitizes POST values from an input taxonomy metabox.
  1766. *
  1767. * @since 5.1.0
  1768. *
  1769. * @param string $taxonomy The taxonomy name.
  1770. * @param array|string $terms Raw term data from the 'tax_input' field.
  1771. * @return array
  1772. */
  1773. function taxonomy_meta_box_sanitize_cb_input( $taxonomy, $terms ) {
  1774. /*
  1775. * Assume that a 'tax_input' string is a comma-separated list of term names.
  1776. * Some languages may use a character other than a comma as a delimiter, so we standardize on
  1777. * commas before parsing the list.
  1778. */
  1779. if ( ! is_array( $terms ) ) {
  1780. $comma = _x( ',', 'tag delimiter' );
  1781. if ( ',' !== $comma ) {
  1782. $terms = str_replace( $comma, ',', $terms );
  1783. }
  1784. $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
  1785. }
  1786. $clean_terms = array();
  1787. foreach ( $terms as $term ) {
  1788. // Empty terms are invalid input.
  1789. if ( empty( $term ) ) {
  1790. continue;
  1791. }
  1792. $_term = get_terms(
  1793. array(
  1794. 'taxonomy' => $taxonomy,
  1795. 'name' => $term,
  1796. 'fields' => 'ids',
  1797. 'hide_empty' => false,
  1798. )
  1799. );
  1800. if ( ! empty( $_term ) ) {
  1801. $clean_terms[] = (int) $_term[0];
  1802. } else {
  1803. // No existing term was found, so pass the string. A new term will be created.
  1804. $clean_terms[] = $term;
  1805. }
  1806. }
  1807. return $clean_terms;
  1808. }
  1809. /**
  1810. * Return whether the post can be edited in the block editor.
  1811. *
  1812. * @since 5.0.0
  1813. *
  1814. * @param int|WP_Post $post Post ID or WP_Post object.
  1815. * @return bool Whether the post can be edited in the block editor.
  1816. */
  1817. function use_block_editor_for_post( $post ) {
  1818. $post = get_post( $post );
  1819. if ( ! $post ) {
  1820. return false;
  1821. }
  1822. // We're in the meta box loader, so don't use the block editor.
  1823. if ( isset( $_GET['meta-box-loader'] ) ) {
  1824. check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' );
  1825. return false;
  1826. }
  1827. $use_block_editor = use_block_editor_for_post_type( $post->post_type );
  1828. /**
  1829. * Filters whether a post is able to be edited in the block editor.
  1830. *
  1831. * @since 5.0.0
  1832. *
  1833. * @param bool $use_block_editor Whether the post can be edited or not.
  1834. * @param WP_Post $post The post being checked.
  1835. */
  1836. return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
  1837. }
  1838. /**
  1839. * Return whether a post type is compatible with the block editor.
  1840. *
  1841. * The block editor depends on the REST API, and if the post type is not shown in the
  1842. * REST API, then it won't work with the block editor.
  1843. *
  1844. * @since 5.0.0
  1845. *
  1846. * @param string $post_type The post type.
  1847. * @return bool Whether the post type can be edited with the block editor.
  1848. */
  1849. function use_block_editor_for_post_type( $post_type ) {
  1850. if ( ! post_type_exists( $post_type ) ) {
  1851. return false;
  1852. }
  1853. if ( ! post_type_supports( $post_type, 'editor' ) ) {
  1854. return false;
  1855. }
  1856. $post_type_object = get_post_type_object( $post_type );
  1857. if ( $post_type_object && ! $post_type_object->show_in_rest ) {
  1858. return false;
  1859. }
  1860. /**
  1861. * Filters whether a post is able to be edited in the block editor.
  1862. *
  1863. * @since 5.0.0
  1864. *
  1865. * @param bool $use_block_editor Whether the post type can be edited or not. Default true.
  1866. * @param string $post_type The post type being checked.
  1867. */
  1868. return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
  1869. }
  1870. /**
  1871. * Prepares server-registered blocks for the block editor.
  1872. *
  1873. * Returns an associative array of registered block data keyed by block name. Data includes properties
  1874. * of a block relevant for client registration.
  1875. *
  1876. * @since 5.0.0
  1877. *
  1878. * @return array An associative array of registered block data.
  1879. */
  1880. function get_block_editor_server_block_settings() {
  1881. $block_registry = WP_Block_Type_Registry::get_instance();
  1882. $blocks = array();
  1883. $fields_to_pick = array(
  1884. 'api_version' => 'apiVersion',
  1885. 'title' => 'title',
  1886. 'description' => 'description',
  1887. 'icon' => 'icon',
  1888. 'attributes' => 'attributes',
  1889. 'provides_context' => 'providesContext',
  1890. 'uses_context' => 'usesContext',
  1891. 'supports' => 'supports',
  1892. 'category' => 'category',
  1893. 'styles' => 'styles',
  1894. 'textdomain' => 'textdomain',
  1895. 'parent' => 'parent',
  1896. 'keywords' => 'keywords',
  1897. 'example' => 'example',
  1898. 'variations' => 'variations',
  1899. );
  1900. foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
  1901. foreach ( $fields_to_pick as $field => $key ) {
  1902. if ( ! isset( $block_type->{ $field } ) ) {
  1903. continue;
  1904. }
  1905. if ( ! isset( $blocks[ $block_name ] ) ) {
  1906. $blocks[ $block_name ] = array();
  1907. }
  1908. $blocks[ $block_name ][ $key ] = $block_type->{ $field };
  1909. }
  1910. }
  1911. return $blocks;
  1912. }
  1913. /**
  1914. * Renders the meta boxes forms.
  1915. *
  1916. * @since 5.0.0
  1917. */
  1918. function the_block_editor_meta_boxes() {
  1919. global $post, $current_screen, $wp_meta_boxes;
  1920. // Handle meta box state.
  1921. $_original_meta_boxes = $wp_meta_boxes;
  1922. /**
  1923. * Fires right before the meta boxes are rendered.
  1924. *
  1925. * This allows for the filtering of meta box data, that should already be
  1926. * present by this point. Do not use as a means of adding meta box data.
  1927. *
  1928. * @since 5.0.0
  1929. *
  1930. * @param array $wp_meta_boxes Global meta box state.
  1931. */
  1932. $wp_meta_boxes = apply_filters( 'filter_block_editor_meta_boxes', $wp_meta_boxes );
  1933. $locations = array( 'side', 'normal', 'advanced' );
  1934. $priorities = array( 'high', 'sorted', 'core', 'default', 'low' );
  1935. // Render meta boxes.
  1936. ?>
  1937. <form class="metabox-base-form">
  1938. <?php the_block_editor_meta_box_post_form_hidden_fields( $post ); ?>
  1939. </form>
  1940. <form id="toggle-custom-fields-form" method="post" action="<?php echo esc_url( admin_url( 'post.php' ) ); ?>">
  1941. <?php wp_nonce_field( 'toggle-custom-fields', 'toggle-custom-fields-nonce' ); ?>
  1942. <input type="hidden" name="action" value="toggle-custom-fields" />
  1943. </form>
  1944. <?php foreach ( $locations as $location ) : ?>
  1945. <form class="metabox-location-<?php echo esc_attr( $location ); ?>" onsubmit="return false;">
  1946. <div id="poststuff" class="sidebar-open">
  1947. <div id="postbox-container-2" class="postbox-container">
  1948. <?php
  1949. do_meta_boxes(
  1950. $current_screen,
  1951. $location,
  1952. $post
  1953. );
  1954. ?>
  1955. </div>
  1956. </div>
  1957. </form>
  1958. <?php endforeach; ?>
  1959. <?php
  1960. $meta_boxes_per_location = array();
  1961. foreach ( $locations as $location ) {
  1962. $meta_boxes_per_location[ $location ] = array();
  1963. if ( ! isset( $wp_meta_boxes[ $current_screen->id ][ $location ] ) ) {
  1964. continue;
  1965. }
  1966. foreach ( $priorities as $priority ) {
  1967. if ( ! isset( $wp_meta_boxes[ $current_screen->id ][ $location ][ $priority ] ) ) {
  1968. continue;
  1969. }
  1970. $meta_boxes = (array) $wp_meta_boxes[ $current_screen->id ][ $location ][ $priority ];
  1971. foreach ( $meta_boxes as $meta_box ) {
  1972. if ( false == $meta_box || ! $meta_box['title'] ) {
  1973. continue;
  1974. }
  1975. // If a meta box is just here for back compat, don't show it in the block editor.
  1976. if ( isset( $meta_box['args']['__back_compat_meta_box'] ) && $meta_box['args']['__back_compat_meta_box'] ) {
  1977. continue;
  1978. }
  1979. $meta_boxes_per_location[ $location ][] = array(
  1980. 'id' => $meta_box['id'],
  1981. 'title' => $meta_box['title'],
  1982. );
  1983. }
  1984. }
  1985. }
  1986. /**
  1987. * Sadly we probably can not add this data directly into editor settings.
  1988. *
  1989. * Some meta boxes need admin_head to fire for meta box registry.
  1990. * admin_head fires after admin_enqueue_scripts, which is where we create our
  1991. * editor instance.
  1992. */
  1993. $script = 'window._wpLoadBlockEditor.then( function() {
  1994. wp.data.dispatch( \'core/edit-post\' ).setAvailableMetaBoxesPerLocation( ' . wp_json_encode( $meta_boxes_per_location ) . ' );
  1995. } );';
  1996. wp_add_inline_script( 'wp-edit-post', $script );
  1997. /**
  1998. * When `wp-edit-post` is output in the `<head>`, the inline script needs to be manually printed. Otherwise,
  1999. * meta boxes will not display because inline scripts for `wp-edit-post` will not be printed again after this point.
  2000. */
  2001. if ( wp_script_is( 'wp-edit-post', 'done' ) ) {
  2002. printf( "<script type='text/javascript'>\n%s\n</script>\n", trim( $script ) );
  2003. }
  2004. /**
  2005. * If the 'postcustom' meta box is enabled, then we need to perform some
  2006. * extra initialization on it.
  2007. */
  2008. $enable_custom_fields = (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true );
  2009. if ( $enable_custom_fields ) {
  2010. $script = "( function( $ ) {
  2011. if ( $('#postcustom').length ) {
  2012. $( '#the-list' ).wpList( {
  2013. addBefore: function( s ) {
  2014. s.data += '&post_id=$post->ID';
  2015. return s;
  2016. },
  2017. addAfter: function() {
  2018. $('table#list-table').show();
  2019. }
  2020. });
  2021. }
  2022. } )( jQuery );";
  2023. wp_enqueue_script( 'wp-lists' );
  2024. wp_add_inline_script( 'wp-lists', $script );
  2025. }
  2026. // Reset meta box data.
  2027. $wp_meta_boxes = $_original_meta_boxes;
  2028. }
  2029. /**
  2030. * Renders the hidden form required for the meta boxes form.
  2031. *
  2032. * @since 5.0.0
  2033. *
  2034. * @param WP_Post $post Current post object.
  2035. */
  2036. function the_block_editor_meta_box_post_form_hidden_fields( $post ) {
  2037. $form_extra = '';
  2038. if ( 'auto-draft' === $post->post_status ) {
  2039. $form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />";
  2040. }
  2041. $form_action = 'editpost';
  2042. $nonce_action = 'update-post_' . $post->ID;
  2043. $form_extra .= "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_attr( $post->ID ) . "' />";
  2044. $referer = wp_get_referer();
  2045. $current_user = wp_get_current_user();
  2046. $user_id = $current_user->ID;
  2047. wp_nonce_field( $nonce_action );
  2048. /*
  2049. * Some meta boxes hook into these actions to add hidden input fields in the classic post form. For backwards
  2050. * compatibility, we can capture the output from these actions, and extract the hidden input fields.
  2051. */
  2052. ob_start();
  2053. /** This filter is documented in wp-admin/edit-form-advanced.php */
  2054. do_action( 'edit_form_after_title', $post );
  2055. /** This filter is documented in wp-admin/edit-form-advanced.php */
  2056. do_action( 'edit_form_advanced', $post );
  2057. $classic_output = ob_get_clean();
  2058. $classic_elements = wp_html_split( $classic_output );
  2059. $hidden_inputs = '';
  2060. foreach ( $classic_elements as $element ) {
  2061. if ( 0 !== strpos( $element, '<input ' ) ) {
  2062. continue;
  2063. }
  2064. if ( preg_match( '/\stype=[\'"]hidden[\'"]\s/', $element ) ) {
  2065. echo $element;
  2066. }
  2067. }
  2068. ?>
  2069. <input type="hidden" id="user-id" name="user_ID" value="<?php echo (int) $user_id; ?>" />
  2070. <input type="hidden" id="hiddenaction" name="action" value="<?php echo esc_attr( $form_action ); ?>" />
  2071. <input type="hidden" id="originalaction" name="originalaction" value="<?php echo esc_attr( $form_action ); ?>" />
  2072. <input type="hidden" id="post_type" name="post_type" value="<?php echo esc_attr( $post->post_type ); ?>" />
  2073. <input type="hidden" id="original_post_status" name="original_post_status" value="<?php echo esc_attr( $post->post_status ); ?>" />
  2074. <input type="hidden" id="referredby" name="referredby" value="<?php echo $referer ? esc_url( $referer ) : ''; ?>" />
  2075. <?php
  2076. if ( 'draft' !== get_post_status( $post ) ) {
  2077. wp_original_referer_field( true, 'previous' );
  2078. }
  2079. echo $form_extra;
  2080. wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
  2081. wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
  2082. // Permalink title nonce.
  2083. wp_nonce_field( 'samplepermalink', 'samplepermalinknonce', false );
  2084. /**
  2085. * Add hidden input fields to the meta box save form.
  2086. *
  2087. * Hook into this action to print `<input type="hidden" ... />` fields, which will be POSTed back to
  2088. * the server when meta boxes are saved.
  2089. *
  2090. * @since 5.0.0
  2091. *
  2092. * @param WP_Post $post The post that is being edited.
  2093. */
  2094. do_action( 'block_editor_meta_box_hidden_fields', $post );
  2095. }