暫無描述

post.php 76KB

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