Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

1857 Zeilen
58 KiB

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