PageRenderTime 57ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/blog/wp-admin/includes/post.php

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