post.php 76 KB

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