post.php 73 KB

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