PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/post.php

https://github.com/shaiquddin/WordPress
PHP | 1324 lines | 800 code | 212 blank | 312 comment | 277 complexity | 43fc838ee3c670323d1c3624fd0211ca MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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. if ( isset( $post_data['content'] ) )
  26. $post_data['post_content'] = $post_data['content'];
  27. if ( isset( $post_data['excerpt'] ) )
  28. $post_data['post_excerpt'] = $post_data['excerpt'];
  29. if ( isset( $post_data['parent_id'] ) )
  30. $post_data['post_parent'] = (int) $post_data['parent_id'];
  31. if ( isset($post_data['trackback_url']) )
  32. $post_data['to_ping'] = $post_data['trackback_url'];
  33. if ( !isset($post_data['user_ID']) )
  34. $post_data['user_ID'] = $GLOBALS['user_ID'];
  35. if (!empty ( $post_data['post_author_override'] ) ) {
  36. $post_data['post_author'] = (int) $post_data['post_author_override'];
  37. } else {
  38. if (!empty ( $post_data['post_author'] ) ) {
  39. $post_data['post_author'] = (int) $post_data['post_author'];
  40. } else {
  41. $post_data['post_author'] = (int) $post_data['user_ID'];
  42. }
  43. }
  44. $ptype = get_post_type_object( $post_data['post_type'] );
  45. if ( isset($post_data['user_ID']) && ($post_data['post_author'] != $post_data['user_ID']) ) {
  46. if ( !current_user_can( $ptype->cap->edit_others_posts ) ) {
  47. if ( 'page' == $post_data['post_type'] ) {
  48. return new WP_Error( 'edit_others_pages', $update ?
  49. __( 'You are not allowed to edit pages as this user.' ) :
  50. __( 'You are not allowed to create pages as this user.' )
  51. );
  52. } else {
  53. return new WP_Error( 'edit_others_posts', $update ?
  54. __( 'You are not allowed to edit posts as this user.' ) :
  55. __( 'You are not allowed to create posts as this user.' )
  56. );
  57. }
  58. }
  59. }
  60. // What to do based on which button they pressed
  61. if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
  62. $post_data['post_status'] = 'draft';
  63. if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] )
  64. $post_data['post_status'] = 'private';
  65. if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( !isset($post_data['post_status']) || $post_data['post_status'] != 'private' ) )
  66. $post_data['post_status'] = 'publish';
  67. if ( isset($post_data['advanced']) && '' != $post_data['advanced'] )
  68. $post_data['post_status'] = 'draft';
  69. if ( isset($post_data['pending']) && '' != $post_data['pending'] )
  70. $post_data['post_status'] = 'pending';
  71. if ( isset( $post_data['ID'] ) )
  72. $post_id = $post_data['ID'];
  73. else
  74. $post_id = false;
  75. $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
  76. // Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
  77. // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
  78. if ( isset($post_data['post_status']) && ('publish' == $post_data['post_status'] && !current_user_can( $ptype->cap->publish_posts )) )
  79. if ( $previous_status != 'publish' || !current_user_can( 'edit_post', $post_id ) )
  80. $post_data['post_status'] = 'pending';
  81. if ( ! isset($post_data['post_status']) )
  82. $post_data['post_status'] = $previous_status;
  83. if (!isset( $post_data['comment_status'] ))
  84. $post_data['comment_status'] = 'closed';
  85. if (!isset( $post_data['ping_status'] ))
  86. $post_data['ping_status'] = 'closed';
  87. foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
  88. if ( !empty( $post_data['hidden_' . $timeunit] ) && $post_data['hidden_' . $timeunit] != $post_data[$timeunit] ) {
  89. $post_data['edit_date'] = '1';
  90. break;
  91. }
  92. }
  93. if ( !empty( $post_data['edit_date'] ) ) {
  94. $aa = $post_data['aa'];
  95. $mm = $post_data['mm'];
  96. $jj = $post_data['jj'];
  97. $hh = $post_data['hh'];
  98. $mn = $post_data['mn'];
  99. $ss = $post_data['ss'];
  100. $aa = ($aa <= 0 ) ? date('Y') : $aa;
  101. $mm = ($mm <= 0 ) ? date('n') : $mm;
  102. $jj = ($jj > 31 ) ? 31 : $jj;
  103. $jj = ($jj <= 0 ) ? date('j') : $jj;
  104. $hh = ($hh > 23 ) ? $hh -24 : $hh;
  105. $mn = ($mn > 59 ) ? $mn -60 : $mn;
  106. $ss = ($ss > 59 ) ? $ss -60 : $ss;
  107. $post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
  108. $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
  109. if ( !$valid_date ) {
  110. return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
  111. }
  112. $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
  113. }
  114. return $post_data;
  115. }
  116. /**
  117. * Update an existing post with values provided in $_POST.
  118. *
  119. * @since 1.5.0
  120. *
  121. * @param array $post_data Optional.
  122. * @return int Post ID.
  123. */
  124. function edit_post( $post_data = null ) {
  125. if ( empty($post_data) )
  126. $post_data = &$_POST;
  127. // Clear out any data in internal vars.
  128. unset( $post_data['filter'] );
  129. $post_ID = (int) $post_data['post_ID'];
  130. $post = get_post( $post_ID );
  131. $post_data['post_type'] = $post->post_type;
  132. $post_data['post_mime_type'] = $post->post_mime_type;
  133. $ptype = get_post_type_object($post_data['post_type']);
  134. if ( !current_user_can( $ptype->cap->edit_post, $post_ID ) ) {
  135. if ( 'page' == $post_data['post_type'] )
  136. wp_die( __('You are not allowed to edit this page.' ));
  137. else
  138. wp_die( __('You are not allowed to edit this post.' ));
  139. }
  140. $post_data = _wp_translate_postdata( true, $post_data );
  141. if ( is_wp_error($post_data) )
  142. wp_die( $post_data->get_error_message() );
  143. if ( 'autosave' != $post_data['action'] && 'auto-draft' == $post_data['post_status'] )
  144. $post_data['post_status'] = 'draft';
  145. if ( isset($post_data['visibility']) ) {
  146. switch ( $post_data['visibility'] ) {
  147. case 'public' :
  148. $post_data['post_password'] = '';
  149. break;
  150. case 'password' :
  151. unset( $post_data['sticky'] );
  152. break;
  153. case 'private' :
  154. $post_data['post_status'] = 'private';
  155. $post_data['post_password'] = '';
  156. unset( $post_data['sticky'] );
  157. break;
  158. }
  159. }
  160. // Post Formats
  161. if ( isset( $post_data['post_format'] ) ) {
  162. if ( current_theme_supports( 'post-formats', $post_data['post_format'] ) )
  163. set_post_format( $post_ID, $post_data['post_format'] );
  164. elseif ( '0' == $post_data['post_format'] )
  165. set_post_format( $post_ID, false );
  166. }
  167. // Featured Images
  168. if ( isset( $post_data['thumbnail_id'] ) ) {
  169. if ( '-1' == $post_data['thumbnail_id'] )
  170. delete_post_thumbnail( $post_ID );
  171. else
  172. set_post_thumbnail( $post_ID, $post_data['thumbnail_id'] );
  173. }
  174. // Meta Stuff
  175. if ( isset($post_data['meta']) && $post_data['meta'] ) {
  176. foreach ( $post_data['meta'] as $key => $value ) {
  177. if ( !$meta = get_post_meta_by_id( $key ) )
  178. continue;
  179. if ( $meta->post_id != $post_ID )
  180. continue;
  181. if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) )
  182. continue;
  183. update_meta( $key, $value['key'], $value['value'] );
  184. }
  185. }
  186. if ( isset($post_data['deletemeta']) && $post_data['deletemeta'] ) {
  187. foreach ( $post_data['deletemeta'] as $key => $value ) {
  188. if ( !$meta = get_post_meta_by_id( $key ) )
  189. continue;
  190. if ( $meta->post_id != $post_ID )
  191. continue;
  192. if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) )
  193. continue;
  194. delete_meta( $key );
  195. }
  196. }
  197. // Attachment stuff
  198. if ( 'attachment' == $post_data['post_type'] && isset( $post_data['_wp_attachment_image_alt'] ) ) {
  199. $image_alt = get_post_meta( $post_ID, '_wp_attachment_image_alt', true );
  200. if ( $image_alt != stripslashes( $post_data['_wp_attachment_image_alt'] ) ) {
  201. $image_alt = wp_strip_all_tags( stripslashes( $post_data['_wp_attachment_image_alt'] ), true );
  202. // update_meta expects slashed
  203. update_post_meta( $post_ID, '_wp_attachment_image_alt', addslashes( $image_alt ) );
  204. }
  205. }
  206. add_meta( $post_ID );
  207. update_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
  208. wp_update_post( $post_data );
  209. // Now that we have an ID we can fix any attachment anchor hrefs
  210. _fix_attachment_links( $post_ID );
  211. wp_set_post_lock( $post_ID );
  212. if ( current_user_can( $ptype->cap->edit_others_posts ) ) {
  213. if ( ! empty( $post_data['sticky'] ) )
  214. stick_post( $post_ID );
  215. else
  216. unstick_post( $post_ID );
  217. }
  218. return $post_ID;
  219. }
  220. /**
  221. * Process the post data for the bulk editing of posts.
  222. *
  223. * Updates all bulk edited posts/pages, adding (but not removing) tags and
  224. * categories. Skips pages when they would be their own parent or child.
  225. *
  226. * @since 2.7.0
  227. *
  228. * @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal.
  229. * @return array
  230. */
  231. function bulk_edit_posts( $post_data = null ) {
  232. global $wpdb;
  233. if ( empty($post_data) )
  234. $post_data = &$_POST;
  235. if ( isset($post_data['post_type']) )
  236. $ptype = get_post_type_object($post_data['post_type']);
  237. else
  238. $ptype = get_post_type_object('post');
  239. if ( !current_user_can( $ptype->cap->edit_posts ) ) {
  240. if ( 'page' == $ptype->name )
  241. wp_die( __('You are not allowed to edit pages.'));
  242. else
  243. wp_die( __('You are not allowed to edit posts.'));
  244. }
  245. if ( -1 == $post_data['_status'] ) {
  246. $post_data['post_status'] = null;
  247. unset($post_data['post_status']);
  248. } else {
  249. $post_data['post_status'] = $post_data['_status'];
  250. }
  251. unset($post_data['_status']);
  252. $post_IDs = array_map( 'intval', (array) $post_data['post'] );
  253. $reset = array( 'post_author', 'post_status', 'post_password', 'post_parent', 'page_template', 'comment_status', 'ping_status', 'keep_private', 'tax_input', 'post_category', 'sticky' );
  254. foreach ( $reset as $field ) {
  255. if ( isset($post_data[$field]) && ( '' == $post_data[$field] || -1 == $post_data[$field] ) )
  256. unset($post_data[$field]);
  257. }
  258. if ( isset($post_data['post_category']) ) {
  259. if ( is_array($post_data['post_category']) && ! empty($post_data['post_category']) )
  260. $new_cats = array_map( 'absint', $post_data['post_category'] );
  261. else
  262. unset($post_data['post_category']);
  263. }
  264. $tax_input = array();
  265. if ( isset($post_data['tax_input'])) {
  266. foreach ( $post_data['tax_input'] as $tax_name => $terms ) {
  267. if ( empty($terms) )
  268. continue;
  269. if ( is_taxonomy_hierarchical( $tax_name ) ) {
  270. $tax_input[ $tax_name ] = array_map( 'absint', $terms );
  271. } else {
  272. $comma = _x( ',', 'tag delimiter' );
  273. if ( ',' !== $comma )
  274. $terms = str_replace( $comma, ',', $terms );
  275. $tax_input[ $tax_name ] = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
  276. }
  277. }
  278. }
  279. if ( isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent']) ) {
  280. $pages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'");
  281. $children = array();
  282. for ( $i = 0; $i < 50 && $parent > 0; $i++ ) {
  283. $children[] = $parent;
  284. foreach ( $pages as $page ) {
  285. if ( $page->ID == $parent ) {
  286. $parent = $page->post_parent;
  287. break;
  288. }
  289. }
  290. }
  291. }
  292. if ( isset( $post_data['post_format'] ) ) {
  293. if ( '0' == $post_data['post_format'] )
  294. $post_data['post_format'] = false;
  295. // don't change the post format if it's not supported or not '0' (standard)
  296. elseif ( ! current_theme_supports( 'post-formats', $post_data['post_format'] ) )
  297. unset( $post_data['post_format'] );
  298. }
  299. $updated = $skipped = $locked = array();
  300. foreach ( $post_IDs as $post_ID ) {
  301. $post_type_object = get_post_type_object( get_post_type( $post_ID ) );
  302. if ( !isset( $post_type_object ) || ( isset($children) && in_array($post_ID, $children) ) || !current_user_can( $post_type_object->cap->edit_post, $post_ID ) ) {
  303. $skipped[] = $post_ID;
  304. continue;
  305. }
  306. if ( wp_check_post_lock( $post_ID ) ) {
  307. $locked[] = $post_ID;
  308. continue;
  309. }
  310. $post = get_post( $post_ID );
  311. $tax_names = get_object_taxonomies( $post );
  312. foreach ( $tax_names as $tax_name ) {
  313. $taxonomy_obj = get_taxonomy($tax_name);
  314. if ( isset( $tax_input[$tax_name]) && current_user_can( $taxonomy_obj->cap->assign_terms ) )
  315. $new_terms = $tax_input[$tax_name];
  316. else
  317. $new_terms = array();
  318. if ( $taxonomy_obj->hierarchical )
  319. $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'ids') );
  320. else
  321. $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'names') );
  322. $post_data['tax_input'][$tax_name] = array_merge( $current_terms, $new_terms );
  323. }
  324. if ( isset($new_cats) && in_array( 'category', $tax_names ) ) {
  325. $cats = (array) wp_get_post_categories($post_ID);
  326. $post_data['post_category'] = array_unique( array_merge($cats, $new_cats) );
  327. unset( $post_data['tax_input']['category'] );
  328. }
  329. $post_data['post_mime_type'] = $post->post_mime_type;
  330. $post_data['guid'] = $post->guid;
  331. $post_data['ID'] = $post_ID;
  332. $updated[] = wp_update_post( $post_data );
  333. if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
  334. if ( 'sticky' == $post_data['sticky'] )
  335. stick_post( $post_ID );
  336. else
  337. unstick_post( $post_ID );
  338. }
  339. if ( isset( $post_data['post_format'] ) )
  340. set_post_format( $post_ID, $post_data['post_format'] );
  341. }
  342. return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked );
  343. }
  344. /**
  345. * Default post information to use when populating the "Write Post" form.
  346. *
  347. * @since 2.0.0
  348. *
  349. * @param string $post_type A post type string, defaults to 'post'.
  350. * @return WP_Post Post object containing all the default post data as attributes
  351. */
  352. function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
  353. global $wpdb;
  354. $post_title = '';
  355. if ( !empty( $_REQUEST['post_title'] ) )
  356. $post_title = esc_html( stripslashes( $_REQUEST['post_title'] ));
  357. $post_content = '';
  358. if ( !empty( $_REQUEST['content'] ) )
  359. $post_content = esc_html( stripslashes( $_REQUEST['content'] ));
  360. $post_excerpt = '';
  361. if ( !empty( $_REQUEST['excerpt'] ) )
  362. $post_excerpt = esc_html( stripslashes( $_REQUEST['excerpt'] ));
  363. if ( $create_in_db ) {
  364. $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
  365. $post = get_post( $post_id );
  366. if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
  367. set_post_format( $post, get_option( 'default_post_format' ) );
  368. } else {
  369. $post = new stdClass;
  370. $post->ID = 0;
  371. $post->post_author = '';
  372. $post->post_date = '';
  373. $post->post_date_gmt = '';
  374. $post->post_password = '';
  375. $post->post_type = $post_type;
  376. $post->post_status = 'draft';
  377. $post->to_ping = '';
  378. $post->pinged = '';
  379. $post->comment_status = get_option( 'default_comment_status' );
  380. $post->ping_status = get_option( 'default_ping_status' );
  381. $post->post_pingback = get_option( 'default_pingback_flag' );
  382. $post->post_category = get_option( 'default_category' );
  383. $post->page_template = 'default';
  384. $post->post_parent = 0;
  385. $post->menu_order = 0;
  386. $post = new WP_Post( $post );
  387. }
  388. $post->post_content = apply_filters( 'default_content', $post_content, $post );
  389. $post->post_title = apply_filters( 'default_title', $post_title, $post );
  390. $post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post );
  391. $post->post_name = '';
  392. return $post;
  393. }
  394. /**
  395. * Determine if a post exists based on title, content, and date
  396. *
  397. * @since 2.0.0
  398. *
  399. * @param string $title Post title
  400. * @param string $content Optional post content
  401. * @param string $date Optional post date
  402. * @return int Post ID if post exists, 0 otherwise.
  403. */
  404. function post_exists($title, $content = '', $date = '') {
  405. global $wpdb;
  406. $post_title = stripslashes( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
  407. $post_content = stripslashes( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
  408. $post_date = stripslashes( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
  409. $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
  410. $args = array();
  411. if ( !empty ( $date ) ) {
  412. $query .= ' AND post_date = %s';
  413. $args[] = $post_date;
  414. }
  415. if ( !empty ( $title ) ) {
  416. $query .= ' AND post_title = %s';
  417. $args[] = $post_title;
  418. }
  419. if ( !empty ( $content ) ) {
  420. $query .= 'AND post_content = %s';
  421. $args[] = $post_content;
  422. }
  423. if ( !empty ( $args ) )
  424. return $wpdb->get_var( $wpdb->prepare($query, $args) );
  425. return 0;
  426. }
  427. /**
  428. * Creates a new post from the "Write Post" form using $_POST information.
  429. *
  430. * @since 2.1.0
  431. *
  432. * @return unknown
  433. */
  434. function wp_write_post() {
  435. global $user_ID;
  436. if ( isset($_POST['post_type']) )
  437. $ptype = get_post_type_object($_POST['post_type']);
  438. else
  439. $ptype = get_post_type_object('post');
  440. if ( !current_user_can( $ptype->cap->edit_posts ) ) {
  441. if ( 'page' == $ptype->name )
  442. return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this site.' ) );
  443. else
  444. return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this site.' ) );
  445. }
  446. $_POST['post_mime_type'] = '';
  447. // Clear out any data in internal vars.
  448. unset( $_POST['filter'] );
  449. // Edit don't write if we have a post id.
  450. if ( isset( $_POST['post_ID'] ) )
  451. return edit_post();
  452. $translated = _wp_translate_postdata( false );
  453. if ( is_wp_error($translated) )
  454. return $translated;
  455. if ( isset($_POST['visibility']) ) {
  456. switch ( $_POST['visibility'] ) {
  457. case 'public' :
  458. $_POST['post_password'] = '';
  459. break;
  460. case 'password' :
  461. unset( $_POST['sticky'] );
  462. break;
  463. case 'private' :
  464. $_POST['post_status'] = 'private';
  465. $_POST['post_password'] = '';
  466. unset( $_POST['sticky'] );
  467. break;
  468. }
  469. }
  470. // Create the post.
  471. $post_ID = wp_insert_post( $_POST );
  472. if ( is_wp_error( $post_ID ) )
  473. return $post_ID;
  474. if ( empty($post_ID) )
  475. return 0;
  476. add_meta( $post_ID );
  477. add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
  478. // Now that we have an ID we can fix any attachment anchor hrefs
  479. _fix_attachment_links( $post_ID );
  480. wp_set_post_lock( $post_ID );
  481. return $post_ID;
  482. }
  483. /**
  484. * Calls wp_write_post() and handles the errors.
  485. *
  486. * @since 2.0.0
  487. * @uses wp_write_post()
  488. * @uses is_wp_error()
  489. * @uses wp_die()
  490. * @return unknown
  491. */
  492. function write_post() {
  493. $result = wp_write_post();
  494. if ( is_wp_error( $result ) )
  495. wp_die( $result->get_error_message() );
  496. else
  497. return $result;
  498. }
  499. //
  500. // Post Meta
  501. //
  502. /**
  503. * {@internal Missing Short Description}}
  504. *
  505. * @since 1.2.0
  506. *
  507. * @param unknown_type $post_ID
  508. * @return unknown
  509. */
  510. function add_meta( $post_ID ) {
  511. global $wpdb;
  512. $post_ID = (int) $post_ID;
  513. $metakeyselect = isset($_POST['metakeyselect']) ? stripslashes( trim( $_POST['metakeyselect'] ) ) : '';
  514. $metakeyinput = isset($_POST['metakeyinput']) ? stripslashes( trim( $_POST['metakeyinput'] ) ) : '';
  515. $metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : '';
  516. if ( is_string( $metavalue ) )
  517. $metavalue = trim( $metavalue );
  518. if ( ('0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput ) ) ) {
  519. // We have a key/value pair. If both the select and the
  520. // input for the key have data, the input takes precedence:
  521. if ( '#NONE#' != $metakeyselect )
  522. $metakey = $metakeyselect;
  523. if ( $metakeyinput )
  524. $metakey = $metakeyinput; // default
  525. if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) )
  526. return false;
  527. $metakey = esc_sql( $metakey );
  528. return add_post_meta( $post_ID, $metakey, $metavalue );
  529. }
  530. return false;
  531. } // add_meta
  532. /**
  533. * {@internal Missing Short Description}}
  534. *
  535. * @since 1.2.0
  536. *
  537. * @param unknown_type $mid
  538. * @return unknown
  539. */
  540. function delete_meta( $mid ) {
  541. return delete_metadata_by_mid( 'post' , $mid );
  542. }
  543. /**
  544. * Get a list of previously defined keys.
  545. *
  546. * @since 1.2.0
  547. *
  548. * @return unknown
  549. */
  550. function get_meta_keys() {
  551. global $wpdb;
  552. $keys = $wpdb->get_col( "
  553. SELECT meta_key
  554. FROM $wpdb->postmeta
  555. GROUP BY meta_key
  556. ORDER BY meta_key" );
  557. return $keys;
  558. }
  559. /**
  560. * {@internal Missing Short Description}}
  561. *
  562. * @since 2.1.0
  563. *
  564. * @param unknown_type $mid
  565. * @return unknown
  566. */
  567. function get_post_meta_by_id( $mid ) {
  568. return get_metadata_by_mid( 'post', $mid );
  569. }
  570. /**
  571. * {@internal Missing Short Description}}
  572. *
  573. * Some postmeta stuff.
  574. *
  575. * @since 1.2.0
  576. *
  577. * @param unknown_type $postid
  578. * @return unknown
  579. */
  580. function has_meta( $postid ) {
  581. global $wpdb;
  582. return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id
  583. FROM $wpdb->postmeta WHERE post_id = %d
  584. ORDER BY meta_key,meta_id", $postid), ARRAY_A );
  585. }
  586. /**
  587. * {@internal Missing Short Description}}
  588. *
  589. * @since 1.2.0
  590. *
  591. * @param unknown_type $meta_id
  592. * @param unknown_type $meta_key Expect Slashed
  593. * @param unknown_type $meta_value Expect Slashed
  594. * @return unknown
  595. */
  596. function update_meta( $meta_id, $meta_key, $meta_value ) {
  597. $meta_key = stripslashes( $meta_key );
  598. $meta_value = stripslashes_deep( $meta_value );
  599. return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key );
  600. }
  601. //
  602. // Private
  603. //
  604. /**
  605. * Replace hrefs of attachment anchors with up-to-date permalinks.
  606. *
  607. * @since 2.3.0
  608. * @access private
  609. *
  610. * @param unknown_type $post_ID
  611. * @return unknown
  612. */
  613. function _fix_attachment_links( $post_ID ) {
  614. $post = get_post( $post_ID, ARRAY_A );
  615. $content = $post['post_content'];
  616. // quick sanity check, don't run if no pretty permalinks or post is not published
  617. if ( !get_option('permalink_structure') || $post['post_status'] != 'publish' )
  618. return;
  619. // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero)
  620. if ( !strpos($content, '?attachment_id=') || !preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) )
  621. return;
  622. $site_url = get_bloginfo('url');
  623. $site_url = substr( $site_url, (int) strpos($site_url, '://') ); // remove the http(s)
  624. $replace = '';
  625. foreach ( $link_matches[1] as $key => $value ) {
  626. if ( !strpos($value, '?attachment_id=') || !strpos($value, 'wp-att-')
  627. || !preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match )
  628. || !preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) )
  629. continue;
  630. $quote = $url_match[1]; // the quote (single or double)
  631. $url_id = (int) $url_match[2];
  632. $rel_id = (int) $rel_match[1];
  633. if ( !$url_id || !$rel_id || $url_id != $rel_id || strpos($url_match[0], $site_url) === false )
  634. continue;
  635. $link = $link_matches[0][$key];
  636. $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );
  637. $content = str_replace( $link, $replace, $content );
  638. }
  639. if ( $replace ) {
  640. $post['post_content'] = $content;
  641. // Escape data pulled from DB.
  642. $post = add_magic_quotes($post);
  643. return wp_update_post($post);
  644. }
  645. }
  646. /**
  647. * Move child posts to a new parent.
  648. *
  649. * @since 2.3.0
  650. * @access private
  651. *
  652. * @param unknown_type $old_ID
  653. * @param unknown_type $new_ID
  654. * @return unknown
  655. */
  656. function _relocate_children( $old_ID, $new_ID ) {
  657. global $wpdb;
  658. $old_ID = (int) $old_ID;
  659. $new_ID = (int) $new_ID;
  660. $children = $wpdb->get_col( $wpdb->prepare("
  661. SELECT post_id
  662. FROM $wpdb->postmeta
  663. WHERE meta_key = '_wp_attachment_temp_parent'
  664. AND meta_value = %d", $old_ID) );
  665. foreach ( $children as $child_id ) {
  666. $wpdb->update($wpdb->posts, array('post_parent' => $new_ID), array('ID' => $child_id) );
  667. delete_post_meta($child_id, '_wp_attachment_temp_parent');
  668. }
  669. }
  670. /**
  671. * Get all the possible statuses for a post_type
  672. *
  673. * @since 2.5.0
  674. *
  675. * @param string $type The post_type you want the statuses for
  676. * @return array As array of all the statuses for the supplied post type
  677. */
  678. function get_available_post_statuses($type = 'post') {
  679. $stati = wp_count_posts($type);
  680. return array_keys(get_object_vars($stati));
  681. }
  682. /**
  683. * Run the wp query to fetch the posts for listing on the edit posts page
  684. *
  685. * @since 2.5.0
  686. *
  687. * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal.
  688. * @return array
  689. */
  690. function wp_edit_posts_query( $q = false ) {
  691. if ( false === $q )
  692. $q = $_GET;
  693. $q['m'] = isset($q['m']) ? (int) $q['m'] : 0;
  694. $q['cat'] = isset($q['cat']) ? (int) $q['cat'] : 0;
  695. $post_stati = get_post_stati();
  696. if ( isset($q['post_type']) && in_array( $q['post_type'], get_post_types() ) )
  697. $post_type = $q['post_type'];
  698. else
  699. $post_type = 'post';
  700. $avail_post_stati = get_available_post_statuses($post_type);
  701. if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) {
  702. $post_status = $q['post_status'];
  703. $perm = 'readable';
  704. }
  705. if ( isset($q['orderby']) )
  706. $orderby = $q['orderby'];
  707. elseif ( isset($q['post_status']) && in_array($q['post_status'], array('pending', 'draft')) )
  708. $orderby = 'modified';
  709. if ( isset($q['order']) )
  710. $order = $q['order'];
  711. elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] )
  712. $order = 'ASC';
  713. $per_page = 'edit_' . $post_type . '_per_page';
  714. $posts_per_page = (int) get_user_option( $per_page );
  715. if ( empty( $posts_per_page ) || $posts_per_page < 1 )
  716. $posts_per_page = 20;
  717. $posts_per_page = apply_filters( $per_page, $posts_per_page );
  718. $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type );
  719. $query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page');
  720. // Hierarchical types require special args.
  721. if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) {
  722. $query['orderby'] = 'menu_order title';
  723. $query['order'] = 'asc';
  724. $query['posts_per_page'] = -1;
  725. $query['posts_per_archive_page'] = -1;
  726. }
  727. if ( ! empty( $q['show_sticky'] ) )
  728. $query['post__in'] = (array) get_option( 'sticky_posts' );
  729. wp( $query );
  730. return $avail_post_stati;
  731. }
  732. /**
  733. * Get default post mime types
  734. *
  735. * @since 2.9.0
  736. *
  737. * @return array
  738. */
  739. function get_post_mime_types() {
  740. $post_mime_types = array( // array( adj, noun )
  741. 'image' => array(__('Images'), __('Manage Images'), _n_noop('Image <span class="count">(%s)</span>', 'Images <span class="count">(%s)</span>')),
  742. 'audio' => array(__('Audio'), __('Manage Audio'), _n_noop('Audio <span class="count">(%s)</span>', 'Audio <span class="count">(%s)</span>')),
  743. 'video' => array(__('Video'), __('Manage Video'), _n_noop('Video <span class="count">(%s)</span>', 'Video <span class="count">(%s)</span>')),
  744. );
  745. return apply_filters('post_mime_types', $post_mime_types);
  746. }
  747. /**
  748. * {@internal Missing Short Description}}
  749. *
  750. * @since 2.5.0
  751. *
  752. * @param unknown_type $type
  753. * @return unknown
  754. */
  755. function get_available_post_mime_types($type = 'attachment') {
  756. global $wpdb;
  757. $types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type));
  758. return $types;
  759. }
  760. /**
  761. * Executes a query for attachments. An array of WP_Query arguments
  762. * can be passed in, which will override the arguments set by this function.
  763. *
  764. * @since 2.5.0
  765. * @uses apply_filters() Calls 'upload_per_page' on posts_per_page argument
  766. *
  767. * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal.
  768. * @return array
  769. */
  770. function wp_edit_attachments_query( $q = false ) {
  771. if ( false === $q )
  772. $q = $_GET;
  773. $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0;
  774. $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
  775. $q['post_type'] = 'attachment';
  776. $post_type = get_post_type_object( 'attachment' );
  777. $states = 'inherit';
  778. if ( current_user_can( $post_type->cap->read_private_posts ) )
  779. $states .= ',private';
  780. $q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states;
  781. $media_per_page = (int) get_user_option( 'upload_per_page' );
  782. if ( empty( $media_per_page ) || $media_per_page < 1 )
  783. $media_per_page = 20;
  784. $q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page );
  785. $post_mime_types = get_post_mime_types();
  786. $avail_post_mime_types = get_available_post_mime_types('attachment');
  787. if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) )
  788. unset($q['post_mime_type']);
  789. if ( isset($q['detached']) )
  790. add_filter('posts_where', '_edit_attachments_query_helper');
  791. wp( $q );
  792. if ( isset($q['detached']) )
  793. remove_filter('posts_where', '_edit_attachments_query_helper');
  794. return array($post_mime_types, $avail_post_mime_types);
  795. }
  796. function _edit_attachments_query_helper($where) {
  797. global $wpdb;
  798. return $where .= " AND {$wpdb->posts}.post_parent < 1";
  799. }
  800. /**
  801. * Returns the list of classes to be used by a metabox
  802. *
  803. * @uses get_user_option()
  804. * @since 2.5.0
  805. *
  806. * @param unknown_type $id
  807. * @param unknown_type $page
  808. * @return unknown
  809. */
  810. function postbox_classes( $id, $page ) {
  811. if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) {
  812. $classes = array( '' );
  813. } elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) {
  814. if ( !is_array( $closed ) ) {
  815. $classes = array( '' );
  816. } else {
  817. $classes = in_array( $id, $closed ) ? array( 'closed' ) : array( '' );
  818. }
  819. } else {
  820. $classes = array( '' );
  821. }
  822. $classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes );
  823. return implode( ' ', $classes );
  824. }
  825. /**
  826. * {@internal Missing Short Description}}
  827. *
  828. * @since 2.5.0
  829. *
  830. * @param int|object $id Post ID or post object.
  831. * @param string $title (optional) Title
  832. * @param string $name (optional) Name
  833. * @return array With two entries of type string
  834. */
  835. function get_sample_permalink($id, $title = null, $name = null) {
  836. $post = get_post($id);
  837. if ( !$post->ID )
  838. return array('', '');
  839. $ptype = get_post_type_object($post->post_type);
  840. $original_status = $post->post_status;
  841. $original_date = $post->post_date;
  842. $original_name = $post->post_name;
  843. // Hack: get_permalink would return ugly permalink for
  844. // drafts, so we will fake, that our post is published
  845. if ( in_array($post->post_status, array('draft', 'pending')) ) {
  846. $post->post_status = 'publish';
  847. $post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
  848. }
  849. // If the user wants to set a new name -- override the current one
  850. // Note: if empty name is supplied -- use the title instead, see #6072
  851. if ( !is_null($name) )
  852. $post->post_name = sanitize_title($name ? $name : $title, $post->ID);
  853. $post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent);
  854. $post->filter = 'sample';
  855. $permalink = get_permalink($post, true);
  856. // Replace custom post_type Token with generic pagename token for ease of use.
  857. $permalink = str_replace("%$post->post_type%", '%pagename%', $permalink);
  858. // Handle page hierarchy
  859. if ( $ptype->hierarchical ) {
  860. $uri = get_page_uri($post);
  861. $uri = untrailingslashit($uri);
  862. $uri = strrev( stristr( strrev( $uri ), '/' ) );
  863. $uri = untrailingslashit($uri);
  864. $uri = apply_filters( 'editable_slug', $uri );
  865. if ( !empty($uri) )
  866. $uri .= '/';
  867. $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
  868. }
  869. $permalink = array($permalink, apply_filters('editable_slug', $post->post_name));
  870. $post->post_status = $original_status;
  871. $post->post_date = $original_date;
  872. $post->post_name = $original_name;
  873. unset($post->filter);
  874. return $permalink;
  875. }
  876. /**
  877. * sample permalink html
  878. *
  879. * intended to be used for the inplace editor of the permalink post slug on in the post (and page?) editor.
  880. *
  881. * @since 2.5.0
  882. *
  883. * @param int|object $id Post ID or post object.
  884. * @param string $new_title (optional) New title
  885. * @param string $new_slug (optional) New slug
  886. * @return string intended to be used for the inplace editor of the permalink post slug on in the post (and page?) editor.
  887. */
  888. function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
  889. global $wpdb;
  890. $post = get_post($id);
  891. list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
  892. if ( 'publish' == get_post_status( $post ) ) {
  893. $ptype = get_post_type_object($post->post_type);
  894. $view_post = $ptype->labels->view_item;
  895. $title = __('Click to edit this part of the permalink');
  896. } else {
  897. $title = __('Temporary permalink. Click to edit this part.');
  898. }
  899. if ( false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%') ) {
  900. $return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink" tabindex="-1">' . $permalink . "</span>\n";
  901. if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) )
  902. $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
  903. if ( isset($view_post) )
  904. $return .= "<span id='view-post-btn'><a href='$permalink' class='button button-small'>$view_post</a></span>\n";
  905. $return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
  906. return $return;
  907. }
  908. if ( function_exists('mb_strlen') ) {
  909. if ( mb_strlen($post_name) > 30 ) {
  910. $post_name_abridged = mb_substr($post_name, 0, 14). '&hellip;' . mb_substr($post_name, -14);
  911. } else {
  912. $post_name_abridged = $post_name;
  913. }
  914. } else {
  915. if ( strlen($post_name) > 30 ) {
  916. $post_name_abridged = substr($post_name, 0, 14). '&hellip;' . substr($post_name, -14);
  917. } else {
  918. $post_name_abridged = $post_name;
  919. }
  920. }
  921. $post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>';
  922. $display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink);
  923. $view_link = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
  924. $return = '<strong>' . __('Permalink:') . "</strong>\n";
  925. $return .= '<span id="sample-permalink" tabindex="-1">' . $display_link . "</span>\n";
  926. $return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
  927. $return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug button button-small hide-if-no-js" onclick="editPermalink(' . $id . '); return false;">' . __('Edit') . "</a></span>\n";
  928. $return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n";
  929. if ( isset($view_post) )
  930. $return .= "<span id='view-post-btn'><a href='$view_link' class='button button-small'>$view_post</a></span>\n";
  931. $return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
  932. return $return;
  933. }
  934. /**
  935. * Output HTML for the post thumbnail meta-box.
  936. *
  937. * @since 2.9.0
  938. *
  939. * @param int $thumbnail_id ID of the attachment used for thumbnail
  940. * @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post.
  941. * @return string html
  942. */
  943. function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
  944. global $content_width, $_wp_additional_image_sizes;
  945. $post = get_post( $post );
  946. $upload_iframe_src = esc_url( get_upload_iframe_src('image', $post->ID ) );
  947. $set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>';
  948. $content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set featured image' ) );
  949. if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
  950. $old_content_width = $content_width;
  951. $content_width = 266;
  952. if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) )
  953. $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
  954. else
  955. $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' );
  956. if ( !empty( $thumbnail_html ) ) {
  957. $ajax_nonce = wp_create_nonce( 'set_post_thumbnail-' . $post->ID );
  958. $content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
  959. $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a></p>';
  960. }
  961. $content_width = $old_content_width;
  962. }
  963. return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID );
  964. }
  965. /**
  966. * Check to see if the post is currently being edited by another user.
  967. *
  968. * @since 2.5.0
  969. *
  970. * @param int $post_id ID of the post to check for editing
  971. * @return bool|int False: not locked or locked by current user. Int: user ID of user with lock.
  972. */
  973. function wp_check_post_lock( $post_id ) {
  974. if ( !$post = get_post( $post_id ) )
  975. return false;
  976. if ( !$lock = get_post_meta( $post->ID, '_edit_lock', true ) )
  977. return false;
  978. $lock = explode( ':', $lock );
  979. $time = $lock[0];
  980. $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
  981. $time_window = apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 );
  982. if ( $time && $time > time() - $time_window && $user != get_current_user_id() )
  983. return $user;
  984. return false;
  985. }
  986. /**
  987. * Mark the post as currently being edited by the current user
  988. *
  989. * @since 2.5.0
  990. *
  991. * @param int $post_id ID of the post to being edited
  992. * @return bool|array Returns false if the post doesn't exist of there is no current user, or
  993. * an array of the lock time and the user ID.
  994. */
  995. function wp_set_post_lock( $post_id ) {
  996. if ( !$post = get_post( $post_id ) )
  997. return false;
  998. if ( 0 == ($user_id = get_current_user_id()) )
  999. return false;
  1000. $now = time();
  1001. $lock = "$now:$user_id";
  1002. update_post_meta( $post->ID, '_edit_lock', $lock );
  1003. return array( $now, $user_id );
  1004. }
  1005. /**
  1006. * Outputs the notice message to say that someone else is editing this post at the moment.
  1007. *
  1008. * @since 2.8.5
  1009. * @return none
  1010. */
  1011. function _admin_notice_post_locked() {
  1012. $post = get_post();
  1013. $lock = explode( ':', get_post_meta( $post->ID, '_edit_lock', true ) );
  1014. $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
  1015. $last_user = get_userdata( $user );
  1016. $last_user_name = $last_user ? $last_user->display_name : __('Somebody');
  1017. switch ($post->post_type) {
  1018. case 'post':
  1019. $message = __( 'Warning: %s is currently editing this post' );
  1020. break;
  1021. case 'page':
  1022. $message = __( 'Warning: %s is currently editing this page' );
  1023. break;
  1024. default:
  1025. $message = __( 'Warning: %s is currently editing this.' );
  1026. }
  1027. $message = sprintf( $message, esc_html( $last_user_name ) );
  1028. echo "<div class='error'><p>$message</p></div>";
  1029. }
  1030. /**
  1031. * Creates autosave data for the specified post from $_POST data.
  1032. *
  1033. * @package WordPress
  1034. * @subpackage Post_Revisions
  1035. * @since 2.6.0
  1036. *
  1037. * @uses _wp_translate_postdata()
  1038. * @uses _wp_post_revision_fields()
  1039. *
  1040. * @return unknown
  1041. */
  1042. function wp_create_post_autosave( $post_id ) {
  1043. $translated = _wp_translate_postdata( true );
  1044. if ( is_wp_error( $translated ) )
  1045. return $translated;
  1046. // Only store one autosave. If there is already an autosave, overwrite it.
  1047. if ( $old_autosave = wp_get_post_autosave( $post_id ) ) {
  1048. $new_autosave = _wp_post_revision_fields( $_POST, true );
  1049. $new_autosave['ID'] = $old_autosave->ID;
  1050. $new_autosave['post_author'] = get_current_user_id();
  1051. return wp_update_post( $new_autosave );
  1052. }
  1053. // _wp_put_post_revision() expects unescaped.
  1054. $_POST = stripslashes_deep($_POST);
  1055. // Otherwise create the new autosave as a special post revision
  1056. return _wp_put_post_revision( $_POST, true );
  1057. }
  1058. /**
  1059. * Save draft or manually autosave for showing preview.
  1060. *
  1061. * @package WordPress
  1062. * @since 2.7.0
  1063. *
  1064. * @uses get_post_status()
  1065. * @uses edit_post()
  1066. * @uses get_post()
  1067. * @uses current_user_can()
  1068. * @uses wp_die()
  1069. * @uses wp_create_post_autosave()
  1070. * @uses add_query_arg()
  1071. * @uses wp_create_nonce()
  1072. *
  1073. * @return str URL to redirect to show the preview
  1074. */
  1075. function post_preview() {
  1076. $post_ID = (int) $_POST['post_ID'];
  1077. $status = get_post_status( $post_ID );
  1078. if ( 'auto-draft' == $status )
  1079. wp_die( __('Preview not available. Please save as a draft first.') );
  1080. if ( isset($_POST['catslist']) )
  1081. $_POST['post_category'] = explode(",", $_POST['catslist']);
  1082. if ( isset($_POST['tags_input']) )
  1083. $_POST['tags_input'] = explode(",", $_POST['tags_input']);
  1084. if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
  1085. unset($_POST['post_category']);
  1086. $_POST['ID'] = $post_ID;
  1087. $post = get_post($post_ID);
  1088. if ( 'page' == $post->post_type ) {
  1089. if ( !current_user_can('edit_page', $post_ID) )
  1090. wp_die(__('You are not allowed to edit this page.'));
  1091. } else {
  1092. if ( !current_user_can('edit_post', $post_ID) )
  1093. wp_die(__('You are not allowed to edit this post.'));
  1094. }
  1095. if ( 'draft' == $post->post_status ) {
  1096. $id = edit_post();
  1097. } else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
  1098. $id = wp_create_post_autosave( $post->ID );
  1099. if ( ! is_wp_error($id) )
  1100. $id = $post->ID;
  1101. }
  1102. if ( is_wp_error($id) )
  1103. wp_die( $id->get_error_message() );
  1104. if ( $_POST['post_status'] == 'draft' ) {
  1105. $url = add_query_arg( 'preview', 'true', get_permalink($id) );
  1106. } else {
  1107. $nonce = wp_create_nonce('post_preview_' . $id);
  1108. $url = add_query_arg( array( 'preview' => 'true', 'preview_id' => $id, 'preview_nonce' => $nonce ), get_permalink($id) );
  1109. }
  1110. return $url;
  1111. }