post.php 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. <?php
  2. /**
  3. * WordPress Post Administration API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Rename $_POST data from form names to DB post columns.
  10. *
  11. * Manipulates $_POST directly.
  12. *
  13. * @since 2.6.0
  14. *
  15. * @param bool $update Are we updating a pre-existing post?
  16. * @param array $post_data Array of post data. Defaults to the contents of $_POST.
  17. * @return object|bool WP_Error on failure, true on success.
  18. */
  19. function _wp_translate_postdata( $update = false, $post_data = null ) {
  20. if ( empty($post_data) )
  21. $post_data = &$_POST;
  22. if ( $update )
  23. $post_data['ID'] = (int) $post_data['post_ID'];
  24. $ptype = get_post_type_object( $post_data['post_type'] );
  25. if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) {
  26. if ( 'page' == $post_data['post_type'] )
  27. return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );
  28. else
  29. return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );
  30. } elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) {
  31. if ( 'page' == $post_data['post_type'] )
  32. return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );
  33. else
  34. return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );
  35. }
  36. if ( isset( $post_data['content'] ) )
  37. $post_data['post_content'] = $post_data['content'];
  38. if ( isset( $post_data['excerpt'] ) )
  39. $post_data['post_excerpt'] = $post_data['excerpt'];
  40. if ( isset( $post_data['parent_id'] ) )
  41. $post_data['post_parent'] = (int) $post_data['parent_id'];
  42. if ( isset($post_data['trackback_url']) )
  43. $post_data['to_ping'] = $post_data['trackback_url'];
  44. $post_data['user_ID'] = get_current_user_id();
  45. if (!empty ( $post_data['post_author_override'] ) ) {
  46. $post_data['post_author'] = (int) $post_data['post_author_override'];
  47. } else {
  48. if (!empty ( $post_data['post_author'] ) ) {
  49. $post_data['post_author'] = (int) $post_data['post_author'];
  50. } else {
  51. $post_data['post_author'] = (int) $post_data['user_ID'];
  52. }
  53. }
  54. if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] )
  55. && ! current_user_can( $ptype->cap->edit_others_posts ) ) {
  56. if ( $update ) {
  57. if ( 'page' == $post_data['post_type'] )
  58. return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );
  59. else
  60. return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );
  61. } else {
  62. if ( 'page' == $post_data['post_type'] )
  63. return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );
  64. else
  65. return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );
  66. }
  67. }
  68. if ( ! empty( $post_data['post_status'] ) ) {
  69. $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
  70. // No longer an auto-draft
  71. if ( 'auto-draft' === $post_data['post_status'] ) {
  72. $post_data['post_status'] = 'draft';
  73. }
  74. if ( ! get_post_status_object( $post_data['post_status'] ) ) {
  75. unset( $post_data['post_status'] );
  76. }
  77. }
  78. // What to do based on which button they pressed
  79. if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
  80. $post_data['post_status'] = 'draft';
  81. if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] )
  82. $post_data['post_status'] = 'private';
  83. if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( !isset($post_data['post_status']) || $post_data['post_status'] != 'private' ) )
  84. $post_data['post_status'] = 'publish';
  85. if ( isset($post_data['advanced']) && '' != $post_data['advanced'] )
  86. $post_data['post_status'] = 'draft';
  87. if ( isset($post_data['pending']) && '' != $post_data['pending'] )
  88. $post_data['post_status'] = 'pending';
  89. if ( isset( $post_data['ID'] ) )
  90. $post_id = $post_data['ID'];
  91. else
  92. $post_id = false;
  93. $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
  94. if ( isset( $post_data['post_status'] ) && 'private' == $post_data['post_status'] && ! current_user_can( $ptype->cap->publish_posts ) ) {
  95. $post_data['post_status'] = $previous_status ? $previous_status : 'pending';
  96. }
  97. $published_statuses = array( 'publish', 'future' );
  98. // Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
  99. // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
  100. if ( isset($post_data['post_status']) && (in_array( $post_data['post_status'], $published_statuses ) && !current_user_can( $ptype->cap->publish_posts )) )
  101. if ( ! in_array( $previous_status, $published_statuses ) || !current_user_can( 'edit_post', $post_id ) )
  102. $post_data['post_status'] = 'pending';
  103. if ( ! isset( $post_data['post_status'] ) ) {
  104. $post_data['post_status'] = 'auto-draft' === $previous_status ? 'draft' : $previous_status;
  105. }
  106. if ( isset( $post_data['post_password'] ) && ! current_user_can( $ptype->cap->publish_posts ) ) {
  107. unset( $post_data['post_password'] );
  108. }
  109. if (!isset( $post_data['comment_status'] ))
  110. $post_data['comment_status'] = 'closed';
  111. if (!isset( $post_data['ping_status'] ))
  112. $post_data['ping_status'] = 'closed';
  113. foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
  114. if ( !empty( $post_data['hidden_' . $timeunit] ) && $post_data['hidden_' . $timeunit] != $post_data[$timeunit] ) {
  115. $post_data['edit_date'] = '1';
  116. break;
  117. }
  118. }
  119. if ( !empty( $post_data['edit_date'] ) ) {
  120. $aa = $post_data['aa'];
  121. $mm = $post_data['mm'];
  122. $jj = $post_data['jj'];
  123. $hh = $post_data['hh'];
  124. $mn = $post_data['mn'];
  125. $ss = $post_data['ss'];
  126. $aa = ($aa <= 0 ) ? date('Y') : $aa;
  127. $mm = ($mm <= 0 ) ? date('n') : $mm;
  128. $jj = ($jj > 31 ) ? 31 : $jj;
  129. $jj = ($jj <= 0 ) ? date('j') : $jj;
  130. $hh = ($hh > 23 ) ? $hh -24 : $hh;
  131. $mn = ($mn > 59 ) ? $mn -60 : $mn;
  132. $ss = ($ss > 59 ) ? $ss -60 : $ss;
  133. $post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
  134. $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
  135. if ( !$valid_date ) {
  136. return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
  137. }
  138. $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
  139. }
  140. if ( isset( $post_data['post_category'] ) ) {
  141. $category_object = get_taxonomy( 'category' );
  142. if ( ! current_user_can( $category_object->cap->assign_terms ) ) {
  143. unset( $post_data['post_category'] );
  144. }
  145. }
  146. return $post_data;
  147. }
  148. /**
  149. * Returns only allowed post data fields
  150. *
  151. * @since 4.9.9
  152. *
  153. * @param array $post_data Array of post data. Defaults to the contents of $_POST.
  154. * @return object|bool WP_Error on failure, true on success.
  155. */
  156. function _wp_get_allowed_postdata( $post_data = null ) {
  157. if ( empty( $post_data ) ) {
  158. $post_data = $_POST;
  159. }
  160. // Pass through errors
  161. if ( is_wp_error( $post_data ) ) {
  162. return $post_data;
  163. }
  164. return array_diff_key( $post_data, array_flip( array( 'meta_input', 'file', 'guid' ) ) );
  165. }
  166. /**
  167. * Update an existing post with values provided in $_POST.
  168. *
  169. * @since 1.5.0
  170. *
  171. * @global wpdb $wpdb WordPress database abstraction object.
  172. *
  173. * @param array $post_data Optional.
  174. * @return int Post ID.
  175. */
  176. function edit_post( $post_data = null ) {
  177. global $wpdb;
  178. if ( empty($post_data) )
  179. $post_data = &$_POST;
  180. // Clear out any data in internal vars.
  181. unset( $post_data['filter'] );
  182. $post_ID = (int) $post_data['post_ID'];
  183. $post = get_post( $post_ID );
  184. $post_data['post_type'] = $post->post_type;
  185. $post_data['post_mime_type'] = $post->post_mime_type;
  186. if ( ! empty( $post_data['post_status'] ) ) {
  187. $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
  188. if ( 'inherit' == $post_data['post_status'] ) {
  189. unset( $post_data['post_status'] );
  190. }
  191. }
  192. $ptype = get_post_type_object($post_data['post_type']);
  193. if ( !current_user_can( 'edit_post', $post_ID ) ) {
  194. if ( 'page' == $post_data['post_type'] )
  195. wp_die( __('Sorry, you are not allowed to edit this page.' ));
  196. else
  197. wp_die( __('Sorry, you are not allowed to edit this post.' ));
  198. }
  199. if ( post_type_supports( $ptype->name, 'revisions' ) ) {
  200. $revisions = wp_get_post_revisions( $post_ID, array( 'order' => 'ASC', 'posts_per_page' => 1 ) );
  201. $revision = current( $revisions );
  202. // Check if the revisions have been upgraded
  203. if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 )
  204. _wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_ID ) );
  205. }
  206. if ( isset($post_data['visibility']) ) {
  207. switch ( $post_data['visibility'] ) {
  208. case 'public' :
  209. $post_data['post_password'] = '';
  210. break;
  211. case 'password' :
  212. unset( $post_data['sticky'] );
  213. break;
  214. case 'private' :
  215. $post_data['post_status'] = 'private';
  216. $post_data['post_password'] = '';
  217. unset( $post_data['sticky'] );
  218. break;
  219. }
  220. }
  221. $post_data = _wp_translate_postdata( true, $post_data );
  222. if ( is_wp_error($post_data) )
  223. wp_die( $post_data->get_error_message() );
  224. $translated = _wp_get_allowed_postdata( $post_data );
  225. // Post Formats
  226. if ( isset( $post_data['post_format'] ) )
  227. set_post_format( $post_ID, $post_data['post_format'] );
  228. $format_meta_urls = array( 'url', 'link_url', 'quote_source_url' );
  229. foreach ( $format_meta_urls as $format_meta_url ) {
  230. $keyed = '_format_' . $format_meta_url;
  231. if ( isset( $post_data[ $keyed ] ) )
  232. update_post_meta( $post_ID, $keyed, wp_slash( esc_url_raw( wp_unslash( $post_data[ $keyed ] ) ) ) );
  233. }
  234. $format_keys = array( 'quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed' );
  235. foreach ( $format_keys as $key ) {
  236. $keyed = '_format_' . $key;
  237. if ( isset( $post_data[ $keyed ] ) ) {
  238. if ( current_user_can( 'unfiltered_html' ) )
  239. update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] );
  240. else
  241. update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
  242. }
  243. }
  244. if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) {
  245. $id3data = wp_get_attachment_metadata( $post_ID );
  246. if ( ! is_array( $id3data ) ) {
  247. $id3data = array();
  248. }
  249. foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) {
  250. if ( isset( $post_data[ 'id3_' . $key ] ) ) {
  251. $id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) );
  252. }
  253. }
  254. wp_update_attachment_metadata( $post_ID, $id3data );
  255. }
  256. // Meta Stuff
  257. if ( isset($post_data['meta']) && $post_data['meta'] ) {
  258. foreach ( $post_data['meta'] as $key => $value ) {
  259. if ( !$meta = get_post_meta_by_id( $key ) )
  260. continue;
  261. if ( $meta->post_id != $post_ID )
  262. continue;
  263. if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $meta->meta_key ) )
  264. continue;
  265. if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) )
  266. continue;
  267. update_meta( $key, $value['key'], $value['value'] );
  268. }
  269. }
  270. if ( isset($post_data['deletemeta']) && $post_data['deletemeta'] ) {
  271. foreach ( $post_data['deletemeta'] as $key => $value ) {
  272. if ( !$meta = get_post_meta_by_id( $key ) )
  273. continue;
  274. if ( $meta->post_id != $post_ID )
  275. continue;
  276. if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) )
  277. continue;
  278. delete_meta( $key );
  279. }
  280. }
  281. // Attachment stuff
  282. if ( 'attachment' == $post_data['post_type'] ) {
  283. if ( isset( $post_data[ '_wp_attachment_image_alt' ] ) ) {
  284. $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] );
  285. if ( $image_alt != get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) ) {
  286. $image_alt = wp_strip_all_tags( $image_alt, true );
  287. // update_meta expects slashed.
  288. update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
  289. }
  290. }
  291. $attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array();
  292. /** This filter is documented in wp-admin/includes/media.php */
  293. $translated = apply_filters( 'attachment_fields_to_save', $translated, $attachment_data );
  294. }
  295. // Convert taxonomy input to term IDs, to avoid ambiguity.
  296. if ( isset( $post_data['tax_input'] ) ) {
  297. foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) {
  298. // Hierarchical taxonomy data is already sent as term IDs, so no conversion is necessary.
  299. if ( is_taxonomy_hierarchical( $taxonomy ) ) {
  300. continue;
  301. }
  302. /*
  303. * Assume that a 'tax_input' string is a comma-separated list of term names.
  304. * Some languages may use a character other than a comma as a delimiter, so we standardize on
  305. * commas before parsing the list.
  306. */
  307. if ( ! is_array( $terms ) ) {
  308. $comma = _x( ',', 'tag delimiter' );
  309. if ( ',' !== $comma ) {
  310. $terms = str_replace( $comma, ',', $terms );
  311. }
  312. $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
  313. }
  314. $clean_terms = array();
  315. foreach ( $terms as $term ) {
  316. // Empty terms are invalid input.
  317. if ( empty( $term ) ) {
  318. continue;
  319. }
  320. $_term = get_terms( $taxonomy, array(
  321. 'name' => $term,
  322. 'fields' => 'ids',
  323. 'hide_empty' => false,
  324. ) );
  325. if ( ! empty( $_term ) ) {
  326. $clean_terms[] = intval( $_term[0] );
  327. } else {
  328. // No existing term was found, so pass the string. A new term will be created.
  329. $clean_terms[] = $term;
  330. }
  331. }
  332. $translated['tax_input'][ $taxonomy ] = $clean_terms;
  333. }
  334. }
  335. add_meta( $post_ID );
  336. update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
  337. $success = wp_update_post( $translated );
  338. // If the save failed, see if we can sanity check the main fields and try again
  339. if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) {
  340. $fields = array( 'post_title', 'post_content', 'post_excerpt' );
  341. foreach ( $fields as $field ) {
  342. if ( isset( $translated[ $field ] ) ) {
  343. $translated[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $translated[ $field ] );
  344. }
  345. }
  346. wp_update_post( $translated );
  347. }
  348. // Now that we have an ID we can fix any attachment anchor hrefs
  349. _fix_attachment_links( $post_ID );
  350. wp_set_post_lock( $post_ID );
  351. if ( current_user_can( $ptype->cap->edit_others_posts ) && current_user_can( $ptype->cap->publish_posts ) ) {
  352. if ( ! empty( $post_data['sticky'] ) )
  353. stick_post( $post_ID );
  354. else
  355. unstick_post( $post_ID );
  356. }
  357. return $post_ID;
  358. }
  359. /**
  360. * Process the post data for the bulk editing of posts.
  361. *
  362. * Updates all bulk edited posts/pages, adding (but not removing) tags and
  363. * categories. Skips pages when they would be their own parent or child.
  364. *
  365. * @since 2.7.0
  366. *
  367. * @global wpdb $wpdb WordPress database abstraction object.
  368. *
  369. * @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal.
  370. * @return array
  371. */
  372. function bulk_edit_posts( $post_data = null ) {
  373. global $wpdb;
  374. if ( empty($post_data) )
  375. $post_data = &$_POST;
  376. if ( isset($post_data['post_type']) )
  377. $ptype = get_post_type_object($post_data['post_type']);
  378. else
  379. $ptype = get_post_type_object('post');
  380. if ( !current_user_can( $ptype->cap->edit_posts ) ) {
  381. if ( 'page' == $ptype->name )
  382. wp_die( __('Sorry, you are not allowed to edit pages.'));
  383. else
  384. wp_die( __('Sorry, you are not allowed to edit posts.'));
  385. }
  386. if ( -1 == $post_data['_status'] ) {
  387. $post_data['post_status'] = null;
  388. unset($post_data['post_status']);
  389. } else {
  390. $post_data['post_status'] = $post_data['_status'];
  391. }
  392. unset($post_data['_status']);
  393. if ( ! empty( $post_data['post_status'] ) ) {
  394. $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
  395. if ( 'inherit' == $post_data['post_status'] ) {
  396. unset( $post_data['post_status'] );
  397. }
  398. }
  399. $post_IDs = array_map( 'intval', (array) $post_data['post'] );
  400. $reset = array(
  401. 'post_author', 'post_status', 'post_password',
  402. 'post_parent', 'page_template', 'comment_status',
  403. 'ping_status', 'keep_private', 'tax_input',
  404. 'post_category', 'sticky', 'post_format',
  405. );
  406. foreach ( $reset as $field ) {
  407. if ( isset($post_data[$field]) && ( '' == $post_data[$field] || -1 == $post_data[$field] ) )
  408. unset($post_data[$field]);
  409. }
  410. if ( isset($post_data['post_category']) ) {
  411. if ( is_array($post_data['post_category']) && ! empty($post_data['post_category']) )
  412. $new_cats = array_map( 'absint', $post_data['post_category'] );
  413. else
  414. unset($post_data['post_category']);
  415. }
  416. $tax_input = array();
  417. if ( isset($post_data['tax_input'])) {
  418. foreach ( $post_data['tax_input'] as $tax_name => $terms ) {
  419. if ( empty($terms) )
  420. continue;
  421. if ( is_taxonomy_hierarchical( $tax_name ) ) {
  422. $tax_input[ $tax_name ] = array_map( 'absint', $terms );
  423. } else {
  424. $comma = _x( ',', 'tag delimiter' );
  425. if ( ',' !== $comma )
  426. $terms = str_replace( $comma, ',', $terms );
  427. $tax_input[ $tax_name ] = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
  428. }
  429. }
  430. }
  431. if ( isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent']) ) {
  432. $pages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'");
  433. $children = array();
  434. for ( $i = 0; $i < 50 && $parent > 0; $i++ ) {
  435. $children[] = $parent;
  436. foreach ( $pages as $page ) {
  437. if ( $page->ID == $parent ) {
  438. $parent = $page->post_parent;
  439. break;
  440. }
  441. }
  442. }
  443. }
  444. $updated = $skipped = $locked = array();
  445. $shared_post_data = $post_data;
  446. foreach ( $post_IDs as $post_ID ) {
  447. // Start with fresh post data with each iteration.
  448. $post_data = $shared_post_data;
  449. $post_type_object = get_post_type_object( get_post_type( $post_ID ) );
  450. if ( !isset( $post_type_object ) || ( isset($children) && in_array($post_ID, $children) ) || !current_user_can( 'edit_post', $post_ID ) ) {
  451. $skipped[] = $post_ID;
  452. continue;
  453. }
  454. if ( wp_check_post_lock( $post_ID ) ) {
  455. $locked[] = $post_ID;
  456. continue;
  457. }
  458. $post = get_post( $post_ID );
  459. $tax_names = get_object_taxonomies( $post );
  460. foreach ( $tax_names as $tax_name ) {
  461. $taxonomy_obj = get_taxonomy($tax_name);
  462. if ( isset( $tax_input[$tax_name]) && current_user_can( $taxonomy_obj->cap->assign_terms ) )
  463. $new_terms = $tax_input[$tax_name];
  464. else
  465. $new_terms = array();
  466. if ( $taxonomy_obj->hierarchical )
  467. $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'ids') );
  468. else
  469. $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'names') );
  470. $post_data['tax_input'][$tax_name] = array_merge( $current_terms, $new_terms );
  471. }
  472. if ( isset($new_cats) && in_array( 'category', $tax_names ) ) {
  473. $cats = (array) wp_get_post_categories($post_ID);
  474. $post_data['post_category'] = array_unique( array_merge($cats, $new_cats) );
  475. unset( $post_data['tax_input']['category'] );
  476. }
  477. $post_data['post_ID'] = $post_ID;
  478. $post_data['post_type'] = $post->post_type;
  479. $post_data['post_mime_type'] = $post->post_mime_type;
  480. foreach ( array( 'comment_status', 'ping_status', 'post_author' ) as $field ) {
  481. if ( ! isset( $post_data[ $field ] ) ) {
  482. $post_data[ $field ] = $post->$field;
  483. }
  484. }
  485. $post_data = _wp_translate_postdata( true, $post_data );
  486. if ( is_wp_error( $post_data ) ) {
  487. $skipped[] = $post_ID;
  488. continue;
  489. }
  490. $post_data = _wp_get_allowed_postdata( $post_data );
  491. if ( isset( $shared_post_data['post_format'] ) ) {
  492. set_post_format( $post_ID, $shared_post_data['post_format'] );
  493. unset( $post_data['tax_input']['post_format'] );
  494. }
  495. $updated[] = wp_update_post( $post_data );
  496. if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
  497. if ( 'sticky' == $post_data['sticky'] )
  498. stick_post( $post_ID );
  499. else
  500. unstick_post( $post_ID );
  501. }
  502. }
  503. return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked );
  504. }
  505. /**
  506. * Default post information to use when populating the "Write Post" form.
  507. *
  508. * @since 2.0.0
  509. *
  510. * @param string $post_type Optional. A post type string. Default 'post'.
  511. * @param bool $create_in_db Optional. Whether to insert the post into database. Default false.
  512. * @return WP_Post Post object containing all the default post data as attributes
  513. */
  514. function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
  515. $post_title = '';
  516. if ( !empty( $_REQUEST['post_title'] ) )
  517. $post_title = esc_html( wp_unslash( $_REQUEST['post_title'] ));
  518. $post_content = '';
  519. if ( !empty( $_REQUEST['content'] ) )
  520. $post_content = esc_html( wp_unslash( $_REQUEST['content'] ));
  521. $post_excerpt = '';
  522. if ( !empty( $_REQUEST['excerpt'] ) )
  523. $post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] ));
  524. if ( $create_in_db ) {
  525. $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
  526. $post = get_post( $post_id );
  527. if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
  528. set_post_format( $post, get_option( 'default_post_format' ) );
  529. } else {
  530. $post = new stdClass;
  531. $post->ID = 0;
  532. $post->post_author = '';
  533. $post->post_date = '';
  534. $post->post_date_gmt = '';
  535. $post->post_password = '';
  536. $post->post_name = '';
  537. $post->post_type = $post_type;
  538. $post->post_status = 'draft';
  539. $post->to_ping = '';
  540. $post->pinged = '';
  541. $post->comment_status = get_default_comment_status( $post_type );
  542. $post->ping_status = get_default_comment_status( $post_type, 'pingback' );
  543. $post->post_pingback = get_option( 'default_pingback_flag' );
  544. $post->post_category = get_option( 'default_category' );
  545. $post->page_template = 'default';
  546. $post->post_parent = 0;
  547. $post->menu_order = 0;
  548. $post = new WP_Post( $post );
  549. }
  550. /**
  551. * Filters the default post content initially used in the "Write Post" form.
  552. *
  553. * @since 1.5.0
  554. *
  555. * @param string $post_content Default post content.
  556. * @param WP_Post $post Post object.
  557. */
  558. $post->post_content = apply_filters( 'default_content', $post_content, $post );
  559. /**
  560. * Filters the default post title initially used in the "Write Post" form.
  561. *
  562. * @since 1.5.0
  563. *
  564. * @param string $post_title Default post title.
  565. * @param WP_Post $post Post object.
  566. */
  567. $post->post_title = apply_filters( 'default_title', $post_title, $post );
  568. /**
  569. * Filters the default post excerpt initially used in the "Write Post" form.
  570. *
  571. * @since 1.5.0
  572. *
  573. * @param string $post_excerpt Default post excerpt.
  574. * @param WP_Post $post Post object.
  575. */
  576. $post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post );
  577. return $post;
  578. }
  579. /**
  580. * Determine if a post exists based on title, content, and date
  581. *
  582. * @since 2.0.0
  583. *
  584. * @global wpdb $wpdb WordPress database abstraction object.
  585. *
  586. * @param string $title Post title
  587. * @param string $content Optional post content
  588. * @param string $date Optional post date
  589. * @return int Post ID if post exists, 0 otherwise.
  590. */
  591. function post_exists($title, $content = '', $date = '') {
  592. global $wpdb;
  593. $post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
  594. $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
  595. $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
  596. $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
  597. $args = array();
  598. if ( !empty ( $date ) ) {
  599. $query .= ' AND post_date = %s';
  600. $args[] = $post_date;
  601. }
  602. if ( !empty ( $title ) ) {
  603. $query .= ' AND post_title = %s';
  604. $args[] = $post_title;
  605. }
  606. if ( !empty ( $content ) ) {
  607. $query .= ' AND post_content = %s';
  608. $args[] = $post_content;
  609. }
  610. if ( !empty ( $args ) )
  611. return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
  612. return 0;
  613. }
  614. /**
  615. * Creates a new post from the "Write Post" form using $_POST information.
  616. *
  617. * @since 2.1.0
  618. *
  619. * @global WP_User $current_user
  620. *
  621. * @return int|WP_Error
  622. */
  623. function wp_write_post() {
  624. if ( isset($_POST['post_type']) )
  625. $ptype = get_post_type_object($_POST['post_type']);
  626. else
  627. $ptype = get_post_type_object('post');
  628. if ( !current_user_can( $ptype->cap->edit_posts ) ) {
  629. if ( 'page' == $ptype->name )
  630. return new WP_Error( 'edit_pages', __( 'Sorry, you are not allowed to create pages on this site.' ) );
  631. else
  632. return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to create posts or drafts on this site.' ) );
  633. }
  634. $_POST['post_mime_type'] = '';
  635. // Clear out any data in internal vars.
  636. unset( $_POST['filter'] );
  637. // Edit don't write if we have a post id.
  638. if ( isset( $_POST['post_ID'] ) )
  639. return edit_post();
  640. if ( isset($_POST['visibility']) ) {
  641. switch ( $_POST['visibility'] ) {
  642. case 'public' :
  643. $_POST['post_password'] = '';
  644. break;
  645. case 'password' :
  646. unset( $_POST['sticky'] );
  647. break;
  648. case 'private' :
  649. $_POST['post_status'] = 'private';
  650. $_POST['post_password'] = '';
  651. unset( $_POST['sticky'] );
  652. break;
  653. }
  654. }
  655. $translated = _wp_translate_postdata( false );
  656. if ( is_wp_error($translated) )
  657. return $translated;
  658. $translated = _wp_get_allowed_postdata( $translated );
  659. // Create the post.
  660. $post_ID = wp_insert_post( $translated );
  661. if ( is_wp_error( $post_ID ) )
  662. return $post_ID;
  663. if ( empty($post_ID) )
  664. return 0;
  665. add_meta( $post_ID );
  666. add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
  667. // Now that we have an ID we can fix any attachment anchor hrefs
  668. _fix_attachment_links( $post_ID );
  669. wp_set_post_lock( $post_ID );
  670. return $post_ID;
  671. }
  672. /**
  673. * Calls wp_write_post() and handles the errors.
  674. *
  675. * @since 2.0.0
  676. *
  677. * @return int|null
  678. */
  679. function write_post() {
  680. $result = wp_write_post();
  681. if ( is_wp_error( $result ) )
  682. wp_die( $result->get_error_message() );
  683. else
  684. return $result;
  685. }
  686. //
  687. // Post Meta
  688. //
  689. /**
  690. * Add post meta data defined in $_POST superglobal for post with given ID.
  691. *
  692. * @since 1.2.0
  693. *
  694. * @param int $post_ID
  695. * @return int|bool
  696. */
  697. function add_meta( $post_ID ) {
  698. $post_ID = (int) $post_ID;
  699. $metakeyselect = isset($_POST['metakeyselect']) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';
  700. $metakeyinput = isset($_POST['metakeyinput']) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : '';
  701. $metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : '';
  702. if ( is_string( $metavalue ) )
  703. $metavalue = trim( $metavalue );
  704. if ( ('0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput ) ) ) {
  705. /*
  706. * We have a key/value pair. If both the select and the input
  707. * for the key have data, the input takes precedence.
  708. */
  709. if ( '#NONE#' != $metakeyselect )
  710. $metakey = $metakeyselect;
  711. if ( $metakeyinput )
  712. $metakey = $metakeyinput; // default
  713. if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) )
  714. return false;
  715. $metakey = wp_slash( $metakey );
  716. return add_post_meta( $post_ID, $metakey, $metavalue );
  717. }
  718. return false;
  719. } // add_meta
  720. /**
  721. * Delete post meta data by meta ID.
  722. *
  723. * @since 1.2.0
  724. *
  725. * @param int $mid
  726. * @return bool
  727. */
  728. function delete_meta( $mid ) {
  729. return delete_metadata_by_mid( 'post' , $mid );
  730. }
  731. /**
  732. * Get a list of previously defined keys.
  733. *
  734. * @since 1.2.0
  735. *
  736. * @global wpdb $wpdb WordPress database abstraction object.
  737. *
  738. * @return mixed
  739. */
  740. function get_meta_keys() {
  741. global $wpdb;
  742. $keys = $wpdb->get_col( "
  743. SELECT meta_key
  744. FROM $wpdb->postmeta
  745. GROUP BY meta_key
  746. ORDER BY meta_key" );
  747. return $keys;
  748. }
  749. /**
  750. * Get post meta data by meta ID.
  751. *
  752. * @since 2.1.0
  753. *
  754. * @param int $mid
  755. * @return object|bool
  756. */
  757. function get_post_meta_by_id( $mid ) {
  758. return get_metadata_by_mid( 'post', $mid );
  759. }
  760. /**
  761. * Get meta data for the given post ID.
  762. *
  763. * @since 1.2.0
  764. *
  765. * @global wpdb $wpdb WordPress database abstraction object.
  766. *
  767. * @param int $postid
  768. * @return mixed
  769. */
  770. function has_meta( $postid ) {
  771. global $wpdb;
  772. return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id
  773. FROM $wpdb->postmeta WHERE post_id = %d
  774. ORDER BY meta_key,meta_id", $postid), ARRAY_A );
  775. }
  776. /**
  777. * Update post meta data by meta ID.
  778. *
  779. * @since 1.2.0
  780. *
  781. * @param int $meta_id
  782. * @param string $meta_key Expect Slashed
  783. * @param string $meta_value Expect Slashed
  784. * @return bool
  785. */
  786. function update_meta( $meta_id, $meta_key, $meta_value ) {
  787. $meta_key = wp_unslash( $meta_key );
  788. $meta_value = wp_unslash( $meta_value );
  789. return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key );
  790. }
  791. //
  792. // Private
  793. //
  794. /**
  795. * Replace hrefs of attachment anchors with up-to-date permalinks.
  796. *
  797. * @since 2.3.0
  798. * @access private
  799. *
  800. * @param int|object $post Post ID or post object.
  801. * @return void|int|WP_Error Void if nothing fixed. 0 or WP_Error on update failure. The post ID on update success.
  802. */
  803. function _fix_attachment_links( $post ) {
  804. $post = get_post( $post, ARRAY_A );
  805. $content = $post['post_content'];
  806. // Don't run if no pretty permalinks or post is not published, scheduled, or privately published.
  807. if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ) ) )
  808. return;
  809. // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero)
  810. if ( !strpos($content, '?attachment_id=') || !preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) )
  811. return;
  812. $site_url = get_bloginfo('url');
  813. $site_url = substr( $site_url, (int) strpos($site_url, '://') ); // remove the http(s)
  814. $replace = '';
  815. foreach ( $link_matches[1] as $key => $value ) {
  816. if ( !strpos($value, '?attachment_id=') || !strpos($value, 'wp-att-')
  817. || !preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match )
  818. || !preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) )
  819. continue;
  820. $quote = $url_match[1]; // the quote (single or double)
  821. $url_id = (int) $url_match[2];
  822. $rel_id = (int) $rel_match[1];
  823. if ( !$url_id || !$rel_id || $url_id != $rel_id || strpos($url_match[0], $site_url) === false )
  824. continue;
  825. $link = $link_matches[0][$key];
  826. $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );
  827. $content = str_replace( $link, $replace, $content );
  828. }
  829. if ( $replace ) {
  830. $post['post_content'] = $content;
  831. // Escape data pulled from DB.
  832. $post = add_magic_quotes($post);
  833. return wp_update_post($post);
  834. }
  835. }
  836. /**
  837. * Get all the possible statuses for a post_type
  838. *
  839. * @since 2.5.0
  840. *
  841. * @param string $type The post_type you want the statuses for
  842. * @return array As array of all the statuses for the supplied post type
  843. */
  844. function get_available_post_statuses($type = 'post') {
  845. $stati = wp_count_posts($type);
  846. return array_keys(get_object_vars($stati));
  847. }
  848. /**
  849. * Run the wp query to fetch the posts for listing on the edit posts page
  850. *
  851. * @since 2.5.0
  852. *
  853. * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal.
  854. * @return array
  855. */
  856. function wp_edit_posts_query( $q = false ) {
  857. if ( false === $q )
  858. $q = $_GET;
  859. $q['m'] = isset($q['m']) ? (int) $q['m'] : 0;
  860. $q['cat'] = isset($q['cat']) ? (int) $q['cat'] : 0;
  861. $post_stati = get_post_stati();
  862. if ( isset($q['post_type']) && in_array( $q['post_type'], get_post_types() ) )
  863. $post_type = $q['post_type'];
  864. else
  865. $post_type = 'post';
  866. $avail_post_stati = get_available_post_statuses($post_type);
  867. if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) {
  868. $post_status = $q['post_status'];
  869. $perm = 'readable';
  870. }
  871. if ( isset( $q['orderby'] ) ) {
  872. $orderby = $q['orderby'];
  873. } elseif ( isset( $q['post_status'] ) && in_array( $q['post_status'], array( 'pending', 'draft' ) ) ) {
  874. $orderby = 'modified';
  875. }
  876. if ( isset( $q['order'] ) ) {
  877. $order = $q['order'];
  878. } elseif ( isset( $q['post_status'] ) && 'pending' == $q['post_status'] ) {
  879. $order = 'ASC';
  880. }
  881. $per_page = "edit_{$post_type}_per_page";
  882. $posts_per_page = (int) get_user_option( $per_page );
  883. if ( empty( $posts_per_page ) || $posts_per_page < 1 )
  884. $posts_per_page = 20;
  885. /**
  886. * Filters the number of items per page to show for a specific 'per_page' type.
  887. *
  888. * The dynamic portion of the hook name, `$post_type`, refers to the post type.
  889. *
  890. * Some examples of filter hooks generated here include: 'edit_attachment_per_page',
  891. * 'edit_post_per_page', 'edit_page_per_page', etc.
  892. *
  893. * @since 3.0.0
  894. *
  895. * @param int $posts_per_page Number of posts to display per page for the given post
  896. * type. Default 20.
  897. */
  898. $posts_per_page = apply_filters( "edit_{$post_type}_per_page", $posts_per_page );
  899. /**
  900. * Filters the number of posts displayed per page when specifically listing "posts".
  901. *
  902. * @since 2.8.0
  903. *
  904. * @param int $posts_per_page Number of posts to be displayed. Default 20.
  905. * @param string $post_type The post type.
  906. */
  907. $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type );
  908. $query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page');
  909. // Hierarchical types require special args.
  910. if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) {
  911. $query['orderby'] = 'menu_order title';
  912. $query['order'] = 'asc';
  913. $query['posts_per_page'] = -1;
  914. $query['posts_per_archive_page'] = -1;
  915. $query['fields'] = 'id=>parent';
  916. }
  917. if ( ! empty( $q['show_sticky'] ) )
  918. $query['post__in'] = (array) get_option( 'sticky_posts' );
  919. wp( $query );
  920. return $avail_post_stati;
  921. }
  922. /**
  923. * Get all available post MIME types for a given post type.
  924. *
  925. * @since 2.5.0
  926. *
  927. * @global wpdb $wpdb WordPress database abstraction object.
  928. *
  929. * @param string $type
  930. * @return mixed
  931. */
  932. function get_available_post_mime_types($type = 'attachment') {
  933. global $wpdb;
  934. $types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type));
  935. return $types;
  936. }
  937. /**
  938. * Get the query variables for the current attachments request.
  939. *
  940. * @since 4.2.0
  941. *
  942. * @param array|false $q Optional. Array of query variables to use to build the query or false
  943. * to use $_GET superglobal. Default false.
  944. * @return array The parsed query vars.
  945. */
  946. function wp_edit_attachments_query_vars( $q = false ) {
  947. if ( false === $q ) {
  948. $q = $_GET;
  949. }
  950. $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0;
  951. $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
  952. $q['post_type'] = 'attachment';
  953. $post_type = get_post_type_object( 'attachment' );
  954. $states = 'inherit';
  955. if ( current_user_can( $post_type->cap->read_private_posts ) ) {
  956. $states .= ',private';
  957. }
  958. $q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states;
  959. $q['post_status'] = isset( $q['attachment-filter'] ) && 'trash' == $q['attachment-filter'] ? 'trash' : $states;
  960. $media_per_page = (int) get_user_option( 'upload_per_page' );
  961. if ( empty( $media_per_page ) || $media_per_page < 1 ) {
  962. $media_per_page = 20;
  963. }
  964. /**
  965. * Filters the number of items to list per page when listing media items.
  966. *
  967. * @since 2.9.0
  968. *
  969. * @param int $media_per_page Number of media to list. Default 20.
  970. */
  971. $q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page );
  972. $post_mime_types = get_post_mime_types();
  973. if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) ) {
  974. unset($q['post_mime_type']);
  975. }
  976. foreach ( array_keys( $post_mime_types ) as $type ) {
  977. if ( isset( $q['attachment-filter'] ) && "post_mime_type:$type" == $q['attachment-filter'] ) {
  978. $q['post_mime_type'] = $type;
  979. break;
  980. }
  981. }
  982. if ( isset( $q['detached'] ) || ( isset( $q['attachment-filter'] ) && 'detached' == $q['attachment-filter'] ) ) {
  983. $q['post_parent'] = 0;
  984. }
  985. if ( isset( $q['mine'] ) || ( isset( $q['attachment-filter'] ) && 'mine' == $q['attachment-filter'] ) ) {
  986. $q['author'] = get_current_user_id();
  987. }
  988. // Filter query clauses to include filenames.
  989. if ( isset( $q['s'] ) ) {
  990. add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
  991. }
  992. return $q;
  993. }
  994. /**
  995. * Executes a query for attachments. An array of WP_Query arguments
  996. * can be passed in, which will override the arguments set by this function.
  997. *
  998. * @since 2.5.0
  999. *
  1000. * @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal.
  1001. * @return array
  1002. */
  1003. function wp_edit_attachments_query( $q = false ) {
  1004. wp( wp_edit_attachments_query_vars( $q ) );
  1005. $post_mime_types = get_post_mime_types();
  1006. $avail_post_mime_types = get_available_post_mime_types( 'attachment' );
  1007. return array( $post_mime_types, $avail_post_mime_types );
  1008. }
  1009. /**
  1010. * Returns the list of classes to be used by a meta box.
  1011. *
  1012. * @since 2.5.0
  1013. *
  1014. * @param string $id
  1015. * @param string $page
  1016. * @return string
  1017. */
  1018. function postbox_classes( $id, $page ) {
  1019. if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) {
  1020. $classes = array( '' );
  1021. } elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) {
  1022. if ( !is_array( $closed ) ) {
  1023. $classes = array( '' );
  1024. } else {
  1025. $classes = in_array( $id, $closed ) ? array( 'closed' ) : array( '' );
  1026. }
  1027. } else {
  1028. $classes = array( '' );
  1029. }
  1030. /**
  1031. * Filters the postbox classes for a specific screen and screen ID combo.
  1032. *
  1033. * The dynamic portions of the hook name, `$page` and `$id`, refer to
  1034. * the screen and screen ID, respectively.
  1035. *
  1036. * @since 3.2.0
  1037. *
  1038. * @param array $classes An array of postbox classes.
  1039. */
  1040. $classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes );
  1041. return implode( ' ', $classes );
  1042. }
  1043. /**
  1044. * Get a sample permalink based off of the post name.
  1045. *
  1046. * @since 2.5.0
  1047. *
  1048. * @param int $id Post ID or post object.
  1049. * @param string $title Optional. Title to override the post's current title when generating the post name. Default null.
  1050. * @param string $name Optional. Name to override the post name. Default null.
  1051. * @return array Array containing the sample permalink with placeholder for the post name, and the post name.
  1052. */
  1053. function get_sample_permalink($id, $title = null, $name = null) {
  1054. $post = get_post( $id );
  1055. if ( ! $post )
  1056. return array( '', '' );
  1057. $ptype = get_post_type_object($post->post_type);
  1058. $original_status = $post->post_status;
  1059. $original_date = $post->post_date;
  1060. $original_name = $post->post_name;
  1061. // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
  1062. if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ) ) ) {
  1063. $post->post_status = 'publish';
  1064. $post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
  1065. }
  1066. // If the user wants to set a new name -- override the current one
  1067. // Note: if empty name is supplied -- use the title instead, see #6072
  1068. if ( !is_null($name) )
  1069. $post->post_name = sanitize_title($name ? $name : $title, $post->ID);
  1070. $post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent);
  1071. $post->filter = 'sample';
  1072. $permalink = get_permalink($post, true);
  1073. // Replace custom post_type Token with generic pagename token for ease of use.
  1074. $permalink = str_replace("%$post->post_type%", '%pagename%', $permalink);
  1075. // Handle page hierarchy
  1076. if ( $ptype->hierarchical ) {
  1077. $uri = get_page_uri($post);
  1078. if ( $uri ) {
  1079. $uri = untrailingslashit($uri);
  1080. $uri = strrev( stristr( strrev( $uri ), '/' ) );
  1081. $uri = untrailingslashit($uri);
  1082. }
  1083. /** This filter is documented in wp-admin/edit-tag-form.php */
  1084. $uri = apply_filters( 'editable_slug', $uri, $post );
  1085. if ( !empty($uri) )
  1086. $uri .= '/';
  1087. $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
  1088. }
  1089. /** This filter is documented in wp-admin/edit-tag-form.php */
  1090. $permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) );
  1091. $post->post_status = $original_status;
  1092. $post->post_date = $original_date;
  1093. $post->post_name = $original_name;
  1094. unset($post->filter);
  1095. /**
  1096. * Filters the sample permalink.
  1097. *
  1098. * @since 4.4.0
  1099. *
  1100. * @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name.
  1101. * @param int $post_id Post ID.
  1102. * @param string $title Post title.
  1103. * @param string $name Post name (slug).
  1104. * @param WP_Post $post Post object.
  1105. */
  1106. return apply_filters( 'get_sample_permalink', $permalink, $post->ID, $title, $name, $post );
  1107. }
  1108. /**
  1109. * Returns the HTML of the sample permalink slug editor.
  1110. *
  1111. * @since 2.5.0
  1112. *
  1113. * @param int $id Post ID or post object.
  1114. * @param string $new_title Optional. New title. Default null.
  1115. * @param string $new_slug Optional. New slug. Default null.
  1116. * @return string The HTML of the sample permalink slug editor.
  1117. */
  1118. function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
  1119. $post = get_post( $id );
  1120. if ( ! $post )
  1121. return '';
  1122. list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
  1123. $view_link = false;
  1124. $preview_target = '';
  1125. if ( current_user_can( 'read_post', $post->ID ) ) {
  1126. if ( 'draft' === $post->post_status || empty( $post->post_name ) ) {
  1127. $view_link = get_preview_post_link( $post );
  1128. $preview_target = " target='wp-preview-{$post->ID}'";
  1129. } else {
  1130. if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
  1131. $view_link = get_permalink( $post );
  1132. } else {
  1133. // Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set
  1134. $view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink );
  1135. }
  1136. }
  1137. }
  1138. // Permalinks without a post/page name placeholder don't have anything to edit
  1139. if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) {
  1140. $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
  1141. if ( false !== $view_link ) {
  1142. $display_link = urldecode( $view_link );
  1143. $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>\n";
  1144. } else {
  1145. $return .= '<span id="sample-permalink">' . $permalink . "</span>\n";
  1146. }
  1147. // Encourage a pretty permalink setting
  1148. if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) {
  1149. $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
  1150. }
  1151. } else {
  1152. if ( mb_strlen( $post_name ) > 34 ) {
  1153. $post_name_abridged = mb_substr( $post_name, 0, 16 ) . '&hellip;' . mb_substr( $post_name, -16 );
  1154. } else {
  1155. $post_name_abridged = $post_name;
  1156. }
  1157. $post_name_html = '<span id="editable-post-name">' . esc_html( $post_name_abridged ) . '</span>';
  1158. $display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) );
  1159. $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
  1160. $return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>\n";
  1161. $return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
  1162. $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";
  1163. $return .= '<span id="editable-post-name-full">' . esc_html( $post_name ) . "</span>\n";
  1164. }
  1165. /**
  1166. * Filters the sample permalink HTML markup.
  1167. *
  1168. * @since 2.9.0
  1169. * @since 4.4.0 Added `$post` parameter.
  1170. *
  1171. * @param string $return Sample permalink HTML markup.
  1172. * @param int $post_id Post ID.
  1173. * @param string $new_title New sample permalink title.
  1174. * @param string $new_slug New sample permalink slug.
  1175. * @param WP_Post $post Post object.
  1176. */
  1177. $return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post );
  1178. return $return;
  1179. }
  1180. /**
  1181. * Output HTML for the post thumbnail meta-box.
  1182. *
  1183. * @since 2.9.0
  1184. *
  1185. * @param int $thumbnail_id ID of the attachment used for thumbnail
  1186. * @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post.
  1187. * @return string html
  1188. */
  1189. function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
  1190. $_wp_additional_image_sizes = wp_get_additional_image_sizes();
  1191. $post = get_post( $post );
  1192. $post_type_object = get_post_type_object( $post->post_type );
  1193. $set_thumbnail_link = '<p class="hide-if-no-js"><a href="%s" id="set-post-thumbnail"%s class="thickbox">%s</a></p>';
  1194. $upload_iframe_src = get_upload_iframe_src( 'image', $post->ID );
  1195. $content = sprintf( $set_thumbnail_link,
  1196. esc_url( $upload_iframe_src ),
  1197. '', // Empty when there's no featured image set, `aria-describedby` attribute otherwise.
  1198. esc_html( $post_type_object->labels->set_featured_image )
  1199. );
  1200. if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
  1201. $size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : array( 266, 266 );
  1202. /**
  1203. * Filters the size used to display the post thumbnail image in the 'Featured Image' meta box.
  1204. *
  1205. * Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail'
  1206. * image size is registered, which differs from the 'thumbnail' image size
  1207. * managed via the Settings > Media screen. See the `$size` parameter description
  1208. * for more information on default values.
  1209. *
  1210. * @since 4.4.0
  1211. *
  1212. * @param string|array $size Post thumbnail image size to display in the meta box. Accepts any valid
  1213. * image size, or an array of width and height values in pixels (in that order).
  1214. * If the 'post-thumbnail' size is set, default is 'post-thumbnail'. Otherwise,
  1215. * default is an array with 266 as both the height and width values.
  1216. * @param int $thumbnail_id Post thumbnail attachment ID.
  1217. * @param WP_Post $post The post object associated with the thumbnail.
  1218. */
  1219. $size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post );
  1220. $thumbnail_html = wp_get_attachment_image( $thumbnail_id, $size );
  1221. if ( ! empty( $thumbnail_html ) ) {
  1222. $content = sprintf( $set_thumbnail_link,
  1223. esc_url( $upload_iframe_src ),
  1224. ' aria-describedby="set-post-thumbnail-desc"',
  1225. $thumbnail_html
  1226. );
  1227. $content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __( 'Click the image to edit or update' ) . '</p>';
  1228. $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>';
  1229. }
  1230. }
  1231. $content .= '<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="' . esc_attr( $thumbnail_id ? $thumbnail_id : '-1' ) . '" />';
  1232. /**
  1233. * Filters the admin post thumbnail HTML markup to return.
  1234. *
  1235. * @since 2.9.0
  1236. * @since 3.5.0 Added the `$post_id` parameter.
  1237. * @since 4.6.0 Added the `$thumbnail_id` parameter.
  1238. *
  1239. * @param string $content Admin post thumbnail HTML markup.
  1240. * @param int $post_id Post ID.
  1241. * @param int $thumbnail_id Thumbnail ID.
  1242. */
  1243. return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id );
  1244. }
  1245. /**
  1246. * Check to see if the post is currently being edited by another user.
  1247. *
  1248. * @since 2.5.0
  1249. *
  1250. * @param int $post_id ID of the post to check for editing.
  1251. * @return int|false ID of the user with lock. False if the post does not exist, post is not locked,
  1252. * the user with lock does not exist, or the post is locked by current user.
  1253. */
  1254. function wp_check_post_lock( $post_id ) {
  1255. if ( ! $post = get_post( $post_id ) ) {
  1256. return false;
  1257. }
  1258. if ( ! $lock = get_post_meta( $post->ID, '_edit_lock', true ) ) {
  1259. return false;
  1260. }
  1261. $lock = explode( ':', $lock );
  1262. $time = $lock[0];
  1263. $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
  1264. if ( ! get_userdata( $user ) ) {
  1265. return false;
  1266. }
  1267. /** This filter is documented in wp-admin/includes/ajax-actions.php */
  1268. $time_window = apply_filters( 'wp_check_post_lock_window', 150 );
  1269. if ( $time && $time > time() - $time_window && $user != get_current_user_id() ) {
  1270. return $user;
  1271. }
  1272. return false;
  1273. }
  1274. /**
  1275. * Mark the post as currently being edited by the current user
  1276. *
  1277. * @since 2.5.0
  1278. *
  1279. * @param int $post_id ID of the post being edited.
  1280. * @return array|false Array of the lock time and user ID. False if the post does not exist, or
  1281. * there is no current user.
  1282. */
  1283. function wp_set_post_lock( $post_id ) {
  1284. if ( ! $post = get_post( $post_id ) ) {
  1285. return false;
  1286. }
  1287. if ( 0 == ( $user_id = get_current_user_id() ) ) {
  1288. return false;
  1289. }
  1290. $now = time();
  1291. $lock = "$now:$user_id";
  1292. update_post_meta( $post->ID, '_edit_lock', $lock );
  1293. return array( $now, $user_id );
  1294. }
  1295. /**
  1296. * Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.
  1297. *
  1298. * @since 2.8.5
  1299. * @return none
  1300. */
  1301. function _admin_notice_post_locked() {
  1302. if ( ! $post = get_post() )
  1303. return;
  1304. $user = null;
  1305. if ( $user_id = wp_check_post_lock( $post->ID ) )
  1306. $user = get_userdata( $user_id );
  1307. if ( $user ) {
  1308. /**
  1309. * Filters whether to show the post locked dialog.
  1310. *
  1311. * Returning a falsey value to the filter will short-circuit displaying the dialog.
  1312. *
  1313. * @since 3.6.0
  1314. *
  1315. * @param bool $display Whether to display the dialog. Default true.
  1316. * @param WP_Post $post Post object.
  1317. * @param WP_User|bool $user WP_User object on success, false otherwise.
  1318. */
  1319. if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) )
  1320. return;
  1321. $locked = true;
  1322. } else {
  1323. $locked = false;
  1324. }
  1325. if ( $locked && ( $sendback = wp_get_referer() ) &&
  1326. false === strpos( $sendback, 'post.php' ) && false === strpos( $sendback, 'post-new.php' ) ) {
  1327. $sendback_text = __('Go back');
  1328. } else {
  1329. $sendback = admin_url( 'edit.php' );
  1330. if ( 'post' != $post->post_type )
  1331. $sendback = add_query_arg( 'post_type', $post->post_type, $sendback );
  1332. $sendback_text = get_post_type_object( $post->post_type )->labels->all_items;
  1333. }
  1334. $hidden = $locked ? '' : ' hidden';
  1335. ?>
  1336. <div id="post-lock-dialog" class="notification-dialog-wrap<?php echo $hidden; ?>">
  1337. <div class="notification-dialog-background"></div>
  1338. <div class="notification-dialog">
  1339. <?php
  1340. if ( $locked ) {
  1341. $query_args = array();
  1342. if ( get_post_type_object( $post->post_type )->public ) {
  1343. if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) {
  1344. // Latest content is in autosave
  1345. $nonce = wp_create_nonce( 'post_preview_' . $post->ID );
  1346. $query_args['preview_id'] = $post->ID;
  1347. $query_args['preview_nonce'] = $nonce;
  1348. }
  1349. }
  1350. $preview_link = get_preview_post_link( $post->ID, $query_args );
  1351. /**
  1352. * Filters whether to allow the post lock to be overridden.
  1353. *
  1354. * Returning a falsey value to the filter will disable the ability
  1355. * to override the post lock.
  1356. *
  1357. * @since 3.6.0
  1358. *
  1359. * @param bool $override Whether to allow overriding post locks. Default true.
  1360. * @param WP_Post $post Post object.
  1361. * @param WP_User $user User object.
  1362. */
  1363. $override = apply_filters( 'override_post_lock', true, $post, $user );
  1364. $tab_last = $override ? '' : ' wp-tab-last';
  1365. ?>
  1366. <div class="post-locked-message">
  1367. <div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div>
  1368. <p class="currently-editing wp-tab-first" tabindex="0">
  1369. <?php
  1370. if ( $override ) {
  1371. /* translators: %s: user's display name */
  1372. printf( __( '%s is already editing this post. Do you want to take over?' ), esc_html( $user->display_name ) );
  1373. } else {
  1374. /* translators: %s: user's display name */
  1375. printf( __( '%s is already editing this post.' ), esc_html( $user->display_name ) );
  1376. }
  1377. ?>
  1378. </p>
  1379. <?php
  1380. /**
  1381. * Fires inside the post locked dialog before the buttons are displayed.
  1382. *
  1383. * @since 3.6.0
  1384. *
  1385. * @param WP_Post $post Post object.
  1386. */
  1387. do_action( 'post_locked_dialog', $post );
  1388. ?>
  1389. <p>
  1390. <a class="button" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a>
  1391. <?php if ( $preview_link ) { ?>
  1392. <a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"><?php _e('Preview'); ?></a>
  1393. <?php
  1394. }
  1395. // Allow plugins to prevent some users overriding the post lock
  1396. if ( $override ) {
  1397. ?>
  1398. <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>
  1399. <?php
  1400. }
  1401. ?>
  1402. </p>
  1403. </div>
  1404. <?php
  1405. } else {
  1406. ?>
  1407. <div class="post-taken-over">
  1408. <div class="post-locked-avatar"></div>
  1409. <p class="wp-tab-first" tabindex="0">
  1410. <span class="currently-editing"></span><br />
  1411. <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>
  1412. <span class="locked-saved hidden"><?php _e('Your latest changes were saved as a revision.'); ?></span>
  1413. </p>
  1414. <?php
  1415. /**
  1416. * Fires inside the dialog displayed when a user has lost the post lock.
  1417. *
  1418. * @since 3.6.0
  1419. *
  1420. * @param WP_Post $post Post object.
  1421. */
  1422. do_action( 'post_lock_lost_dialog', $post );
  1423. ?>
  1424. <p><a class="button button-primary wp-tab-last" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a></p>
  1425. </div>
  1426. <?php
  1427. }
  1428. ?>
  1429. </div>
  1430. </div>
  1431. <?php
  1432. }
  1433. /**
  1434. * Creates autosave data for the specified post from $_POST data.
  1435. *
  1436. * @since 2.6.0
  1437. *
  1438. * @param mixed $post_data Associative array containing the post data or int post ID.
  1439. * @return mixed The autosave revision ID. WP_Error or 0 on error.
  1440. */
  1441. function wp_create_post_autosave( $post_data ) {
  1442. if ( is_numeric( $post_data ) ) {
  1443. $post_id = $post_data;
  1444. $post_data = $_POST;
  1445. } else {
  1446. $post_id = (int) $post_data['post_ID'];
  1447. }
  1448. $post_data = _wp_translate_postdata( true, $post_data );
  1449. if ( is_wp_error( $post_data ) )
  1450. return $post_data;
  1451. $post_data = _wp_get_allowed_postdata( $post_data );
  1452. $post_author = get_current_user_id();
  1453. // Store one autosave per author. If there is already an autosave, overwrite it.
  1454. if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
  1455. $new_autosave = _wp_post_revision_data( $post_data, true );
  1456. $new_autosave['ID'] = $old_autosave->ID;
  1457. $new_autosave['post_author'] = $post_author;
  1458. // If the new autosave has the same content as the post, delete the autosave.
  1459. $post = get_post( $post_id );
  1460. $autosave_is_different = false;
  1461. foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
  1462. if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
  1463. $autosave_is_different = true;
  1464. break;
  1465. }
  1466. }
  1467. if ( ! $autosave_is_different ) {
  1468. wp_delete_post_revision( $old_autosave->ID );
  1469. return 0;
  1470. }
  1471. /**
  1472. * Fires before an autosave is stored.
  1473. *
  1474. * @since 4.1.0
  1475. *
  1476. * @param array $new_autosave Post array - the autosave that is about to be saved.
  1477. */
  1478. do_action( 'wp_creating_autosave', $new_autosave );
  1479. return wp_update_post( $new_autosave );
  1480. }
  1481. // _wp_put_post_revision() expects unescaped.
  1482. $post_data = wp_unslash( $post_data );
  1483. // Otherwise create the new autosave as a special post revision
  1484. return _wp_put_post_revision( $post_data, true );
  1485. }
  1486. /**
  1487. * Saves a draft or manually autosaves for the purpose of showing a post preview.
  1488. *
  1489. * @since 2.7.0
  1490. *
  1491. * @return string URL to redirect to show the preview.
  1492. */
  1493. function post_preview() {
  1494. $post_ID = (int) $_POST['post_ID'];
  1495. $_POST['ID'] = $post_ID;
  1496. if ( ! $post = get_post( $post_ID ) ) {
  1497. wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
  1498. }
  1499. if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  1500. wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
  1501. }
  1502. $is_autosave = false;
  1503. if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) {
  1504. $saved_post_id = edit_post();
  1505. } else {
  1506. $is_autosave = true;
  1507. if ( isset( $_POST['post_status'] ) && 'auto-draft' == $_POST['post_status'] )
  1508. $_POST['post_status'] = 'draft';
  1509. $saved_post_id = wp_create_post_autosave( $post->ID );
  1510. }
  1511. if ( is_wp_error( $saved_post_id ) )
  1512. wp_die( $saved_post_id->get_error_message() );
  1513. $query_args = array();
  1514. if ( $is_autosave && $saved_post_id ) {
  1515. $query_args['preview_id'] = $post->ID;
  1516. $query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
  1517. if ( isset( $_POST['post_format'] ) ) {
  1518. $query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
  1519. }
  1520. if ( isset( $_POST['_thumbnail_id'] ) ) {
  1521. $query_args['_thumbnail_id'] = ( intval( $_POST['_thumbnail_id'] ) <= 0 ) ? '-1' : intval( $_POST['_thumbnail_id'] );
  1522. }
  1523. }
  1524. return get_preview_post_link( $post, $query_args );
  1525. }
  1526. /**
  1527. * Save a post submitted with XHR
  1528. *
  1529. * Intended for use with heartbeat and autosave.js
  1530. *
  1531. * @since 3.9.0
  1532. *
  1533. * @param array $post_data Associative array of the submitted post data.
  1534. * @return mixed The value 0 or WP_Error on failure. The saved post ID on success.
  1535. * The ID can be the draft post_id or the autosave revision post_id.
  1536. */
  1537. function wp_autosave( $post_data ) {
  1538. // Back-compat
  1539. if ( ! defined( 'DOING_AUTOSAVE' ) )
  1540. define( 'DOING_AUTOSAVE', true );
  1541. $post_id = (int) $post_data['post_id'];
  1542. $post_data['ID'] = $post_data['post_ID'] = $post_id;
  1543. if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) ) {
  1544. return new WP_Error( 'invalid_nonce', __( 'Error while saving.' ) );
  1545. }
  1546. $post = get_post( $post_id );
  1547. if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  1548. return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to edit this item.' ) );
  1549. }
  1550. if ( 'auto-draft' == $post->post_status )
  1551. $post_data['post_status'] = 'draft';
  1552. if ( $post_data['post_type'] != 'page' && ! empty( $post_data['catslist'] ) )
  1553. $post_data['post_category'] = explode( ',', $post_data['catslist'] );
  1554. if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) {
  1555. // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked
  1556. return edit_post( wp_slash( $post_data ) );
  1557. } else {
  1558. // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
  1559. return wp_create_post_autosave( wp_slash( $post_data ) );
  1560. }
  1561. }
  1562. /**
  1563. * Redirect to previous page.
  1564. *
  1565. * @param int $post_id Optional. Post ID.
  1566. */
  1567. function redirect_post($post_id = '') {
  1568. if ( isset($_POST['save']) || isset($_POST['publish']) ) {
  1569. $status = get_post_status( $post_id );
  1570. if ( isset( $_POST['publish'] ) ) {
  1571. switch ( $status ) {
  1572. case 'pending':
  1573. $message = 8;
  1574. break;
  1575. case 'future':
  1576. $message = 9;
  1577. break;
  1578. default:
  1579. $message = 6;
  1580. }
  1581. } else {
  1582. $message = 'draft' == $status ? 10 : 1;
  1583. }
  1584. $location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) );
  1585. } elseif ( isset($_POST['addmeta']) && $_POST['addmeta'] ) {
  1586. $location = add_query_arg( 'message', 2, wp_get_referer() );
  1587. $location = explode('#', $location);
  1588. $location = $location[0] . '#postcustom';
  1589. } elseif ( isset($_POST['deletemeta']) && $_POST['deletemeta'] ) {
  1590. $location = add_query_arg( 'message', 3, wp_get_referer() );
  1591. $location = explode('#', $location);
  1592. $location = $location[0] . '#postcustom';
  1593. } else {
  1594. $location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) );
  1595. }
  1596. /**
  1597. * Filters the post redirect destination URL.
  1598. *
  1599. * @since 2.9.0
  1600. *
  1601. * @param string $location The destination URL.
  1602. * @param int $post_id The post ID.
  1603. */
  1604. wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
  1605. exit;
  1606. }