PageRenderTime 67ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/post.php

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