PageRenderTime 56ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/post.php

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