/wp-includes/post.php

  1. <?php 
  2. /** 
  3. * Core Post API 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Post 
  7. */ 
  8.  
  9. // 
  10. // Post Type Registration 
  11. // 
  12.   
  13. /** 
  14. * Creates the initial post types when 'init' action is fired. 
  15. * 
  16. * See {@see 'init'}. 
  17. * 
  18. * @since 2.9.0 
  19. */ 
  20. function create_initial_post_types() { 
  21. register_post_type( 'post', array( 
  22. 'labels' => array( 
  23. 'name_admin_bar' => _x( 'Post', 'add new from admin bar' ),  
  24. ),  
  25. 'public' => true,  
  26. '_builtin' => true, /** internal use only. don't use this when registering your own post type. */ 
  27. '_edit_link' => 'post.php?post=%d', /** internal use only. don't use this when registering your own post type. */ 
  28. 'capability_type' => 'post',  
  29. 'map_meta_cap' => true,  
  30. 'menu_position' => 5,  
  31. 'hierarchical' => false,  
  32. 'rewrite' => false,  
  33. 'query_var' => false,  
  34. 'delete_with_user' => true,  
  35. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),  
  36. 'show_in_rest' => true,  
  37. 'rest_base' => 'posts',  
  38. 'rest_controller_class' => 'WP_REST_Posts_Controller',  
  39. ) ); 
  40.  
  41. register_post_type( 'page', array( 
  42. 'labels' => array( 
  43. 'name_admin_bar' => _x( 'Page', 'add new from admin bar' ),  
  44. ),  
  45. 'public' => true,  
  46. 'publicly_queryable' => false,  
  47. '_builtin' => true, /** internal use only. don't use this when registering your own post type. */ 
  48. '_edit_link' => 'post.php?post=%d', /** internal use only. don't use this when registering your own post type. */ 
  49. 'capability_type' => 'page',  
  50. 'map_meta_cap' => true,  
  51. 'menu_position' => 20,  
  52. 'hierarchical' => true,  
  53. 'rewrite' => false,  
  54. 'query_var' => false,  
  55. 'delete_with_user' => true,  
  56. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),  
  57. 'show_in_rest' => true,  
  58. 'rest_base' => 'pages',  
  59. 'rest_controller_class' => 'WP_REST_Posts_Controller',  
  60. ) ); 
  61.  
  62. register_post_type( 'attachment', array( 
  63. 'labels' => array( 
  64. 'name' => _x('Media', 'post type general name'),  
  65. 'name_admin_bar' => _x( 'Media', 'add new from admin bar' ),  
  66. 'add_new' => _x( 'Add New', 'add new media' ),  
  67. 'edit_item' => __( 'Edit Media' ),  
  68. 'view_item' => __( 'View Attachment Page' ),  
  69. 'attributes' => __( 'Attachment Attributes' ),  
  70. ),  
  71. 'public' => true,  
  72. 'show_ui' => true,  
  73. '_builtin' => true, /** internal use only. don't use this when registering your own post type. */ 
  74. '_edit_link' => 'post.php?post=%d', /** internal use only. don't use this when registering your own post type. */ 
  75. 'capability_type' => 'post',  
  76. 'capabilities' => array( 
  77. 'create_posts' => 'upload_files',  
  78. ),  
  79. 'map_meta_cap' => true,  
  80. 'hierarchical' => false,  
  81. 'rewrite' => false,  
  82. 'query_var' => false,  
  83. 'show_in_nav_menus' => false,  
  84. 'delete_with_user' => true,  
  85. 'supports' => array( 'title', 'author', 'comments' ),  
  86. 'show_in_rest' => true,  
  87. 'rest_base' => 'media',  
  88. 'rest_controller_class' => 'WP_REST_Attachments_Controller',  
  89. ) ); 
  90. add_post_type_support( 'attachment:audio', 'thumbnail' ); 
  91. add_post_type_support( 'attachment:video', 'thumbnail' ); 
  92.  
  93. register_post_type( 'revision', array( 
  94. 'labels' => array( 
  95. 'name' => __( 'Revisions' ),  
  96. 'singular_name' => __( 'Revision' ),  
  97. ),  
  98. 'public' => false,  
  99. '_builtin' => true, /** internal use only. don't use this when registering your own post type. */ 
  100. '_edit_link' => 'revision.php?revision=%d', /** internal use only. don't use this when registering your own post type. */ 
  101. 'capability_type' => 'post',  
  102. 'map_meta_cap' => true,  
  103. 'hierarchical' => false,  
  104. 'rewrite' => false,  
  105. 'query_var' => false,  
  106. 'can_export' => false,  
  107. 'delete_with_user' => true,  
  108. 'supports' => array( 'author' ),  
  109. ) ); 
  110.  
  111. register_post_type( 'nav_menu_item', array( 
  112. 'labels' => array( 
  113. 'name' => __( 'Navigation Menu Items' ),  
  114. 'singular_name' => __( 'Navigation Menu Item' ),  
  115. ),  
  116. 'public' => false,  
  117. '_builtin' => true, /** internal use only. don't use this when registering your own post type. */ 
  118. 'hierarchical' => false,  
  119. 'rewrite' => false,  
  120. 'delete_with_user' => false,  
  121. 'query_var' => false,  
  122. ) ); 
  123.  
  124. register_post_type( 'custom_css', array( 
  125. 'labels' => array( 
  126. 'name' => __( 'Custom CSS' ),  
  127. 'singular_name' => __( 'Custom CSS' ),  
  128. ),  
  129. 'public' => false,  
  130. 'hierarchical' => false,  
  131. 'rewrite' => false,  
  132. 'query_var' => false,  
  133. 'delete_with_user' => false,  
  134. 'can_export' => true,  
  135. '_builtin' => true, /** internal use only. don't use this when registering your own post type. */ 
  136. 'supports' => array( 'title', 'revisions' ),  
  137. 'capabilities' => array( 
  138. 'delete_posts' => 'edit_theme_options',  
  139. 'delete_post' => 'edit_theme_options',  
  140. 'delete_published_posts' => 'edit_theme_options',  
  141. 'delete_private_posts' => 'edit_theme_options',  
  142. 'delete_others_posts' => 'edit_theme_options',  
  143. 'edit_post' => 'edit_css',  
  144. 'edit_posts' => 'edit_css',  
  145. 'edit_others_posts' => 'edit_css',  
  146. 'edit_published_posts' => 'edit_css',  
  147. 'read_post' => 'read',  
  148. 'read_private_posts' => 'read',  
  149. 'publish_posts' => 'edit_theme_options',  
  150. ),  
  151. ) ); 
  152.  
  153. register_post_type( 'customize_changeset', array( 
  154. 'labels' => array( 
  155. 'name' => _x( 'Changesets', 'post type general name' ),  
  156. 'singular_name' => _x( 'Changeset', 'post type singular name' ),  
  157. 'menu_name' => _x( 'Changesets', 'admin menu' ),  
  158. 'name_admin_bar' => _x( 'Changeset', 'add new on admin bar' ),  
  159. 'add_new' => _x( 'Add New', 'Customize Changeset' ),  
  160. 'add_new_item' => __( 'Add New Changeset' ),  
  161. 'new_item' => __( 'New Changeset' ),  
  162. 'edit_item' => __( 'Edit Changeset' ),  
  163. 'view_item' => __( 'View Changeset' ),  
  164. 'all_items' => __( 'All Changesets' ),  
  165. 'search_items' => __( 'Search Changesets' ),  
  166. 'not_found' => __( 'No changesets found.' ),  
  167. 'not_found_in_trash' => __( 'No changesets found in Trash.' ),  
  168. ),  
  169. 'public' => false,  
  170. '_builtin' => true, /** internal use only. don't use this when registering your own post type. */ 
  171. 'map_meta_cap' => true,  
  172. 'hierarchical' => false,  
  173. 'rewrite' => false,  
  174. 'query_var' => false,  
  175. 'can_export' => false,  
  176. 'delete_with_user' => false,  
  177. 'supports' => array( 'title', 'author' ),  
  178. 'capability_type' => 'customize_changeset',  
  179. 'capabilities' => array( 
  180. 'create_posts' => 'customize',  
  181. 'delete_others_posts' => 'customize',  
  182. 'delete_post' => 'customize',  
  183. 'delete_posts' => 'customize',  
  184. 'delete_private_posts' => 'customize',  
  185. 'delete_published_posts' => 'customize',  
  186. 'edit_others_posts' => 'customize',  
  187. 'edit_post' => 'customize',  
  188. 'edit_posts' => 'customize',  
  189. 'edit_private_posts' => 'customize',  
  190. 'edit_published_posts' => 'do_not_allow',  
  191. 'publish_posts' => 'customize',  
  192. 'read' => 'read',  
  193. 'read_post' => 'customize',  
  194. 'read_private_posts' => 'customize',  
  195. ),  
  196. ) ); 
  197.  
  198. register_post_status( 'publish', array( 
  199. 'label' => _x( 'Published', 'post status' ),  
  200. 'public' => true,  
  201. '_builtin' => true, /** internal use only. */ 
  202. 'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ),  
  203. ) ); 
  204.  
  205. register_post_status( 'future', array( 
  206. 'label' => _x( 'Scheduled', 'post status' ),  
  207. 'protected' => true,  
  208. '_builtin' => true, /** internal use only. */ 
  209. 'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ),  
  210. ) ); 
  211.  
  212. register_post_status( 'draft', array( 
  213. 'label' => _x( 'Draft', 'post status' ),  
  214. 'protected' => true,  
  215. '_builtin' => true, /** internal use only. */ 
  216. 'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ),  
  217. ) ); 
  218.  
  219. register_post_status( 'pending', array( 
  220. 'label' => _x( 'Pending', 'post status' ),  
  221. 'protected' => true,  
  222. '_builtin' => true, /** internal use only. */ 
  223. 'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ),  
  224. ) ); 
  225.  
  226. register_post_status( 'private', array( 
  227. 'label' => _x( 'Private', 'post status' ),  
  228. 'private' => true,  
  229. '_builtin' => true, /** internal use only. */ 
  230. 'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ),  
  231. ) ); 
  232.  
  233. register_post_status( 'trash', array( 
  234. 'label' => _x( 'Trash', 'post status' ),  
  235. 'internal' => true,  
  236. '_builtin' => true, /** internal use only. */ 
  237. 'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ),  
  238. 'show_in_admin_status_list' => true,  
  239. ) ); 
  240.  
  241. register_post_status( 'auto-draft', array( 
  242. 'label' => 'auto-draft',  
  243. 'internal' => true,  
  244. '_builtin' => true, /** internal use only. */ 
  245. ) ); 
  246.  
  247. register_post_status( 'inherit', array( 
  248. 'label' => 'inherit',  
  249. 'internal' => true,  
  250. '_builtin' => true, /** internal use only. */ 
  251. 'exclude_from_search' => false,  
  252. ) ); 
  253.  
  254. /** 
  255. * Retrieve attached file path based on attachment ID. 
  256. * 
  257. * By default the path will go through the 'get_attached_file' filter, but 
  258. * passing a true to the $unfiltered argument of get_attached_file() will 
  259. * return the file path unfiltered. 
  260. * 
  261. * The function works by getting the single post meta name, named 
  262. * '_wp_attached_file' and returning it. This is a convenience function to 
  263. * prevent looking up the meta name and provide a mechanism for sending the 
  264. * attached filename through a filter. 
  265. * 
  266. * @since 2.0.0 
  267. * 
  268. * @param int $attachment_id Attachment ID. 
  269. * @param bool $unfiltered Optional. Whether to apply filters. Default false. 
  270. * @return string|false The file path to where the attached file should be, false otherwise. 
  271. */ 
  272. function get_attached_file( $attachment_id, $unfiltered = false ) { 
  273. $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); 
  274.  
  275. // If the file is relative, prepend upload dir. 
  276. if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) && ( ( $uploads = wp_get_upload_dir() ) && false === $uploads['error'] ) ) { 
  277. $file = $uploads['basedir'] . "/$file"; 
  278.  
  279. if ( $unfiltered ) { 
  280. return $file; 
  281.  
  282. /** 
  283. * Filters the attached file based on the given ID. 
  284. * 
  285. * @since 2.1.0 
  286. * 
  287. * @param string $file Path to attached file. 
  288. * @param int $attachment_id Attachment ID. 
  289. */ 
  290. return apply_filters( 'get_attached_file', $file, $attachment_id ); 
  291.  
  292. /** 
  293. * Update attachment file path based on attachment ID. 
  294. * 
  295. * Used to update the file path of the attachment, which uses post meta name 
  296. * '_wp_attached_file' to store the path of the attachment. 
  297. * 
  298. * @since 2.1.0 
  299. * 
  300. * @param int $attachment_id Attachment ID. 
  301. * @param string $file File path for the attachment. 
  302. * @return bool True on success, false on failure. 
  303. */ 
  304. function update_attached_file( $attachment_id, $file ) { 
  305. if ( !get_post( $attachment_id ) ) 
  306. return false; 
  307.  
  308. /** 
  309. * Filters the path to the attached file to update. 
  310. * 
  311. * @since 2.1.0 
  312. * 
  313. * @param string $file Path to the attached file to update. 
  314. * @param int $attachment_id Attachment ID. 
  315. */ 
  316. $file = apply_filters( 'update_attached_file', $file, $attachment_id ); 
  317.  
  318. if ( $file = _wp_relative_upload_path( $file ) ) 
  319. return update_post_meta( $attachment_id, '_wp_attached_file', $file ); 
  320. else 
  321. return delete_post_meta( $attachment_id, '_wp_attached_file' ); 
  322.  
  323. /** 
  324. * Return relative path to an uploaded file. 
  325. * 
  326. * The path is relative to the current upload dir. 
  327. * 
  328. * @since 2.9.0 
  329. * 
  330. * @param string $path Full path to the file. 
  331. * @return string Relative path on success, unchanged path on failure. 
  332. */ 
  333. function _wp_relative_upload_path( $path ) { 
  334. $new_path = $path; 
  335.  
  336. $uploads = wp_get_upload_dir(); 
  337. if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) { 
  338. $new_path = str_replace( $uploads['basedir'], '', $new_path ); 
  339. $new_path = ltrim( $new_path, '/' ); 
  340.  
  341. /** 
  342. * Filters the relative path to an uploaded file. 
  343. * 
  344. * @since 2.9.0 
  345. * 
  346. * @param string $new_path Relative path to the file. 
  347. * @param string $path Full path to the file. 
  348. */ 
  349. return apply_filters( '_wp_relative_upload_path', $new_path, $path ); 
  350.  
  351. /** 
  352. * Retrieve all children of the post parent ID. 
  353. * 
  354. * Normally, without any enhancements, the children would apply to pages. In the 
  355. * context of the inner workings of WordPress, pages, posts, and attachments 
  356. * share the same table, so therefore the functionality could apply to any one 
  357. * of them. It is then noted that while this function does not work on posts, it 
  358. * does not mean that it won't work on posts. It is recommended that you know 
  359. * what context you wish to retrieve the children of. 
  360. * 
  361. * Attachments may also be made the child of a post, so if that is an accurate 
  362. * statement (which needs to be verified), it would then be possible to get 
  363. * all of the attachments for a post. Attachments have since changed since 
  364. * version 2.5, so this is most likely inaccurate, but serves generally as an 
  365. * example of what is possible. 
  366. * 
  367. * The arguments listed as defaults are for this function and also of the 
  368. * get_posts() function. The arguments are combined with the get_children defaults 
  369. * and are then passed to the get_posts() function, which accepts additional arguments. 
  370. * You can replace the defaults in this function, listed below and the additional 
  371. * arguments listed in the get_posts() function. 
  372. * 
  373. * The 'post_parent' is the most important argument and important attention 
  374. * needs to be paid to the $args parameter. If you pass either an object or an 
  375. * integer (number), then just the 'post_parent' is grabbed and everything else 
  376. * is lost. If you don't specify any arguments, then it is assumed that you are 
  377. * in The Loop and the post parent will be grabbed for from the current post. 
  378. * 
  379. * The 'post_parent' argument is the ID to get the children. The 'numberposts' 
  380. * is the amount of posts to retrieve that has a default of '-1', which is 
  381. * used to get all of the posts. Giving a number higher than 0 will only 
  382. * retrieve that amount of posts. 
  383. * 
  384. * The 'post_type' and 'post_status' arguments can be used to choose what 
  385. * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress 
  386. * post types are 'post', 'pages', and 'attachments'. The 'post_status' 
  387. * argument will accept any post status within the write administration panels. 
  388. * 
  389. * @since 2.0.0 
  390. * 
  391. * @see get_posts() 
  392. * @todo Check validity of description. 
  393. * 
  394. * @global WP_Post $post 
  395. * 
  396. * @param mixed $args Optional. User defined arguments for replacing the defaults. Default empty. 
  397. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to 
  398. * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. 
  399. * @return array Array of children, where the type of each element is determined by $output parameter. 
  400. * Empty array on failure. 
  401. */ 
  402. function get_children( $args = '', $output = OBJECT ) { 
  403. $kids = array(); 
  404. if ( empty( $args ) ) { 
  405. if ( isset( $GLOBALS['post'] ) ) { 
  406. $args = array('post_parent' => (int) $GLOBALS['post']->post_parent ); 
  407. } else { 
  408. return $kids; 
  409. } elseif ( is_object( $args ) ) { 
  410. $args = array('post_parent' => (int) $args->post_parent ); 
  411. } elseif ( is_numeric( $args ) ) { 
  412. $args = array('post_parent' => (int) $args); 
  413.  
  414. $defaults = array( 
  415. 'numberposts' => -1, 'post_type' => 'any',  
  416. 'post_status' => 'any', 'post_parent' => 0,  
  417. ); 
  418.  
  419. $r = wp_parse_args( $args, $defaults ); 
  420.  
  421. $children = get_posts( $r ); 
  422.  
  423. if ( ! $children ) 
  424. return $kids; 
  425.  
  426. if ( ! empty( $r['fields'] ) ) 
  427. return $children; 
  428.  
  429. update_post_cache($children); 
  430.  
  431. foreach ( $children as $key => $child ) 
  432. $kids[$child->ID] = $children[$key]; 
  433.  
  434. if ( $output == OBJECT ) { 
  435. return $kids; 
  436. } elseif ( $output == ARRAY_A ) { 
  437. $weeuns = array(); 
  438. foreach ( (array) $kids as $kid ) { 
  439. $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]); 
  440. return $weeuns; 
  441. } elseif ( $output == ARRAY_N ) { 
  442. $babes = array(); 
  443. foreach ( (array) $kids as $kid ) { 
  444. $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID])); 
  445. return $babes; 
  446. } else { 
  447. return $kids; 
  448.  
  449. /** 
  450. * Get extended entry info (<!--more-->). 
  451. * 
  452. * There should not be any space after the second dash and before the word 
  453. * 'more'. There can be text or space(s) after the word 'more', but won't be 
  454. * referenced. 
  455. * 
  456. * The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before 
  457. * the `<!--more-->`. The 'extended' key has the content after the 
  458. * `<!--more-->` comment. The 'more_text' key has the custom "Read More" text. 
  459. * 
  460. * @since 1.0.0 
  461. * 
  462. * @param string $post Post content. 
  463. * @return array Post before ('main'), after ('extended'), and custom read more ('more_text'). 
  464. */ 
  465. function get_extended( $post ) { 
  466. //Match the new style more links. 
  467. if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) { 
  468. list($main, $extended) = explode($matches[0], $post, 2); 
  469. $more_text = $matches[1]; 
  470. } else { 
  471. $main = $post; 
  472. $extended = ''; 
  473. $more_text = ''; 
  474.  
  475. // leading and trailing whitespace. 
  476. $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main); 
  477. $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended); 
  478. $more_text = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $more_text); 
  479.  
  480. return array( 'main' => $main, 'extended' => $extended, 'more_text' => $more_text ); 
  481.  
  482. /** 
  483. * Retrieves post data given a post ID or post object. 
  484. * 
  485. * See sanitize_post() for optional $filter values. Also, the parameter 
  486. * `$post`, must be given as a variable, since it is passed by reference. 
  487. * 
  488. * @since 1.5.1 
  489. * 
  490. * @global WP_Post $post 
  491. * 
  492. * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. 
  493. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to 
  494. * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. 
  495. * @param string $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',  
  496. * or 'display'. Default 'raw'. 
  497. * @return WP_Post|array|null Type corresponding to $output on success or null on failure. 
  498. * When $output is OBJECT, a `WP_Post` instance is returned. 
  499. */ 
  500. function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) { 
  501. if ( empty( $post ) && isset( $GLOBALS['post'] ) ) 
  502. $post = $GLOBALS['post']; 
  503.  
  504. if ( $post instanceof WP_Post ) { 
  505. $_post = $post; 
  506. } elseif ( is_object( $post ) ) { 
  507. if ( empty( $post->filter ) ) { 
  508. $_post = sanitize_post( $post, 'raw' ); 
  509. $_post = new WP_Post( $_post ); 
  510. } elseif ( 'raw' == $post->filter ) { 
  511. $_post = new WP_Post( $post ); 
  512. } else { 
  513. $_post = WP_Post::get_instance( $post->ID ); 
  514. } else { 
  515. $_post = WP_Post::get_instance( $post ); 
  516.  
  517. if ( ! $_post ) 
  518. return null; 
  519.  
  520. $_post = $_post->filter( $filter ); 
  521.  
  522. if ( $output == ARRAY_A ) 
  523. return $_post->to_array(); 
  524. elseif ( $output == ARRAY_N ) 
  525. return array_values( $_post->to_array() ); 
  526.  
  527. return $_post; 
  528.  
  529. /** 
  530. * Retrieve ancestors of a post. 
  531. * 
  532. * @since 2.5.0 
  533. * 
  534. * @param int|WP_Post $post Post ID or post object. 
  535. * @return array Ancestor IDs or empty array if none are found. 
  536. */ 
  537. function get_post_ancestors( $post ) { 
  538. $post = get_post( $post ); 
  539.  
  540. if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID ) 
  541. return array(); 
  542.  
  543. $ancestors = array(); 
  544.  
  545. $id = $ancestors[] = $post->post_parent; 
  546.  
  547. while ( $ancestor = get_post( $id ) ) { 
  548. // Loop detection: If the ancestor has been seen before, break. 
  549. if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) ) 
  550. break; 
  551.  
  552. $id = $ancestors[] = $ancestor->post_parent; 
  553.  
  554. return $ancestors; 
  555.  
  556. /** 
  557. * Retrieve data from a post field based on Post ID. 
  558. * 
  559. * Examples of the post field will be, 'post_type', 'post_status', 'post_content',  
  560. * etc and based off of the post object property or key names. 
  561. * 
  562. * The context values are based off of the taxonomy filter functions and 
  563. * supported values are found within those functions. 
  564. * 
  565. * @since 2.3.0 
  566. * @since 4.5.0 The `$post` parameter was made optional. 
  567. * 
  568. * @see sanitize_post_field() 
  569. * 
  570. * @param string $field Post field name. 
  571. * @param int|WP_Post $post Optional. Post ID or post object. Defaults to current post. 
  572. * @param string $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db',  
  573. * or 'display'. Default 'display'. 
  574. * @return string The value of the post field on success, empty string on failure. 
  575. */ 
  576. function get_post_field( $field, $post = null, $context = 'display' ) { 
  577. $post = get_post( $post ); 
  578.  
  579. if ( !$post ) 
  580. return ''; 
  581.  
  582. if ( !isset($post->$field) ) 
  583. return ''; 
  584.  
  585. return sanitize_post_field($field, $post->$field, $post->ID, $context); 
  586.  
  587. /** 
  588. * Retrieve the mime type of an attachment based on the ID. 
  589. * 
  590. * This function can be used with any post type, but it makes more sense with 
  591. * attachments. 
  592. * 
  593. * @since 2.0.0 
  594. * 
  595. * @param int|WP_Post $ID Optional. Post ID or post object. Default empty. 
  596. * @return string|false The mime type on success, false on failure. 
  597. */ 
  598. function get_post_mime_type( $ID = '' ) { 
  599. $post = get_post($ID); 
  600.  
  601. if ( is_object($post) ) 
  602. return $post->post_mime_type; 
  603.  
  604. return false; 
  605.  
  606. /** 
  607. * Retrieve the post status based on the Post ID. 
  608. * 
  609. * If the post ID is of an attachment, then the parent post status will be given 
  610. * instead. 
  611. * 
  612. * @since 2.0.0 
  613. * 
  614. * @param int|WP_Post $ID Optional. Post ID or post object. Default empty. 
  615. * @return string|false Post status on success, false on failure. 
  616. */ 
  617. function get_post_status( $ID = '' ) { 
  618. $post = get_post($ID); 
  619.  
  620. if ( !is_object($post) ) 
  621. return false; 
  622.  
  623. if ( 'attachment' == $post->post_type ) { 
  624. if ( 'private' == $post->post_status ) 
  625. return 'private'; 
  626.  
  627. // Unattached attachments are assumed to be published. 
  628. if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent) ) 
  629. return 'publish'; 
  630.  
  631. // Inherit status from the parent. 
  632. if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) { 
  633. $parent_post_status = get_post_status( $post->post_parent ); 
  634. if ( 'trash' == $parent_post_status ) { 
  635. return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true ); 
  636. } else { 
  637. return $parent_post_status; 
  638.  
  639.  
  640. /** 
  641. * Filters the post status. 
  642. * 
  643. * @since 4.4.0 
  644. * 
  645. * @param string $post_status The post status. 
  646. * @param WP_Post $post The post object. 
  647. */ 
  648. return apply_filters( 'get_post_status', $post->post_status, $post ); 
  649.  
  650. /** 
  651. * Retrieve all of the WordPress supported post statuses. 
  652. * 
  653. * Posts have a limited set of valid status values, this provides the 
  654. * post_status values and descriptions. 
  655. * 
  656. * @since 2.5.0 
  657. * 
  658. * @return array List of post statuses. 
  659. */ 
  660. function get_post_statuses() { 
  661. $status = array( 
  662. 'draft' => __( 'Draft' ),  
  663. 'pending' => __( 'Pending Review' ),  
  664. 'private' => __( 'Private' ),  
  665. 'publish' => __( 'Published' ) 
  666. ); 
  667.  
  668. return $status; 
  669.  
  670. /** 
  671. * Retrieve all of the WordPress support page statuses. 
  672. * 
  673. * Pages have a limited set of valid status values, this provides the 
  674. * post_status values and descriptions. 
  675. * 
  676. * @since 2.5.0 
  677. * 
  678. * @return array List of page statuses. 
  679. */ 
  680. function get_page_statuses() { 
  681. $status = array( 
  682. 'draft' => __( 'Draft' ),  
  683. 'private' => __( 'Private' ),  
  684. 'publish' => __( 'Published' ) 
  685. ); 
  686.  
  687. return $status; 
  688.  
  689. /** 
  690. * Register a post status. Do not use before init. 
  691. * 
  692. * A simple function for creating or modifying a post status based on the 
  693. * parameters given. The function will accept an array (second optional 
  694. * parameter), along with a string for the post status name. 
  695. * 
  696. * Arguments prefixed with an _underscore shouldn't be used by plugins and themes. 
  697. * 
  698. * @since 3.0.0 
  699. * @global array $wp_post_statuses Inserts new post status object into the list 
  700. * 
  701. * @param string $post_status Name of the post status. 
  702. * @param array|string $args { 
  703. * Optional. Array or string of post status arguments. 
  704. * 
  705. * @type bool|string $label A descriptive name for the post status marked 
  706. * for translation. Defaults to value of $post_status. 
  707. * @type bool|array $label_count Descriptive text to use for nooped plurals. 
  708. * Default array of $label, twice 
  709. * @type bool $exclude_from_search Whether to exclude posts with this post status 
  710. * from search results. Default is value of $internal. 
  711. * @type bool $_builtin Whether the status is built-in. Core-use only. 
  712. * Default false. 
  713. * @type bool $public Whether posts of this status should be shown 
  714. * in the front end of the site. Default false. 
  715. * @type bool $internal Whether the status is for internal use only. 
  716. * Default false. 
  717. * @type bool $protected Whether posts with this status should be protected. 
  718. * Default false. 
  719. * @type bool $private Whether posts with this status should be private. 
  720. * Default false. 
  721. * @type bool $publicly_queryable Whether posts with this status should be publicly- 
  722. * queryable. Default is value of $public. 
  723. * @type bool $show_in_admin_all_list Whether to include posts in the edit listing for 
  724. * their post type. Default is value of $internal. 
  725. * @type bool $show_in_admin_status_list Show in the list of statuses with post counts at 
  726. * the top of the edit listings,  
  727. * e.g. All (12) | Published (9) | My Custom Status (2) 
  728. * Default is value of $internal. 
  729. * } 
  730. * @return object 
  731. */ 
  732. function register_post_status( $post_status, $args = array() ) { 
  733. global $wp_post_statuses; 
  734.  
  735. if (!is_array($wp_post_statuses)) 
  736. $wp_post_statuses = array(); 
  737.  
  738. // Args prefixed with an underscore are reserved for internal use. 
  739. $defaults = array( 
  740. 'label' => false,  
  741. 'label_count' => false,  
  742. 'exclude_from_search' => null,  
  743. '_builtin' => false,  
  744. 'public' => null,  
  745. 'internal' => null,  
  746. 'protected' => null,  
  747. 'private' => null,  
  748. 'publicly_queryable' => null,  
  749. 'show_in_admin_status_list' => null,  
  750. 'show_in_admin_all_list' => null,  
  751. ); 
  752. $args = wp_parse_args($args, $defaults); 
  753. $args = (object) $args; 
  754.  
  755. $post_status = sanitize_key($post_status); 
  756. $args->name = $post_status; 
  757.  
  758. // Set various defaults. 
  759. if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private ) 
  760. $args->internal = true; 
  761.  
  762. if ( null === $args->public ) 
  763. $args->public = false; 
  764.  
  765. if ( null === $args->private ) 
  766. $args->private = false; 
  767.  
  768. if ( null === $args->protected ) 
  769. $args->protected = false; 
  770.  
  771. if ( null === $args->internal ) 
  772. $args->internal = false; 
  773.  
  774. if ( null === $args->publicly_queryable ) 
  775. $args->publicly_queryable = $args->public; 
  776.  
  777. if ( null === $args->exclude_from_search ) 
  778. $args->exclude_from_search = $args->internal; 
  779.  
  780. if ( null === $args->show_in_admin_all_list ) 
  781. $args->show_in_admin_all_list = !$args->internal; 
  782.  
  783. if ( null === $args->show_in_admin_status_list ) 
  784. $args->show_in_admin_status_list = !$args->internal; 
  785.  
  786. if ( false === $args->label ) 
  787. $args->label = $post_status; 
  788.  
  789. if ( false === $args->label_count ) 
  790. $args->label_count = array( $args->label, $args->label ); 
  791.  
  792. $wp_post_statuses[$post_status] = $args; 
  793.  
  794. return $args; 
  795.  
  796. /** 
  797. * Retrieve a post status object by name. 
  798. * 
  799. * @since 3.0.0 
  800. * 
  801. * @global array $wp_post_statuses List of post statuses. 
  802. * 
  803. * @see register_post_status() 
  804. * 
  805. * @param string $post_status The name of a registered post status. 
  806. * @return object|null A post status object. 
  807. */ 
  808. function get_post_status_object( $post_status ) { 
  809. global $wp_post_statuses; 
  810.  
  811. if ( empty($wp_post_statuses[$post_status]) ) 
  812. return null; 
  813.  
  814. return $wp_post_statuses[$post_status]; 
  815.  
  816. /** 
  817. * Get a list of post statuses. 
  818. * 
  819. * @since 3.0.0 
  820. * 
  821. * @global array $wp_post_statuses List of post statuses. 
  822. * 
  823. * @see register_post_status() 
  824. * 
  825. * @param array|string $args Optional. Array or string of post status arguments to compare against 
  826. * properties of the global `$wp_post_statuses objects`. Default empty array. 
  827. * @param string $output Optional. The type of output to return, either 'names' or 'objects'. Default 'names'. 
  828. * @param string $operator Optional. The logical operation to perform. 'or' means only one element 
  829. * from the array needs to match; 'and' means all elements must match. 
  830. * Default 'and'. 
  831. * @return array A list of post status names or objects. 
  832. */ 
  833. function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) { 
  834. global $wp_post_statuses; 
  835.  
  836. $field = ('names' == $output) ? 'name' : false; 
  837.  
  838. return wp_filter_object_list($wp_post_statuses, $args, $operator, $field); 
  839.  
  840. /** 
  841. * Whether the post type is hierarchical. 
  842. * 
  843. * A false return value might also mean that the post type does not exist. 
  844. * 
  845. * @since 3.0.0 
  846. * 
  847. * @see get_post_type_object() 
  848. * 
  849. * @param string $post_type Post type name 
  850. * @return bool Whether post type is hierarchical. 
  851. */ 
  852. function is_post_type_hierarchical( $post_type ) { 
  853. if ( ! post_type_exists( $post_type ) ) 
  854. return false; 
  855.  
  856. $post_type = get_post_type_object( $post_type ); 
  857. return $post_type->hierarchical; 
  858.  
  859. /** 
  860. * Check if a post type is registered. 
  861. * 
  862. * @since 3.0.0 
  863. * 
  864. * @see get_post_type_object() 
  865. * 
  866. * @param string $post_type Post type name. 
  867. * @return bool Whether post type is registered. 
  868. */ 
  869. function post_type_exists( $post_type ) { 
  870. return (bool) get_post_type_object( $post_type ); 
  871.  
  872. /** 
  873. * Retrieves the post type of the current post or of a given post. 
  874. * 
  875. * @since 2.1.0 
  876. * 
  877. * @param int|WP_Post|null $post Optional. Post ID or post object. Default is global $post. 
  878. * @return string|false Post type on success, false on failure. 
  879. */ 
  880. function get_post_type( $post = null ) { 
  881. if ( $post = get_post( $post ) ) 
  882. return $post->post_type; 
  883.  
  884. return false; 
  885.  
  886. /** 
  887. * Retrieves a post type object by name. 
  888. * 
  889. * @since 3.0.0 
  890. * @since 4.6.0 Object returned is now an instance of WP_Post_Type. 
  891. * 
  892. * @global array $wp_post_types List of post types. 
  893. * 
  894. * @see register_post_type() 
  895. * 
  896. * @param string $post_type The name of a registered post type. 
  897. * @return WP_Post_Type|null WP_Post_Type object if it exists, null otherwise. 
  898. */ 
  899. function get_post_type_object( $post_type ) { 
  900. global $wp_post_types; 
  901.  
  902. if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) { 
  903. return null; 
  904.  
  905. return $wp_post_types[ $post_type ]; 
  906.  
  907. /** 
  908. * Get a list of all registered post type objects. 
  909. * 
  910. * @since 2.9.0 
  911. * 
  912. * @global array $wp_post_types List of post types. 
  913. * 
  914. * @see register_post_type() for accepted arguments. 
  915. * 
  916. * @param array|string $args Optional. An array of key => value arguments to match against 
  917. * the post type objects. Default empty array. 
  918. * @param string $output Optional. The type of output to return. Accepts post type 'names' 
  919. * or 'objects'. Default 'names'. 
  920. * @param string $operator Optional. The logical operation to perform. 'or' means only one 
  921. * element from the array needs to match; 'and' means all elements 
  922. * must match; 'not' means no elements may match. Default 'and'. 
  923. * @return array A list of post type names or objects. 
  924. */ 
  925. function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) { 
  926. global $wp_post_types; 
  927.  
  928. $field = ('names' == $output) ? 'name' : false; 
  929.  
  930. return wp_filter_object_list($wp_post_types, $args, $operator, $field); 
  931.  
  932. /** 
  933. * Registers a post type. 
  934. * 
  935. * Note: Post type registrations should not be hooked before the 
  936. * {@see 'init'} action. Also, any taxonomy connections should be 
  937. * registered via the `$taxonomies` argument to ensure consistency 
  938. * when hooks such as {@see 'parse_query'} or {@see 'pre_get_posts'} 
  939. * are used. 
  940. * 
  941. * Post types can support any number of built-in core features such 
  942. * as meta boxes, custom fields, post thumbnails, post statuses,  
  943. * comments, and more. See the `$supports` argument for a complete 
  944. * list of supported features. 
  945. * 
  946. * @since 2.9.0 
  947. * @since 3.0.0 The `show_ui` argument is now enforced on the new post screen. 
  948. * @since 4.4.0 The `show_ui` argument is now enforced on the post type listing 
  949. * screen and post editing screen. 
  950. * @since 4.6.0 Post type object returned is now an instance of WP_Post_Type. 
  951. * @since 4.7.0 Introduced `show_in_rest`, 'rest_base' and 'rest_controller_class' 
  952. * arguments to register the post type in REST API. 
  953. * 
  954. * @global array $wp_post_types List of post types. 
  955. * 
  956. * @param string $post_type Post type key. Must not exceed 20 characters and may 
  957. * only contain lowercase alphanumeric characters, dashes,  
  958. * and underscores. See sanitize_key(). 
  959. * @param array|string $args { 
  960. * Array or string of arguments for registering a post type. 
  961. * 
  962. * @type string $label Name of the post type shown in the menu. Usually plural. 
  963. * Default is value of $labels['name']. 
  964. * @type array $labels An array of labels for this post type. If not set, post 
  965. * labels are inherited for non-hierarchical types and page 
  966. * labels for hierarchical ones. See get_post_type_labels() for a full 
  967. * list of supported labels. 
  968. * @type string $description A short descriptive summary of what the post type is. 
  969. * Default empty. 
  970. * @type bool $public Whether a post type is intended for use publicly either via 
  971. * the admin interface or by front-end users. While the default 
  972. * settings of $exclude_from_search, $publicly_queryable, $show_ui,  
  973. * and $show_in_nav_menus are inherited from public, each does not 
  974. * rely on this relationship and controls a very specific intention. 
  975. * Default false. 
  976. * @type bool $hierarchical Whether the post type is hierarchical (e.g. page). Default false. 
  977. * @type bool $exclude_from_search Whether to exclude posts with this post type from front end search 
  978. * results. Default is the opposite value of $public. 
  979. * @type bool $publicly_queryable Whether queries can be performed on the front end for the post type 
  980. * as part of parse_request(). Endpoints would include: 
  981. * * ?post_type={post_type_key} 
  982. * * ?{post_type_key}={single_post_slug} 
  983. * * ?{post_type_query_var}={single_post_slug} 
  984. * If not set, the default is inherited from $public. 
  985. * @type bool $show_ui Whether to generate and allow a UI for managing this post type in the 
  986. * admin. Default is value of $public. 
  987. * @type bool $show_in_menu Where to show the post type in the admin menu. To work, $show_ui 
  988. * must be true. If true, the post type is shown in its own top level 
  989. * menu. If false, no menu is shown. If a string of an existing top 
  990. * level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post 
  991. * type will be placed as a sub-menu of that. 
  992. * Default is value of $show_ui. 
  993. * @type bool $show_in_nav_menus Makes this post type available for selection in navigation menus. 
  994. * Default is value $public. 
  995. * @type bool $show_in_admin_bar Makes this post type available via the admin bar. Default is value 
  996. * of $show_in_menu. 
  997. * @type bool $show_in_rest Whether to add the post type route in the REST API 'wp/v2' namespace. 
  998. * @type string $rest_base To change the base url of REST API route. Default is $post_type. 
  999. * @type string $rest_controller_class REST API Controller class name. Default is 'WP_REST_Posts_Controller'. 
  1000. * @type int $menu_position The position in the menu order the post type should appear. To work,  
  1001. * $show_in_menu must be true. Default null (at the bottom). 
  1002. * @type string $menu_icon The url to the icon to be used for this menu. Pass a base64-encoded 
  1003. * SVG using a data URI, which will be colored to match the color scheme 
  1004. * -- this should begin with 'data:image/svg+xml;base64, '. Pass the name 
  1005. * of a Dashicons helper class to use a font icon, e.g. 
  1006. * 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty 
  1007. * so an icon can be added via CSS. Defaults to use the posts icon. 
  1008. * @type string $capability_type The string to use to build the read, edit, and delete capabilities. 
  1009. * May be passed as an array to allow for alternative plurals when using 
  1010. * this argument as a base to construct the capabilities, e.g. 
  1011. * array('story', 'stories'). Default 'post'. 
  1012. * @type array $capabilities Array of capabilities for this post type. $capability_type is used 
  1013. * as a base to construct capabilities by default. 
  1014. * See get_post_type_capabilities(). 
  1015. * @type bool $map_meta_cap Whether to use the internal default meta capability handling. 
  1016. * Default false. 
  1017. * @type array $supports Core feature(s) the post type supports. Serves as an alias for calling 
  1018. * add_post_type_support() directly. Core features include 'title',  
  1019. * 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt',  
  1020. * 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'. 
  1021. * Additionally, the 'revisions' feature dictates whether the post type 
  1022. * will store revisions, and the 'comments' feature dictates whether the 
  1023. * comments count will show on the edit screen. Defaults is an array 
  1024. * containing 'title' and 'editor'. 
  1025. * @type callable $register_meta_box_cb Provide a callback function that sets up the meta boxes for the 
  1026. * edit form. Do remove_meta_box() and add_meta_box() calls in the 
  1027. * callback. Default null. 
  1028. * @type array $taxonomies An array of taxonomy identifiers that will be registered for the 
  1029. * post type. Taxonomies can be registered later with register_taxonomy() 
  1030. * or register_taxonomy_for_object_type(). 
  1031. * Default empty array. 
  1032. * @type bool|string $has_archive Whether there should be post type archives, or if a string, the 
  1033. * archive slug to use. Will generate the proper rewrite rules if 
  1034. * $rewrite is enabled. Default false. 
  1035. * @type bool|array $rewrite { 
  1036. * Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. 
  1037. * Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be 
  1038. * passed with any of these keys: 
  1039. * 
  1040. * @type string $slug Customize the permastruct slug. Defaults to $post_type key. 
  1041. * @type bool $with_front Whether the permastruct should be prepended with WP_Rewrite::$front. 
  1042. * Default true. 
  1043. * @type bool $feeds Whether the feed permastruct should be built for this post type. 
  1044. * Default is value of $has_archive. 
  1045. * @type bool $pages Whether the permastruct should provide for pagination. Default true. 
  1046. * @type const $ep_mask Endpoint mask to assign. If not specified and permalink_epmask is set,  
  1047. * inherits from $permalink_epmask. If not specified and permalink_epmask 
  1048. * is not set, defaults to EP_PERMALINK. 
  1049. * } 
  1050. * @type string|bool $query_var Sets the query_var key for this post type. Defaults to $post_type 
  1051. * key. If false, a post type cannot be loaded at 
  1052. * ?{query_var}={post_slug}. If specified as a string, the query 
  1053. * ?{query_var_string}={post_slug} will be valid. 
  1054. * @type bool $can_export Whether to allow this post type to be exported. Default true. 
  1055. * @type bool $delete_with_user Whether to delete posts of this type when deleting a user. If true,  
  1056. * posts of this type belonging to the user will be moved to trash 
  1057. * when then user is deleted. If false, posts of this type belonging 
  1058. * to the user will *not* be trashed or deleted. If not set (the default),  
  1059. * posts are trashed if post_type_supports('author'). Otherwise posts 
  1060. * are not trashed or deleted. Default null. 
  1061. * @type bool $_builtin FOR INTERNAL USE ONLY! True if this post type is a native or 
  1062. * "built-in" post_type. Default false. 
  1063. * @type string $_edit_link FOR INTERNAL USE ONLY! URL segment to use for edit link of 
  1064. * this post type. Default 'post.php?post=%d'. 
  1065. * } 
  1066. * @return WP_Post_Type|WP_Error The registered post type object, or an error object. 
  1067. */ 
  1068. function register_post_type( $post_type, $args = array() ) { 
  1069. global $wp_post_types; 
  1070.  
  1071. if ( ! is_array( $wp_post_types ) ) { 
  1072. $wp_post_types = array(); 
  1073.  
  1074. // Sanitize post type name 
  1075. $post_type = sanitize_key( $post_type ); 
  1076.  
  1077. if ( empty( $post_type ) || strlen( $post_type ) > 20 ) { 
  1078. _doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2.0' ); 
  1079. return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) ); 
  1080.  
  1081. $post_type_object = new WP_Post_Type( $post_type, $args ); 
  1082. $post_type_object->add_supports(); 
  1083. $post_type_object->add_rewrite_rules(); 
  1084. $post_type_object->register_meta_boxes(); 
  1085.  
  1086. $wp_post_types[ $post_type ] = $post_type_object; 
  1087.  
  1088. $post_type_object->add_hooks(); 
  1089. $post_type_object->register_taxonomies(); 
  1090.  
  1091. /** 
  1092. * Fires after a post type is registered. 
  1093. * 
  1094. * @since 3.3.0 
  1095. * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object. 
  1096. * 
  1097. * @param string $post_type Post type. 
  1098. * @param WP_Post_Type $post_type_object Arguments used to register the post type. 
  1099. */ 
  1100. do_action( 'registered_post_type', $post_type, $post_type_object ); 
  1101.  
  1102. return $post_type_object; 
  1103.  
  1104. /** 
  1105. * Unregisters a post type. 
  1106. * 
  1107. * Can not be used to unregister built-in post types. 
  1108. * 
  1109. * @since 4.5.0 
  1110. * 
  1111. * @global array $wp_post_types List of post types. 
  1112. * 
  1113. * @param string $post_type Post type to unregister. 
  1114. * @return bool|WP_Error True on success, WP_Error on failure or if the post type doesn't exist. 
  1115. */ 
  1116. function unregister_post_type( $post_type ) { 
  1117. global $wp_post_types; 
  1118.  
  1119. if ( ! post_type_exists( $post_type ) ) { 
  1120. return new WP_Error( 'invalid_post_type', __( 'Invalid post type.' ) ); 
  1121.  
  1122. $post_type_object = get_post_type_object( $post_type ); 
  1123.  
  1124. // Do not allow unregistering internal post types. 
  1125. if ( $post_type_object->_builtin ) { 
  1126. return new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) ); 
  1127.  
  1128. $post_type_object->remove_supports(); 
  1129. $post_type_object->remove_rewrite_rules(); 
  1130. $post_type_object->unregister_meta_boxes(); 
  1131. $post_type_object->remove_hooks(); 
  1132. $post_type_object->unregister_taxonomies(); 
  1133.  
  1134. unset( $wp_post_types[ $post_type ] ); 
  1135.  
  1136. /** 
  1137. * Fires after a post type was unregistered. 
  1138. * 
  1139. * @since 4.5.0 
  1140. * 
  1141. * @param string $post_type Post type key. 
  1142. */ 
  1143. do_action( 'unregistered_post_type', $post_type ); 
  1144.  
  1145. return true; 
  1146.  
  1147. /** 
  1148. * Build an object with all post type capabilities out of a post type object 
  1149. * 
  1150. * Post type capabilities use the 'capability_type' argument as a base, if the 
  1151. * capability is not set in the 'capabilities' argument array or if the 
  1152. * 'capabilities' argument is not supplied. 
  1153. * 
  1154. * The capability_type argument can optionally be registered as an array, with 
  1155. * the first value being singular and the second plural, e.g. array('story, 'stories') 
  1156. * Otherwise, an 's' will be added to the value for the plural form. After 
  1157. * registration, capability_type will always be a string of the singular value. 
  1158. * 
  1159. * By default, seven keys are accepted as part of the capabilities array: 
  1160. * 
  1161. * - edit_post, read_post, and delete_post are meta capabilities, which are then 
  1162. * generally mapped to corresponding primitive capabilities depending on the 
  1163. * context, which would be the post being edited/read/deleted and the user or 
  1164. * role being checked. Thus these capabilities would generally not be granted 
  1165. * directly to users or roles. 
  1166. * 
  1167. * - edit_posts - Controls whether objects of this post type can be edited. 
  1168. * - edit_others_posts - Controls whether objects of this type owned by other users 
  1169. * can be edited. If the post type does not support an author, then this will 
  1170. * behave like edit_posts. 
  1171. * - publish_posts - Controls publishing objects of this post type. 
  1172. * - read_private_posts - Controls whether private objects can be read. 
  1173. * 
  1174. * These four primitive capabilities are checked in core in various locations. 
  1175. * There are also seven other primitive capabilities which are not referenced 
  1176. * directly in core, except in map_meta_cap(), which takes the three aforementioned 
  1177. * meta capabilities and translates them into one or more primitive capabilities 
  1178. * that must then be checked against the user or role, depending on the context. 
  1179. * 
  1180. * - read - Controls whether objects of this post type can be read. 
  1181. * - delete_posts - Controls whether objects of this post type can be deleted. 
  1182. * - delete_private_posts - Controls whether private objects can be deleted. 
  1183. * - delete_published_posts - Controls whether published objects can be deleted. 
  1184. * - delete_others_posts - Controls whether objects owned by other users can be 
  1185. * can be deleted. If the post type does not support an author, then this will 
  1186. * behave like delete_posts. 
  1187. * - edit_private_posts - Controls whether private objects can be edited. 
  1188. * - edit_published_posts - Controls whether published objects can be edited. 
  1189. * 
  1190. * These additional capabilities are only used in map_meta_cap(). Thus, they are 
  1191. * only assigned by default if the post type is registered with the 'map_meta_cap' 
  1192. * argument set to true (default is false). 
  1193. * 
  1194. * @since 3.0.0 
  1195. * 
  1196. * @see register_post_type() 
  1197. * @see map_meta_cap() 
  1198. * 
  1199. * @param object $args Post type registration arguments. 
  1200. * @return object object with all the capabilities as member variables. 
  1201. */ 
  1202. function get_post_type_capabilities( $args ) { 
  1203. if ( ! is_array( $args->capability_type ) ) 
  1204. $args->capability_type = array( $args->capability_type, $args->capability_type . 's' ); 
  1205.  
  1206. // Singular base for meta capabilities, plural base for primitive capabilities. 
  1207. list( $singular_base, $plural_base ) = $args->capability_type; 
  1208.  
  1209. $default_capabilities = array( 
  1210. // Meta capabilities 
  1211. 'edit_post' => 'edit_' . $singular_base,  
  1212. 'read_post' => 'read_' . $singular_base,  
  1213. 'delete_post' => 'delete_' . $singular_base,  
  1214. // Primitive capabilities used outside of map_meta_cap(): 
  1215. 'edit_posts' => 'edit_' . $plural_base,  
  1216. 'edit_others_posts' => 'edit_others_' . $plural_base,  
  1217. 'publish_posts' => 'publish_' . $plural_base,  
  1218. 'read_private_posts' => 'read_private_' . $plural_base,  
  1219. ); 
  1220.  
  1221. // Primitive capabilities used within map_meta_cap(): 
  1222. if ( $args->map_meta_cap ) { 
  1223. $default_capabilities_for_mapping = array( 
  1224. 'read' => 'read',  
  1225. 'delete_posts' => 'delete_' . $plural_base,  
  1226. 'delete_private_posts' => 'delete_private_' . $plural_base,  
  1227. 'delete_published_posts' => 'delete_published_' . $plural_base,  
  1228. 'delete_others_posts' => 'delete_others_' . $plural_base,  
  1229. 'edit_private_posts' => 'edit_private_' . $plural_base,  
  1230. 'edit_published_posts' => 'edit_published_' . $plural_base,  
  1231. ); 
  1232. $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping ); 
  1233.  
  1234. $capabilities = array_merge( $default_capabilities, $args->capabilities ); 
  1235.  
  1236. // Post creation capability simply maps to edit_posts by default: 
  1237. if ( ! isset( $capabilities['create_posts'] ) ) 
  1238. $capabilities['create_posts'] = $capabilities['edit_posts']; 
  1239.  
  1240. // Remember meta capabilities for future reference. 
  1241. if ( $args->map_meta_cap ) 
  1242. _post_type_meta_capabilities( $capabilities ); 
  1243.  
  1244. return (object) $capabilities; 
  1245.  
  1246. /** 
  1247. * Store or return a list of post type meta caps for map_meta_cap(). 
  1248. * 
  1249. * @since 3.1.0 
  1250. * @access private 
  1251. * 
  1252. * @global array $post_type_meta_caps Used to store meta capabilities. 
  1253. * 
  1254. * @param array $capabilities Post type meta capabilities. 
  1255. */ 
  1256. function _post_type_meta_capabilities( $capabilities = null ) { 
  1257. global $post_type_meta_caps; 
  1258.  
  1259. foreach ( $capabilities as $core => $custom ) { 
  1260. if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) ) { 
  1261. $post_type_meta_caps[ $custom ] = $core; 
  1262.  
  1263. /** 
  1264. * Builds an object with all post type labels out of a post type object. 
  1265. * 
  1266. * Accepted keys of the label array in the post type object: 
  1267. * 
  1268. * - `name` - General name for the post type, usually plural. The same and overridden 
  1269. * by `$post_type_object->label`. Default is 'Posts' / 'Pages'. 
  1270. * - `singular_name` - Name for one object of this post type. Default is 'Post' / 'Page'. 
  1271. * - `add_new` - Default is 'Add New' for both hierarchical and non-hierarchical types. 
  1272. * When internationalizing this string, please use a {@link https://codex.wordpress.org/I18n_for_WordPress_Developers#Disambiguation_by_context gettext context} 
  1273. * matching your post type. Example: `_x( 'Add New', 'product', 'textdomain' );`. 
  1274. * - `add_new_item` - Label for adding a new singular item. Default is 'Add New Post' / 'Add New Page'. 
  1275. * - `edit_item` - Label for editing a singular item. Default is 'Edit Post' / 'Edit Page'. 
  1276. * - `new_item` - Label for the new item page title. Default is 'New Post' / 'New Page'. 
  1277. * - `view_item` - Label for viewing a singular item. Default is 'View Post' / 'View Page'. 
  1278. * - `view_items` - Label for viewing post type archives. Default is 'View Posts' / 'View Pages'. 
  1279. * - `search_items` - Label for searching plural items. Default is 'Search Posts' / 'Search Pages'. 
  1280. * - `not_found` - Label used when no items are found. Default is 'No posts found' / 'No pages found'. 
  1281. * - `not_found_in_trash` - Label used when no items are in the trash. Default is 'No posts found in Trash' / 
  1282. * 'No pages found in Trash'. 
  1283. * - `parent_item_colon` - Label used to prefix parents of hierarchical items. Not used on non-hierarchical 
  1284. * post types. Default is 'Parent Page:'. 
  1285. * - `all_items` - Label to signify all items in a submenu link. Default is 'All Posts' / 'All Pages'. 
  1286. * - `archives` - Label for archives in nav menus. Default is 'Post Archives' / 'Page Archives'. 
  1287. * - `attributes` - Label for the attributes meta box. Default is 'Post Attributes' / 'Page Attributes'. 
  1288. * - `insert_into_item` - Label for the media frame button. Default is 'Insert into post' / 'Insert into page'. 
  1289. * - `uploaded_to_this_item` - Label for the media frame filter. Default is 'Uploaded to this post' / 
  1290. * 'Uploaded to this page'. 
  1291. * - `featured_image` - Label for the Featured Image meta box title. Default is 'Featured Image'. 
  1292. * - `set_featured_image` - Label for setting the featured image. Default is 'Set featured image'. 
  1293. * - `remove_featured_image` - Label for removing the featured image. Default is 'Remove featured image'. 
  1294. * - `use_featured_image` - Label in the media frame for using a featured image. Default is 'Use as featured image'. 
  1295. * - `menu_name` - Label for the menu name. Default is the same as `name`. 
  1296. * - `filter_items_list` - Label for the table views hidden heading. Default is 'Filter posts list' / 
  1297. * 'Filter pages list'. 
  1298. * - `items_list_navigation` - Label for the table pagination hidden heading. Default is 'Posts list navigation' / 
  1299. * 'Pages list navigation'. 
  1300. * - `items_list` - Label for the table hidden heading. Default is 'Posts list' / 'Pages list'. 
  1301. * 
  1302. * Above, the first default value is for non-hierarchical post types (like posts) 
  1303. * and the second one is for hierarchical post types (like pages). 
  1304. * 
  1305. * Note: To set labels used in post type admin notices, see the {@see 'post_updated_messages'} filter. 
  1306. * 
  1307. * @since 3.0.0 
  1308. * @since 4.3.0 Added the `featured_image`, `set_featured_image`, `remove_featured_image`,  
  1309. * and `use_featured_image` labels. 
  1310. * @since 4.4.0 Added the `insert_into_item`, `uploaded_to_this_item`, `filter_items_list`,  
  1311. * `items_list_navigation`, and `items_list` labels. 
  1312. * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object. 
  1313. * @since 4.7.0 Added the `view_items` and `attributes` labels. 
  1314. * 
  1315. * @access private 
  1316. * 
  1317. * @param object|WP_Post_Type $post_type_object Post type object. 
  1318. * @return object Object with all the labels as member variables. 
  1319. */ 
  1320. function get_post_type_labels( $post_type_object ) { 
  1321. $nohier_vs_hier_defaults = array( 
  1322. 'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name') ),  
  1323. 'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name') ),  
  1324. 'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ),  
  1325. 'add_new_item' => array( __('Add New Post'), __('Add New Page') ),  
  1326. 'edit_item' => array( __('Edit Post'), __('Edit Page') ),  
  1327. 'new_item' => array( __('New Post'), __('New Page') ),  
  1328. 'view_item' => array( __('View Post'), __('View Page') ),  
  1329. 'view_items' => array( __('View Posts'), __('View Pages') ),  
  1330. 'search_items' => array( __('Search Posts'), __('Search Pages') ),  
  1331. 'not_found' => array( __('No posts found.'), __('No pages found.') ),  
  1332. 'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ),  
  1333. 'parent_item_colon' => array( null, __('Parent Page:') ),  
  1334. 'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) ),  
  1335. 'archives' => array( __( 'Post Archives' ), __( 'Page Archives' ) ),  
  1336. 'attributes' => array( __( 'Post Attributes' ), __( 'Page Attributes' ) ),  
  1337. 'insert_into_item' => array( __( 'Insert into post' ), __( 'Insert into page' ) ),  
  1338. 'uploaded_to_this_item' => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ),  
  1339. 'featured_image' => array( __( 'Featured Image' ), __( 'Featured Image' ) ),  
  1340. 'set_featured_image' => array( __( 'Set featured image' ), __( 'Set featured image' ) ),  
  1341. 'remove_featured_image' => array( __( 'Remove featured image' ), __( 'Remove featured image' ) ),  
  1342. 'use_featured_image' => array( __( 'Use as featured image' ), __( 'Use as featured image' ) ),  
  1343. 'filter_items_list' => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ),  
  1344. 'items_list_navigation' => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),  
  1345. 'items_list' => array( __( 'Posts list' ), __( 'Pages list' ) ),  
  1346. ); 
  1347. $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; 
  1348.  
  1349. $labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults ); 
  1350.  
  1351. $post_type = $post_type_object->name; 
  1352.  
  1353. $default_labels = clone $labels; 
  1354.  
  1355. /** 
  1356. * Filters the labels of a specific post type. 
  1357. * 
  1358. * The dynamic portion of the hook name, `$post_type`, refers to 
  1359. * the post type slug. 
  1360. * 
  1361. * @since 3.5.0 
  1362. * 
  1363. * @see get_post_type_labels() for the full list of labels. 
  1364. * 
  1365. * @param object $labels Object with labels for the post type as member variables. 
  1366. */ 
  1367. $labels = apply_filters( "post_type_labels_{$post_type}", $labels ); 
  1368.  
  1369. // Ensure that the filtered labels contain all required default values. 
  1370. $labels = (object) array_merge( (array) $default_labels, (array) $labels ); 
  1371.  
  1372. return $labels; 
  1373.  
  1374. /** 
  1375. * Build an object with custom-something object (post type, taxonomy) labels 
  1376. * out of a custom-something object 
  1377. * 
  1378. * @since 3.0.0 
  1379. * @access private 
  1380. * 
  1381. * @param object $object A custom-something object. 
  1382. * @param array $nohier_vs_hier_defaults Hierarchical vs non-hierarchical default labels. 
  1383. * @return object Object containing labels for the given custom-something object. 
  1384. */ 
  1385. function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) { 
  1386. $object->labels = (array) $object->labels; 
  1387.  
  1388. if ( isset( $object->label ) && empty( $object->labels['name'] ) ) 
  1389. $object->labels['name'] = $object->label; 
  1390.  
  1391. if ( !isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) ) 
  1392. $object->labels['singular_name'] = $object->labels['name']; 
  1393.  
  1394. if ( ! isset( $object->labels['name_admin_bar'] ) ) 
  1395. $object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name; 
  1396.  
  1397. if ( !isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) ) 
  1398. $object->labels['menu_name'] = $object->labels['name']; 
  1399.  
  1400. if ( !isset( $object->labels['all_items'] ) && isset( $object->labels['menu_name'] ) ) 
  1401. $object->labels['all_items'] = $object->labels['menu_name']; 
  1402.  
  1403. if ( !isset( $object->labels['archives'] ) && isset( $object->labels['all_items'] ) ) { 
  1404. $object->labels['archives'] = $object->labels['all_items']; 
  1405.  
  1406. $defaults = array(); 
  1407. foreach ( $nohier_vs_hier_defaults as $key => $value ) { 
  1408. $defaults[$key] = $object->hierarchical ? $value[1] : $value[0]; 
  1409. $labels = array_merge( $defaults, $object->labels ); 
  1410. $object->labels = (object) $object->labels; 
  1411.  
  1412. return (object) $labels; 
  1413.  
  1414. /** 
  1415. * Add submenus for post types. 
  1416. * 
  1417. * @access private 
  1418. * @since 3.1.0 
  1419. */ 
  1420. function _add_post_type_submenus() { 
  1421. foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) { 
  1422. $ptype_obj = get_post_type_object( $ptype ); 
  1423. // Sub-menus only. 
  1424. if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true ) 
  1425. continue; 
  1426. add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" ); 
  1427.  
  1428. /** 
  1429. * Register support of certain features for a post type. 
  1430. * 
  1431. * All core features are directly associated with a functional area of the edit 
  1432. * screen, such as the editor or a meta box. Features include: 'title', 'editor',  
  1433. * 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes',  
  1434. * 'thumbnail', 'custom-fields', and 'post-formats'. 
  1435. * 
  1436. * Additionally, the 'revisions' feature dictates whether the post type will 
  1437. * store revisions, and the 'comments' feature dictates whether the comments 
  1438. * count will show on the edit screen. 
  1439. * 
  1440. * @since 3.0.0 
  1441. * 
  1442. * @global array $_wp_post_type_features 
  1443. * 
  1444. * @param string $post_type The post type for which to add the feature. 
  1445. * @param string|array $feature The feature being added, accepts an array of 
  1446. * feature strings or a single string. 
  1447. */ 
  1448. function add_post_type_support( $post_type, $feature ) { 
  1449. global $_wp_post_type_features; 
  1450.  
  1451. $features = (array) $feature; 
  1452. foreach ($features as $feature) { 
  1453. if ( func_num_args() == 2 ) 
  1454. $_wp_post_type_features[$post_type][$feature] = true; 
  1455. else 
  1456. $_wp_post_type_features[$post_type][$feature] = array_slice( func_get_args(), 2 ); 
  1457.  
  1458. /** 
  1459. * Remove support for a feature from a post type. 
  1460. * 
  1461. * @since 3.0.0 
  1462. * 
  1463. * @global array $_wp_post_type_features 
  1464. * 
  1465. * @param string $post_type The post type for which to remove the feature. 
  1466. * @param string $feature The feature being removed. 
  1467. */ 
  1468. function remove_post_type_support( $post_type, $feature ) { 
  1469. global $_wp_post_type_features; 
  1470.  
  1471. unset( $_wp_post_type_features[ $post_type ][ $feature ] ); 
  1472.  
  1473. /** 
  1474. * Get all the post type features 
  1475. * 
  1476. * @since 3.4.0 
  1477. * 
  1478. * @global array $_wp_post_type_features 
  1479. * 
  1480. * @param string $post_type The post type. 
  1481. * @return array Post type supports list. 
  1482. */ 
  1483. function get_all_post_type_supports( $post_type ) { 
  1484. global $_wp_post_type_features; 
  1485.  
  1486. if ( isset( $_wp_post_type_features[$post_type] ) ) 
  1487. return $_wp_post_type_features[$post_type]; 
  1488.  
  1489. return array(); 
  1490.  
  1491. /** 
  1492. * Check a post type's support for a given feature. 
  1493. * 
  1494. * @since 3.0.0 
  1495. * 
  1496. * @global array $_wp_post_type_features 
  1497. * 
  1498. * @param string $post_type The post type being checked. 
  1499. * @param string $feature The feature being checked. 
  1500. * @return bool Whether the post type supports the given feature. 
  1501. */ 
  1502. function post_type_supports( $post_type, $feature ) { 
  1503. global $_wp_post_type_features; 
  1504.  
  1505. return ( isset( $_wp_post_type_features[$post_type][$feature] ) ); 
  1506.  
  1507. /** 
  1508. * Retrieves a list of post type names that support a specific feature. 
  1509. * 
  1510. * @since 4.5.0 
  1511. * 
  1512. * @global array $_wp_post_type_features Post type features 
  1513. * 
  1514. * @param array|string $feature Single feature or an array of features the post types should support. 
  1515. * @param string $operator Optional. The logical operation to perform. 'or' means 
  1516. * only one element from the array needs to match; 'and' 
  1517. * means all elements must match; 'not' means no elements may 
  1518. * match. Default 'and'. 
  1519. * @return array A list of post type names. 
  1520. */ 
  1521. function get_post_types_by_support( $feature, $operator = 'and' ) { 
  1522. global $_wp_post_type_features; 
  1523.  
  1524. $features = array_fill_keys( (array) $feature, true ); 
  1525.  
  1526. return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) ); 
  1527.  
  1528. /** 
  1529. * Update the post type for the post ID. 
  1530. * 
  1531. * The page or post cache will be cleaned for the post ID. 
  1532. * 
  1533. * @since 2.5.0 
  1534. * 
  1535. * @global wpdb $wpdb WordPress database abstraction object. 
  1536. * 
  1537. * @param int $post_id Optional. Post ID to change post type. Default 0. 
  1538. * @param string $post_type Optional. Post type. Accepts 'post' or 'page' to 
  1539. * name a few. Default 'post'. 
  1540. * @return int|false Amount of rows changed. Should be 1 for success and 0 for failure. 
  1541. */ 
  1542. function set_post_type( $post_id = 0, $post_type = 'post' ) { 
  1543. global $wpdb; 
  1544.  
  1545. $post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db'); 
  1546. $return = $wpdb->update( $wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) ); 
  1547.  
  1548. clean_post_cache( $post_id ); 
  1549.  
  1550. return $return; 
  1551.  
  1552. /** 
  1553. * Determines whether a post type is considered "viewable". 
  1554. * 
  1555. * For built-in post types such as posts and pages, the 'public' value will be evaluated. 
  1556. * For all others, the 'publicly_queryable' value will be used. 
  1557. * 
  1558. * @since 4.4.0 
  1559. * @since 4.5.0 Added the ability to pass a post type name in addition to object. 
  1560. * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object. 
  1561. * 
  1562. * @param string|WP_Post_Type $post_type Post type name or object. 
  1563. * @return bool Whether the post type should be considered viewable. 
  1564. */ 
  1565. function is_post_type_viewable( $post_type ) { 
  1566. if ( is_scalar( $post_type ) ) { 
  1567. $post_type = get_post_type_object( $post_type ); 
  1568. if ( ! $post_type ) { 
  1569. return false; 
  1570.  
  1571. return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public ); 
  1572.  
  1573. /** 
  1574. * Retrieve list of latest posts or posts matching criteria. 
  1575. * 
  1576. * The defaults are as follows: 
  1577. * 
  1578. * @since 1.2.0 
  1579. * 
  1580. * @see WP_Query::parse_query() 
  1581. * 
  1582. * @param array $args { 
  1583. * Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all 
  1584. * available arguments. 
  1585. * 
  1586. * @type int $numberposts Total number of posts to retrieve. Is an alias of $posts_per_page 
  1587. * in WP_Query. Accepts -1 for all. Default 5. 
  1588. * @type int|string $category Category ID or comma-separated list of IDs (this or any children). 
  1589. * Is an alias of $cat in WP_Query. Default 0. 
  1590. * @type array $include An array of post IDs to retrieve, sticky posts will be included. 
  1591. * Is an alias of $post__in in WP_Query. Default empty array. 
  1592. * @type array $exclude An array of post IDs not to retrieve. Default empty array. 
  1593. * @type bool $suppress_filters Whether to suppress filters. Default true. 
  1594. * } 
  1595. * @return array List of posts. 
  1596. */ 
  1597. function get_posts( $args = null ) { 
  1598. $defaults = array( 
  1599. 'numberposts' => 5,  
  1600. 'category' => 0, 'orderby' => 'date',  
  1601. 'order' => 'DESC', 'include' => array(),  
  1602. 'exclude' => array(), 'meta_key' => '',  
  1603. 'meta_value' =>'', 'post_type' => 'post',  
  1604. 'suppress_filters' => true 
  1605. ); 
  1606.  
  1607. $r = wp_parse_args( $args, $defaults ); 
  1608. if ( empty( $r['post_status'] ) ) 
  1609. $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish'; 
  1610. if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) ) 
  1611. $r['posts_per_page'] = $r['numberposts']; 
  1612. if ( ! empty($r['category']) ) 
  1613. $r['cat'] = $r['category']; 
  1614. if ( ! empty($r['include']) ) { 
  1615. $incposts = wp_parse_id_list( $r['include'] ); 
  1616. $r['posts_per_page'] = count($incposts); // only the number of posts included 
  1617. $r['post__in'] = $incposts; 
  1618. } elseif ( ! empty($r['exclude']) ) 
  1619. $r['post__not_in'] = wp_parse_id_list( $r['exclude'] ); 
  1620.  
  1621. $r['ignore_sticky_posts'] = true; 
  1622. $r['no_found_rows'] = true; 
  1623.  
  1624. $get_posts = new WP_Query; 
  1625. return $get_posts->query($r); 
  1626.  
  1627.  
  1628. // 
  1629.  // Post meta functions 
  1630. // 
  1631.   
  1632. /** 
  1633. * Add meta data field to a post. 
  1634. * 
  1635. * Post meta data is called "Custom Fields" on the Administration Screen. 
  1636. * 
  1637. * @since 1.5.0 
  1638. * 
  1639. * @param int $post_id Post ID. 
  1640. * @param string $meta_key Metadata name. 
  1641. * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 
  1642. * @param bool $unique Optional. Whether the same key should not be added. 
  1643. * Default false. 
  1644. * @return int|false Meta ID on success, false on failure. 
  1645. */ 
  1646. function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) { 
  1647. // Make sure meta is added to the post, not a revision. 
  1648. if ( $the_post = wp_is_post_revision($post_id) ) 
  1649. $post_id = $the_post; 
  1650.  
  1651. return add_metadata('post', $post_id, $meta_key, $meta_value, $unique); 
  1652.  
  1653. /** 
  1654. * Remove metadata matching criteria from a post. 
  1655. * 
  1656. * You can match based on the key, or key and value. Removing based on key and 
  1657. * value, will keep from removing duplicate metadata with the same key. It also 
  1658. * allows removing all metadata matching key, if needed. 
  1659. * 
  1660. * @since 1.5.0 
  1661. * 
  1662. * @param int $post_id Post ID. 
  1663. * @param string $meta_key Metadata name. 
  1664. * @param mixed $meta_value Optional. Metadata value. Must be serializable if 
  1665. * non-scalar. Default empty. 
  1666. * @return bool True on success, false on failure. 
  1667. */ 
  1668. function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) { 
  1669. // Make sure meta is added to the post, not a revision. 
  1670. if ( $the_post = wp_is_post_revision($post_id) ) 
  1671. $post_id = $the_post; 
  1672.  
  1673. return delete_metadata('post', $post_id, $meta_key, $meta_value); 
  1674.  
  1675. /** 
  1676. * Retrieve post meta field for a post. 
  1677. * 
  1678. * @since 1.5.0 
  1679. * 
  1680. * @param int $post_id Post ID. 
  1681. * @param string $key Optional. The meta key to retrieve. By default, returns 
  1682. * data for all keys. Default empty. 
  1683. * @param bool $single Optional. Whether to return a single value. Default false. 
  1684. * @return mixed Will be an array if $single is false. Will be value of meta data 
  1685. * field if $single is true. 
  1686. */ 
  1687. function get_post_meta( $post_id, $key = '', $single = false ) { 
  1688. return get_metadata('post', $post_id, $key, $single); 
  1689.  
  1690. /** 
  1691. * Update post meta field based on post ID. 
  1692. * 
  1693. * Use the $prev_value parameter to differentiate between meta fields with the 
  1694. * same key and post ID. 
  1695. * 
  1696. * If the meta field for the post does not exist, it will be added. 
  1697. * 
  1698. * @since 1.5.0 
  1699. * 
  1700. * @param int $post_id Post ID. 
  1701. * @param string $meta_key Metadata key. 
  1702. * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 
  1703. * @param mixed $prev_value Optional. Previous value to check before removing. 
  1704. * Default empty. 
  1705. * @return int|bool Meta ID if the key didn't exist, true on successful update,  
  1706. * false on failure. 
  1707. */ 
  1708. function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) { 
  1709. // Make sure meta is added to the post, not a revision. 
  1710. if ( $the_post = wp_is_post_revision($post_id) ) 
  1711. $post_id = $the_post; 
  1712.  
  1713. return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value); 
  1714.  
  1715. /** 
  1716. * Delete everything from post meta matching meta key. 
  1717. * 
  1718. * @since 2.3.0 
  1719. * 
  1720. * @param string $post_meta_key Key to search for when deleting. 
  1721. * @return bool Whether the post meta key was deleted from the database. 
  1722. */ 
  1723. function delete_post_meta_by_key( $post_meta_key ) { 
  1724. return delete_metadata( 'post', null, $post_meta_key, '', true ); 
  1725.  
  1726. /** 
  1727. * Retrieve post meta fields, based on post ID. 
  1728. * 
  1729. * The post meta fields are retrieved from the cache where possible,  
  1730. * so the function is optimized to be called more than once. 
  1731. * 
  1732. * @since 1.2.0 
  1733. * 
  1734. * @param int $post_id Optional. Post ID. Default is ID of the global $post. 
  1735. * @return array Post meta for the given post. 
  1736. */ 
  1737. function get_post_custom( $post_id = 0 ) { 
  1738. $post_id = absint( $post_id ); 
  1739. if ( ! $post_id ) 
  1740. $post_id = get_the_ID(); 
  1741.  
  1742. return get_post_meta( $post_id ); 
  1743.  
  1744. /** 
  1745. * Retrieve meta field names for a post. 
  1746. * 
  1747. * If there are no meta fields, then nothing (null) will be returned. 
  1748. * 
  1749. * @since 1.2.0 
  1750. * 
  1751. * @param int $post_id Optional. Post ID. Default is ID of the global $post. 
  1752. * @return array|void Array of the keys, if retrieved. 
  1753. */ 
  1754. function get_post_custom_keys( $post_id = 0 ) { 
  1755. $custom = get_post_custom( $post_id ); 
  1756.  
  1757. if ( !is_array($custom) ) 
  1758. return; 
  1759.  
  1760. if ( $keys = array_keys($custom) ) 
  1761. return $keys; 
  1762.  
  1763. /** 
  1764. * Retrieve values for a custom post field. 
  1765. * 
  1766. * The parameters must not be considered optional. All of the post meta fields 
  1767. * will be retrieved and only the meta field key values returned. 
  1768. * 
  1769. * @since 1.2.0 
  1770. * 
  1771. * @param string $key Optional. Meta field key. Default empty. 
  1772. * @param int $post_id Optional. Post ID. Default is ID of the global $post. 
  1773. * @return array|null Meta field values. 
  1774. */ 
  1775. function get_post_custom_values( $key = '', $post_id = 0 ) { 
  1776. if ( !$key ) 
  1777. return null; 
  1778.  
  1779. $custom = get_post_custom($post_id); 
  1780.  
  1781. return isset($custom[$key]) ? $custom[$key] : null; 
  1782.  
  1783. /** 
  1784. * Check if post is sticky. 
  1785. * 
  1786. * Sticky posts should remain at the top of The Loop. If the post ID is not 
  1787. * given, then The Loop ID for the current post will be used. 
  1788. * 
  1789. * @since 2.7.0 
  1790. * 
  1791. * @param int $post_id Optional. Post ID. Default is ID of the global $post. 
  1792. * @return bool Whether post is sticky. 
  1793. */ 
  1794. function is_sticky( $post_id = 0 ) { 
  1795. $post_id = absint( $post_id ); 
  1796.  
  1797. if ( ! $post_id ) 
  1798. $post_id = get_the_ID(); 
  1799.  
  1800. $stickies = get_option( 'sticky_posts' ); 
  1801.  
  1802. if ( ! is_array( $stickies ) ) 
  1803. return false; 
  1804.  
  1805. if ( in_array( $post_id, $stickies ) ) 
  1806. return true; 
  1807.  
  1808. return false; 
  1809.  
  1810. /** 
  1811. * Sanitize every post field. 
  1812. * 
  1813. * If the context is 'raw', then the post object or array will get minimal 
  1814. * sanitization of the integer fields. 
  1815. * 
  1816. * @since 2.3.0 
  1817. * 
  1818. * @see sanitize_post_field() 
  1819. * 
  1820. * @param object|WP_Post|array $post The Post Object or Array 
  1821. * @param string $context Optional. How to sanitize post fields. 
  1822. * Accepts 'raw', 'edit', 'db', or 'display'. 
  1823. * Default 'display'. 
  1824. * @return object|WP_Post|array The now sanitized Post Object or Array (will be the 
  1825. * same type as $post). 
  1826. */ 
  1827. function sanitize_post( $post, $context = 'display' ) { 
  1828. if ( is_object($post) ) { 
  1829. // Check if post already filtered for this context. 
  1830. if ( isset($post->filter) && $context == $post->filter ) 
  1831. return $post; 
  1832. if ( !isset($post->ID) ) 
  1833. $post->ID = 0; 
  1834. foreach ( array_keys(get_object_vars($post)) as $field ) 
  1835. $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context); 
  1836. $post->filter = $context; 
  1837. } elseif ( is_array( $post ) ) { 
  1838. // Check if post already filtered for this context. 
  1839. if ( isset($post['filter']) && $context == $post['filter'] ) 
  1840. return $post; 
  1841. if ( !isset($post['ID']) ) 
  1842. $post['ID'] = 0; 
  1843. foreach ( array_keys($post) as $field ) 
  1844. $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context); 
  1845. $post['filter'] = $context; 
  1846. return $post; 
  1847.  
  1848. /** 
  1849. * Sanitize post field based on context. 
  1850. * 
  1851. * Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 
  1852. * 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts 
  1853. * are treated like 'display' when calling filters. 
  1854. * 
  1855. * @since 2.3.0 
  1856. * @since 4.4.0 Like `sanitize_post()`, `$context` defaults to 'display'. 
  1857. * 
  1858. * @param string $field The Post Object field name. 
  1859. * @param mixed $value The Post Object value. 
  1860. * @param int $post_id Post ID. 
  1861. * @param string $context Optional. How to sanitize post fields. Looks for 'raw', 'edit',  
  1862. * 'db', 'display', 'attribute' and 'js'. Default 'display'. 
  1863. * @return mixed Sanitized value. 
  1864. */ 
  1865. function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) { 
  1866. $int_fields = array('ID', 'post_parent', 'menu_order'); 
  1867. if ( in_array($field, $int_fields) ) 
  1868. $value = (int) $value; 
  1869.  
  1870. // Fields which contain arrays of integers. 
  1871. $array_int_fields = array( 'ancestors' ); 
  1872. if ( in_array($field, $array_int_fields) ) { 
  1873. $value = array_map( 'absint', $value); 
  1874. return $value; 
  1875.  
  1876. if ( 'raw' == $context ) 
  1877. return $value; 
  1878.  
  1879. $prefixed = false; 
  1880. if ( false !== strpos($field, 'post_') ) { 
  1881. $prefixed = true; 
  1882. $field_no_prefix = str_replace('post_', '', $field); 
  1883.  
  1884. if ( 'edit' == $context ) { 
  1885. $format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password'); 
  1886.  
  1887. if ( $prefixed ) { 
  1888.  
  1889. /** 
  1890. * Filters the value of a specific post field to edit. 
  1891. * 
  1892. * The dynamic portion of the hook name, `$field`, refers to the post 
  1893. * field name. 
  1894. * 
  1895. * @since 2.3.0 
  1896. * 
  1897. * @param mixed $value Value of the post field. 
  1898. * @param int $post_id Post ID. 
  1899. */ 
  1900. $value = apply_filters( "edit_{$field}", $value, $post_id ); 
  1901.  
  1902. /** 
  1903. * Filters the value of a specific post field to edit. 
  1904. * 
  1905. * The dynamic portion of the hook name, `$field_no_prefix`, refers to 
  1906. * the post field name. 
  1907. * 
  1908. * @since 2.3.0 
  1909. * 
  1910. * @param mixed $value Value of the post field. 
  1911. * @param int $post_id Post ID. 
  1912. */ 
  1913. $value = apply_filters( "{$field_no_prefix}_edit_pre", $value, $post_id ); 
  1914. } else { 
  1915. $value = apply_filters( "edit_post_{$field}", $value, $post_id ); 
  1916.  
  1917. if ( in_array($field, $format_to_edit) ) { 
  1918. if ( 'post_content' == $field ) 
  1919. $value = format_to_edit($value, user_can_richedit()); 
  1920. else 
  1921. $value = format_to_edit($value); 
  1922. } else { 
  1923. $value = esc_attr($value); 
  1924. } elseif ( 'db' == $context ) { 
  1925. if ( $prefixed ) { 
  1926.  
  1927. /** 
  1928. * Filters the value of a specific post field before saving. 
  1929. * 
  1930. * The dynamic portion of the hook name, `$field`, refers to the post 
  1931. * field name. 
  1932. * 
  1933. * @since 2.3.0 
  1934. * 
  1935. * @param mixed $value Value of the post field. 
  1936. */ 
  1937. $value = apply_filters( "pre_{$field}", $value ); 
  1938.  
  1939. /** 
  1940. * Filters the value of a specific field before saving. 
  1941. * 
  1942. * The dynamic portion of the hook name, `$field_no_prefix`, refers 
  1943. * to the post field name. 
  1944. * 
  1945. * @since 2.3.0 
  1946. * 
  1947. * @param mixed $value Value of the post field. 
  1948. */ 
  1949. $value = apply_filters( "{$field_no_prefix}_save_pre", $value ); 
  1950. } else { 
  1951. $value = apply_filters( "pre_post_{$field}", $value ); 
  1952.  
  1953. /** 
  1954. * Filters the value of a specific post field before saving. 
  1955. * 
  1956. * The dynamic portion of the hook name, `$field`, refers to the post 
  1957. * field name. 
  1958. * 
  1959. * @since 2.3.0 
  1960. * 
  1961. * @param mixed $value Value of the post field. 
  1962. */ 
  1963. $value = apply_filters( "{$field}_pre", $value ); 
  1964. } else { 
  1965.  
  1966. // Use display filters by default. 
  1967. if ( $prefixed ) { 
  1968.  
  1969. /** 
  1970. * Filters the value of a specific post field for display. 
  1971. * 
  1972. * The dynamic portion of the hook name, `$field`, refers to the post 
  1973. * field name. 
  1974. * 
  1975. * @since 2.3.0 
  1976. * 
  1977. * @param mixed $value Value of the prefixed post field. 
  1978. * @param int $post_id Post ID. 
  1979. * @param string $context Context for how to sanitize the field. Possible 
  1980. * values include 'raw', 'edit', 'db', 'display',  
  1981. * 'attribute' and 'js'. 
  1982. */ 
  1983. $value = apply_filters( $field, $value, $post_id, $context ); 
  1984. } else { 
  1985. $value = apply_filters( "post_{$field}", $value, $post_id, $context ); 
  1986.  
  1987. if ( 'attribute' == $context ) { 
  1988. $value = esc_attr( $value ); 
  1989. } elseif ( 'js' == $context ) { 
  1990. $value = esc_js( $value ); 
  1991.  
  1992. return $value; 
  1993.  
  1994. /** 
  1995. * Make a post sticky. 
  1996. * 
  1997. * Sticky posts should be displayed at the top of the front page. 
  1998. * 
  1999. * @since 2.7.0 
  2000. * 
  2001. * @param int $post_id Post ID. 
  2002. */ 
  2003. function stick_post( $post_id ) { 
  2004. $stickies = get_option('sticky_posts'); 
  2005.  
  2006. if ( !is_array($stickies) ) 
  2007. $stickies = array($post_id); 
  2008.  
  2009. if ( ! in_array($post_id, $stickies) ) 
  2010. $stickies[] = $post_id; 
  2011.  
  2012. $updated = update_option( 'sticky_posts', $stickies ); 
  2013.  
  2014. if ( $updated ) { 
  2015. /** 
  2016. * Fires once a post has been added to the sticky list. 
  2017. * 
  2018. * @since 4.6.0 
  2019. * 
  2020. * @param int $post_id ID of the post that was stuck. 
  2021. */ 
  2022. do_action( 'post_stuck', $post_id ); 
  2023.  
  2024. /** 
  2025. * Un-stick a post. 
  2026. * 
  2027. * Sticky posts should be displayed at the top of the front page. 
  2028. * 
  2029. * @since 2.7.0 
  2030. * 
  2031. * @param int $post_id Post ID. 
  2032. */ 
  2033. function unstick_post( $post_id ) { 
  2034. $stickies = get_option('sticky_posts'); 
  2035.  
  2036. if ( !is_array($stickies) ) 
  2037. return; 
  2038.  
  2039. if ( ! in_array($post_id, $stickies) ) 
  2040. return; 
  2041.  
  2042. $offset = array_search($post_id, $stickies); 
  2043. if ( false === $offset ) 
  2044. return; 
  2045.  
  2046. array_splice($stickies, $offset, 1); 
  2047.  
  2048. $updated = update_option( 'sticky_posts', $stickies ); 
  2049.  
  2050. if ( $updated ) { 
  2051. /** 
  2052. * Fires once a post has been removed from the sticky list. 
  2053. * 
  2054. * @since 4.6.0 
  2055. * 
  2056. * @param int $post_id ID of the post that was unstuck. 
  2057. */ 
  2058. do_action( 'post_unstuck', $post_id ); 
  2059.  
  2060. /** 
  2061. * Return the cache key for wp_count_posts() based on the passed arguments. 
  2062. * 
  2063. * @since 3.9.0 
  2064. * 
  2065. * @param string $type Optional. Post type to retrieve count Default 'post'. 
  2066. * @param string $perm Optional. 'readable' or empty. Default empty. 
  2067. * @return string The cache key. 
  2068. */ 
  2069. function _count_posts_cache_key( $type = 'post', $perm = '' ) { 
  2070. $cache_key = 'posts-' . $type; 
  2071. if ( 'readable' == $perm && is_user_logged_in() ) { 
  2072. $post_type_object = get_post_type_object( $type ); 
  2073. if ( $post_type_object && ! current_user_can( $post_type_object->cap->read_private_posts ) ) { 
  2074. $cache_key .= '_' . $perm . '_' . get_current_user_id(); 
  2075. return $cache_key; 
  2076.  
  2077. /** 
  2078. * Count number of posts of a post type and if user has permissions to view. 
  2079. * 
  2080. * This function provides an efficient method of finding the amount of post's 
  2081. * type a blog has. Another method is to count the amount of items in 
  2082. * get_posts(), but that method has a lot of overhead with doing so. Therefore,  
  2083. * when developing for 2.5+, use this function instead. 
  2084. * 
  2085. * The $perm parameter checks for 'readable' value and if the user can read 
  2086. * private posts, it will display that for the user that is signed in. 
  2087. * 
  2088. * @since 2.5.0 
  2089. * 
  2090. * @global wpdb $wpdb WordPress database abstraction object. 
  2091. * 
  2092. * @param string $type Optional. Post type to retrieve count. Default 'post'. 
  2093. * @param string $perm Optional. 'readable' or empty. Default empty. 
  2094. * @return object Number of posts for each status. 
  2095. */ 
  2096. function wp_count_posts( $type = 'post', $perm = '' ) { 
  2097. global $wpdb; 
  2098.  
  2099. if ( ! post_type_exists( $type ) ) 
  2100. return new stdClass; 
  2101.  
  2102. $cache_key = _count_posts_cache_key( $type, $perm ); 
  2103.  
  2104. $counts = wp_cache_get( $cache_key, 'counts' ); 
  2105. if ( false !== $counts ) { 
  2106. /** This filter is documented in wp-includes/post.php */ 
  2107. return apply_filters( 'wp_count_posts', $counts, $type, $perm ); 
  2108.  
  2109. $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; 
  2110. if ( 'readable' == $perm && is_user_logged_in() ) { 
  2111. $post_type_object = get_post_type_object($type); 
  2112. if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) { 
  2113. $query .= $wpdb->prepare( " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",  
  2114. get_current_user_id() 
  2115. ); 
  2116. $query .= ' GROUP BY post_status'; 
  2117.  
  2118. $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); 
  2119. $counts = array_fill_keys( get_post_stati(), 0 ); 
  2120.  
  2121. foreach ( $results as $row ) { 
  2122. $counts[ $row['post_status'] ] = $row['num_posts']; 
  2123.  
  2124. $counts = (object) $counts; 
  2125. wp_cache_set( $cache_key, $counts, 'counts' ); 
  2126.  
  2127. /** 
  2128. * Modify returned post counts by status for the current post type. 
  2129. * 
  2130. * @since 3.7.0 
  2131. * 
  2132. * @param object $counts An object containing the current post_type's post 
  2133. * counts by status. 
  2134. * @param string $type Post type. 
  2135. * @param string $perm The permission to determine if the posts are 'readable' 
  2136. * by the current user. 
  2137. */ 
  2138. return apply_filters( 'wp_count_posts', $counts, $type, $perm ); 
  2139.  
  2140. /** 
  2141. * Count number of attachments for the mime type(s). 
  2142. * 
  2143. * If you set the optional mime_type parameter, then an array will still be 
  2144. * returned, but will only have the item you are looking for. It does not give 
  2145. * you the number of attachments that are children of a post. You can get that 
  2146. * by counting the number of children that post has. 
  2147. * 
  2148. * @since 2.5.0 
  2149. * 
  2150. * @global wpdb $wpdb WordPress database abstraction object. 
  2151. * 
  2152. * @param string|array $mime_type Optional. Array or comma-separated list of 
  2153. * MIME patterns. Default empty. 
  2154. * @return object An object containing the attachment counts by mime type. 
  2155. */ 
  2156. function wp_count_attachments( $mime_type = '' ) { 
  2157. global $wpdb; 
  2158.  
  2159. $and = wp_post_mime_type_where( $mime_type ); 
  2160. $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A ); 
  2161.  
  2162. $counts = array(); 
  2163. foreach ( (array) $count as $row ) { 
  2164. $counts[ $row['post_mime_type'] ] = $row['num_posts']; 
  2165. $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and"); 
  2166.  
  2167. /** 
  2168. * Modify returned attachment counts by mime type. 
  2169. * 
  2170. * @since 3.7.0 
  2171. * 
  2172. * @param object $counts An object containing the attachment counts by 
  2173. * mime type. 
  2174. * @param string $mime_type The mime type pattern used to filter the attachments 
  2175. * counted. 
  2176. */ 
  2177. return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type ); 
  2178.  
  2179. /** 
  2180. * Get default post mime types. 
  2181. * 
  2182. * @since 2.9.0 
  2183. * 
  2184. * @return array List of post mime types. 
  2185. */ 
  2186. function get_post_mime_types() { 
  2187. $post_mime_types = array( // array( adj, noun ) 
  2188. 'image' => array(__('Images'), __('Manage Images'), _n_noop('Image <span class="count">(%s)</span>', 'Images <span class="count">(%s)</span>')),  
  2189. 'audio' => array(__('Audio'), __('Manage Audio'), _n_noop('Audio <span class="count">(%s)</span>', 'Audio <span class="count">(%s)</span>')),  
  2190. 'video' => array(__('Video'), __('Manage Video'), _n_noop('Video <span class="count">(%s)</span>', 'Video <span class="count">(%s)</span>')),  
  2191. ); 
  2192.  
  2193. /** 
  2194. * Filters the default list of post mime types. 
  2195. * 
  2196. * @since 2.5.0 
  2197. * 
  2198. * @param array $post_mime_types Default list of post mime types. 
  2199. */ 
  2200. return apply_filters( 'post_mime_types', $post_mime_types ); 
  2201.  
  2202. /** 
  2203. * Check a MIME-Type against a list. 
  2204. * 
  2205. * If the wildcard_mime_types parameter is a string, it must be comma separated 
  2206. * list. If the real_mime_types is a string, it is also comma separated to 
  2207. * create the list. 
  2208. * 
  2209. * @since 2.5.0 
  2210. * 
  2211. * @param string|array $wildcard_mime_types Mime types, e.g. audio/mpeg or image (same as image/**) 
  2212. * or flash (same as *flash*). 
  2213. * @param string|array $real_mime_types Real post mime type values. 
  2214. * @return array array(wildcard=>array(real types)). 
  2215. */ 
  2216. function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) { 
  2217. $matches = array(); 
  2218. if ( is_string( $wildcard_mime_types ) ) { 
  2219. $wildcard_mime_types = array_map( 'trim', explode( ', ', $wildcard_mime_types ) ); 
  2220. if ( is_string( $real_mime_types ) ) { 
  2221. $real_mime_types = array_map( 'trim', explode( ', ', $real_mime_types ) ); 
  2222.  
  2223. $patternses = array(); 
  2224. $wild = '[-._a-z0-9]*'; 
  2225.  
  2226. foreach ( (array) $wildcard_mime_types as $type ) { 
  2227. $mimes = array_map( 'trim', explode( ', ', $type ) ); 
  2228. foreach ( $mimes as $mime ) { 
  2229. $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) ); 
  2230. $patternses[][$type] = "^$regex$"; 
  2231. if ( false === strpos( $mime, '/' ) ) { 
  2232. $patternses[][$type] = "^$regex/"; 
  2233. $patternses[][$type] = $regex; 
  2234. asort( $patternses ); 
  2235.  
  2236. foreach ( $patternses as $patterns ) { 
  2237. foreach ( $patterns as $type => $pattern ) { 
  2238. foreach ( (array) $real_mime_types as $real ) { 
  2239. if ( preg_match( "#$pattern#", $real ) && ( empty( $matches[$type] ) || false === array_search( $real, $matches[$type] ) ) ) { 
  2240. $matches[$type][] = $real; 
  2241. return $matches; 
  2242.  
  2243. /** 
  2244. * Convert MIME types into SQL. 
  2245. * 
  2246. * @since 2.5.0 
  2247. * 
  2248. * @param string|array $post_mime_types List of mime types or comma separated string 
  2249. * of mime types. 
  2250. * @param string $table_alias Optional. Specify a table alias, if needed. 
  2251. * Default empty. 
  2252. * @return string The SQL AND clause for mime searching. 
  2253. */ 
  2254. function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) { 
  2255. $where = ''; 
  2256. $wildcards = array('', '%', '%/%'); 
  2257. if ( is_string($post_mime_types) ) 
  2258. $post_mime_types = array_map('trim', explode(', ', $post_mime_types)); 
  2259.  
  2260. $wheres = array(); 
  2261.  
  2262. foreach ( (array) $post_mime_types as $mime_type ) { 
  2263. $mime_type = preg_replace('/\s/', '', $mime_type); 
  2264. $slashpos = strpos($mime_type, '/'); 
  2265. if ( false !== $slashpos ) { 
  2266. $mime_group = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, 0, $slashpos)); 
  2267. $mime_subgroup = preg_replace('/[^-*.+a-zA-Z0-9]/', '', substr($mime_type, $slashpos + 1)); 
  2268. if ( empty($mime_subgroup) ) 
  2269. $mime_subgroup = '*'; 
  2270. else 
  2271. $mime_subgroup = str_replace('/', '', $mime_subgroup); 
  2272. $mime_pattern = "$mime_group/$mime_subgroup"; 
  2273. } else { 
  2274. $mime_pattern = preg_replace('/[^-*.a-zA-Z0-9]/', '', $mime_type); 
  2275. if ( false === strpos($mime_pattern, '*') ) 
  2276. $mime_pattern .= '/*'; 
  2277. } 
  2278.   
  2279. $mime_pattern = preg_replace('/\*+/', '%', $mime_pattern); 
  2280.   
  2281. if ( in_array( $mime_type, $wildcards ) ) 
  2282. return ''; 
  2283.   
  2284. if ( false !== strpos($mime_pattern, '%') ) 
  2285. $wheres[] = empty($table_alias) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'"; 
  2286. else 
  2287. $wheres[] = empty($table_alias) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'"; 
  2288. } 
  2289. if ( !empty($wheres) ) 
  2290. $where = ' AND (' . join(' OR ', $wheres) . ') '; 
  2291. return $where; 
  2292. } 
  2293.   
  2294. /** 
  2295. * Trash or delete a post or page. 
  2296. * 
  2297. * When the post and page is permanently deleted, everything that is tied to 
  2298. * it is deleted also. This includes comments, post meta fields, and terms 
  2299. * associated with the post. 
  2300. * 
  2301. * The post or page is moved to trash instead of permanently deleted unless 
  2302. * trash is disabled, item is already in the trash, or $force_delete is true. 
  2303. * 
  2304. * @since 1.0.0 
  2305. * 
  2306. * @global wpdb $wpdb WordPress database abstraction object. 
  2307. * @see wp_delete_attachment() 
  2308. * @see wp_trash_post() 
  2309. * 
  2310. * @param int $postid Optional. Post ID. Default 0. 
  2311. * @param bool $force_delete Optional. Whether to bypass trash and force deletion. 
  2312. * Default false. 
  2313. * @return array|false|WP_Post False on failure. 
  2314. */ 
  2315. function wp_delete_post( $postid = 0, $force_delete = false ) { 
  2316. global $wpdb; 
  2317.  
  2318. if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) ) 
  2319. return $post; 
  2320.  
  2321. if ( !$force_delete && ( $post->post_type == 'post' || $post->post_type == 'page') && get_post_status( $postid ) != 'trash' && EMPTY_TRASH_DAYS ) 
  2322. return wp_trash_post( $postid ); 
  2323.  
  2324. if ( $post->post_type == 'attachment' ) 
  2325. return wp_delete_attachment( $postid, $force_delete ); 
  2326.  
  2327. /** 
  2328. * Filters whether a post deletion should take place. 
  2329. * 
  2330. * @since 4.4.0 
  2331. * 
  2332. * @param bool $delete Whether to go forward with deletion. 
  2333. * @param WP_Post $post Post object. 
  2334. * @param bool $force_delete Whether to bypass the trash. 
  2335. */ 
  2336. $check = apply_filters( 'pre_delete_post', null, $post, $force_delete ); 
  2337. if ( null !== $check ) { 
  2338. return $check; 
  2339.  
  2340. /** 
  2341. * Fires before a post is deleted, at the start of wp_delete_post(). 
  2342. * 
  2343. * @since 3.2.0 
  2344. * 
  2345. * @see wp_delete_post() 
  2346. * 
  2347. * @param int $postid Post ID. 
  2348. */ 
  2349. do_action( 'before_delete_post', $postid ); 
  2350.  
  2351. delete_post_meta($postid, '_wp_trash_meta_status'); 
  2352. delete_post_meta($postid, '_wp_trash_meta_time'); 
  2353.  
  2354. wp_delete_object_term_relationships($postid, get_object_taxonomies($post->post_type)); 
  2355.  
  2356. $parent_data = array( 'post_parent' => $post->post_parent ); 
  2357. $parent_where = array( 'post_parent' => $postid ); 
  2358.  
  2359. if ( is_post_type_hierarchical( $post->post_type ) ) { 
  2360. // Point children of this page to its parent, also clean the cache of affected children. 
  2361. $children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type ); 
  2362. $children = $wpdb->get_results( $children_query ); 
  2363. if ( $children ) { 
  2364. $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) ); 
  2365.  
  2366. // Do raw query. wp_get_post_revisions() is filtered. 
  2367. $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) ); 
  2368. // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. 
  2369. foreach ( $revision_ids as $revision_id ) 
  2370. wp_delete_post_revision( $revision_id ); 
  2371.  
  2372. // Point all attachments to this post up one level. 
  2373. $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); 
  2374.  
  2375. wp_defer_comment_counting( true ); 
  2376.  
  2377. $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid )); 
  2378. foreach ( $comment_ids as $comment_id ) { 
  2379. wp_delete_comment( $comment_id, true ); 
  2380.  
  2381. wp_defer_comment_counting( false ); 
  2382.  
  2383. $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid )); 
  2384. foreach ( $post_meta_ids as $mid ) 
  2385. delete_metadata_by_mid( 'post', $mid ); 
  2386.  
  2387. /** 
  2388. * Fires immediately before a post is deleted from the database. 
  2389. * 
  2390. * @since 1.2.0 
  2391. * 
  2392. * @param int $postid Post ID. 
  2393. */ 
  2394. do_action( 'delete_post', $postid ); 
  2395. $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) ); 
  2396. if ( ! $result ) { 
  2397. return false; 
  2398.  
  2399. /** 
  2400. * Fires immediately after a post is deleted from the database. 
  2401. * 
  2402. * @since 2.2.0 
  2403. * 
  2404. * @param int $postid Post ID. 
  2405. */ 
  2406. do_action( 'deleted_post', $postid ); 
  2407.  
  2408. clean_post_cache( $post ); 
  2409.  
  2410. if ( is_post_type_hierarchical( $post->post_type ) && $children ) { 
  2411. foreach ( $children as $child ) 
  2412. clean_post_cache( $child ); 
  2413.  
  2414. wp_clear_scheduled_hook('publish_future_post', array( $postid ) ); 
  2415.  
  2416. /** 
  2417. * Fires after a post is deleted, at the conclusion of wp_delete_post(). 
  2418. * 
  2419. * @since 3.2.0 
  2420. * 
  2421. * @see wp_delete_post() 
  2422. * 
  2423. * @param int $postid Post ID. 
  2424. */ 
  2425. do_action( 'after_delete_post', $postid ); 
  2426.  
  2427. return $post; 
  2428.  
  2429. /** 
  2430. * Reset the page_on_front, show_on_front, and page_for_post settings when 
  2431. * a linked page is deleted or trashed. 
  2432. * 
  2433. * Also ensures the post is no longer sticky. 
  2434. * 
  2435. * @since 3.7.0 
  2436. * @access private 
  2437. * 
  2438. * @param int $post_id Post ID. 
  2439. */ 
  2440. function _reset_front_page_settings_for_post( $post_id ) { 
  2441. $post = get_post( $post_id ); 
  2442. if ( 'page' == $post->post_type ) { 
  2443. /** 
  2444. * If the page is defined in option page_on_front or post_for_posts,  
  2445. * adjust the corresponding options. 
  2446. */ 
  2447. if ( get_option( 'page_on_front' ) == $post->ID ) { 
  2448. update_option( 'show_on_front', 'posts' ); 
  2449. update_option( 'page_on_front', 0 ); 
  2450. if ( get_option( 'page_for_posts' ) == $post->ID ) { 
  2451. delete_option( 'page_for_posts', 0 ); 
  2452. unstick_post( $post->ID ); 
  2453.  
  2454. /** 
  2455. * Move a post or page to the Trash 
  2456. * 
  2457. * If trash is disabled, the post or page is permanently deleted. 
  2458. * 
  2459. * @since 2.9.0 
  2460. * 
  2461. * @see wp_delete_post() 
  2462. * 
  2463. * @param int $post_id Optional. Post ID. Default is ID of the global $post 
  2464. * if EMPTY_TRASH_DAYS equals true. 
  2465. * @return false|array|WP_Post|null Post data array, otherwise false. 
  2466. */ 
  2467. function wp_trash_post( $post_id = 0 ) { 
  2468. if ( !EMPTY_TRASH_DAYS ) 
  2469. return wp_delete_post($post_id, true); 
  2470.  
  2471. if ( !$post = get_post($post_id, ARRAY_A) ) 
  2472. return $post; 
  2473.  
  2474. if ( $post['post_status'] == 'trash' ) 
  2475. return false; 
  2476.  
  2477. /** 
  2478. * Fires before a post is sent to the trash. 
  2479. * 
  2480. * @since 3.3.0 
  2481. * 
  2482. * @param int $post_id Post ID. 
  2483. */ 
  2484. do_action( 'wp_trash_post', $post_id ); 
  2485.  
  2486. add_post_meta($post_id, '_wp_trash_meta_status', $post['post_status']); 
  2487. add_post_meta($post_id, '_wp_trash_meta_time', time()); 
  2488.  
  2489. $post['post_status'] = 'trash'; 
  2490. wp_insert_post( wp_slash( $post ) ); 
  2491.  
  2492. wp_trash_post_comments($post_id); 
  2493.  
  2494. /** 
  2495. * Fires after a post is sent to the trash. 
  2496. * 
  2497. * @since 2.9.0 
  2498. * 
  2499. * @param int $post_id Post ID. 
  2500. */ 
  2501. do_action( 'trashed_post', $post_id ); 
  2502.  
  2503. return $post; 
  2504.  
  2505. /** 
  2506. * Restore a post or page from the Trash. 
  2507. * 
  2508. * @since 2.9.0 
  2509. * 
  2510. * @param int $post_id Optional. Post ID. Default is ID of the global $post. 
  2511. * @return WP_Post|false WP_Post object. False on failure. 
  2512. */ 
  2513. function wp_untrash_post( $post_id = 0 ) { 
  2514. if ( !$post = get_post($post_id, ARRAY_A) ) 
  2515. return $post; 
  2516.  
  2517. if ( $post['post_status'] != 'trash' ) 
  2518. return false; 
  2519.  
  2520. /** 
  2521. * Fires before a post is restored from the trash. 
  2522. * 
  2523. * @since 2.9.0 
  2524. * 
  2525. * @param int $post_id Post ID. 
  2526. */ 
  2527. do_action( 'untrash_post', $post_id ); 
  2528.  
  2529. $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true); 
  2530.  
  2531. $post['post_status'] = $post_status; 
  2532.  
  2533. delete_post_meta($post_id, '_wp_trash_meta_status'); 
  2534. delete_post_meta($post_id, '_wp_trash_meta_time'); 
  2535.  
  2536. wp_insert_post( wp_slash( $post ) ); 
  2537.  
  2538. wp_untrash_post_comments($post_id); 
  2539.  
  2540. /** 
  2541. * Fires after a post is restored from the trash. 
  2542. * 
  2543. * @since 2.9.0 
  2544. * 
  2545. * @param int $post_id Post ID. 
  2546. */ 
  2547. do_action( 'untrashed_post', $post_id ); 
  2548.  
  2549. return $post; 
  2550.  
  2551. /** 
  2552. * Moves comments for a post to the trash. 
  2553. * 
  2554. * @since 2.9.0 
  2555. * 
  2556. * @global wpdb $wpdb WordPress database abstraction object. 
  2557. * 
  2558. * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. 
  2559. * @return mixed|void False on failure. 
  2560. */ 
  2561. function wp_trash_post_comments( $post = null ) { 
  2562. global $wpdb; 
  2563.  
  2564. $post = get_post($post); 
  2565. if ( empty($post) ) 
  2566. return; 
  2567.  
  2568. $post_id = $post->ID; 
  2569.  
  2570. /** 
  2571. * Fires before comments are sent to the trash. 
  2572. * 
  2573. * @since 2.9.0 
  2574. * 
  2575. * @param int $post_id Post ID. 
  2576. */ 
  2577. do_action( 'trash_post_comments', $post_id ); 
  2578.  
  2579. $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id) ); 
  2580. if ( empty($comments) ) 
  2581. return; 
  2582.  
  2583. // Cache current status for each comment. 
  2584. $statuses = array(); 
  2585. foreach ( $comments as $comment ) 
  2586. $statuses[$comment->comment_ID] = $comment->comment_approved; 
  2587. add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses); 
  2588.  
  2589. // Set status for all comments to post-trashed. 
  2590. $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id)); 
  2591.  
  2592. clean_comment_cache( array_keys($statuses) ); 
  2593.  
  2594. /** 
  2595. * Fires after comments are sent to the trash. 
  2596. * 
  2597. * @since 2.9.0 
  2598. * 
  2599. * @param int $post_id Post ID. 
  2600. * @param array $statuses Array of comment statuses. 
  2601. */ 
  2602. do_action( 'trashed_post_comments', $post_id, $statuses ); 
  2603.  
  2604. return $result; 
  2605.  
  2606. /** 
  2607. * Restore comments for a post from the trash. 
  2608. * 
  2609. * @since 2.9.0 
  2610. * 
  2611. * @global wpdb $wpdb WordPress database abstraction object. 
  2612. * 
  2613. * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. 
  2614. * @return true|void 
  2615. */ 
  2616. function wp_untrash_post_comments( $post = null ) { 
  2617. global $wpdb; 
  2618.  
  2619. $post = get_post($post); 
  2620. if ( empty($post) ) 
  2621. return; 
  2622.  
  2623. $post_id = $post->ID; 
  2624.  
  2625. $statuses = get_post_meta($post_id, '_wp_trash_meta_comments_status', true); 
  2626.  
  2627. if ( empty($statuses) ) 
  2628. return true; 
  2629.  
  2630. /** 
  2631. * Fires before comments are restored for a post from the trash. 
  2632. * 
  2633. * @since 2.9.0 
  2634. * 
  2635. * @param int $post_id Post ID. 
  2636. */ 
  2637. do_action( 'untrash_post_comments', $post_id ); 
  2638.  
  2639. // Restore each comment to its original status. 
  2640. $group_by_status = array(); 
  2641. foreach ( $statuses as $comment_id => $comment_status ) 
  2642. $group_by_status[$comment_status][] = $comment_id; 
  2643.  
  2644. foreach ( $group_by_status as $status => $comments ) { 
  2645. // Sanity check. This shouldn't happen. 
  2646. if ( 'post-trashed' == $status ) { 
  2647. $status = '0'; 
  2648. $comments_in = implode( ', ', array_map( 'intval', $comments ) ); 
  2649. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) ); 
  2650.  
  2651. clean_comment_cache( array_keys($statuses) ); 
  2652.  
  2653. delete_post_meta($post_id, '_wp_trash_meta_comments_status'); 
  2654.  
  2655. /** 
  2656. * Fires after comments are restored for a post from the trash. 
  2657. * 
  2658. * @since 2.9.0 
  2659. * 
  2660. * @param int $post_id Post ID. 
  2661. */ 
  2662. do_action( 'untrashed_post_comments', $post_id ); 
  2663.  
  2664. /** 
  2665. * Retrieve the list of categories for a post. 
  2666. * 
  2667. * Compatibility layer for themes and plugins. Also an easy layer of abstraction 
  2668. * away from the complexity of the taxonomy layer. 
  2669. * 
  2670. * @since 2.1.0 
  2671. * 
  2672. * @see wp_get_object_terms() 
  2673. * 
  2674. * @param int $post_id Optional. The Post ID. Does not default to the ID of the 
  2675. * global $post. Default 0. 
  2676. * @param array $args Optional. Category arguments. See wp_get_object_terms(). Default empty. 
  2677. * @return array List of categories. If the `$fields` argument passed via `$args` is 'all' or 
  2678. * 'all_with_object_id', an array of WP_Term objects will be returned. If `$fields` 
  2679. * is 'ids', an array of category ids. If `$fields` is 'names', an array of category names. 
  2680. */ 
  2681. function wp_get_post_categories( $post_id = 0, $args = array() ) { 
  2682. $post_id = (int) $post_id; 
  2683.  
  2684. $defaults = array('fields' => 'ids'); 
  2685. $args = wp_parse_args( $args, $defaults ); 
  2686.  
  2687. $cats = wp_get_object_terms($post_id, 'category', $args); 
  2688. return $cats; 
  2689.  
  2690. /** 
  2691. * Retrieve the tags for a post. 
  2692. * 
  2693. * There is only one default for this function, called 'fields' and by default 
  2694. * is set to 'all'. There are other defaults that can be overridden in 
  2695. * wp_get_object_terms(). 
  2696. * 
  2697. * @since 2.3.0 
  2698. * 
  2699. * @param int $post_id Optional. The Post ID. Does not default to the ID of the 
  2700. * global $post. Default 0. 
  2701. * @param array $args Optional. Overwrite the defaults 
  2702. * @return array List of post tags. 
  2703. */ 
  2704. function wp_get_post_tags( $post_id = 0, $args = array() ) { 
  2705. return wp_get_post_terms( $post_id, 'post_tag', $args); 
  2706.  
  2707. /** 
  2708. * Retrieve the terms for a post. 
  2709. * 
  2710. * There is only one default for this function, called 'fields' and by default 
  2711. * is set to 'all'. There are other defaults that can be overridden in 
  2712. * wp_get_object_terms(). 
  2713. * 
  2714. * @since 2.8.0 
  2715. * 
  2716. * @param int $post_id Optional. The Post ID. Does not default to the ID of the 
  2717. * global $post. Default 0. 
  2718. * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'. 
  2719. * @param array $args Optional. wp_get_object_terms() arguments. Default empty array. 
  2720. * @return array|WP_Error List of post terms or empty array if no terms were found. WP_Error object 
  2721. * if `$taxonomy` doesn't exist. 
  2722. */ 
  2723. function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) { 
  2724. $post_id = (int) $post_id; 
  2725.  
  2726. $defaults = array('fields' => 'all'); 
  2727. $args = wp_parse_args( $args, $defaults ); 
  2728.  
  2729. $tags = wp_get_object_terms($post_id, $taxonomy, $args); 
  2730.  
  2731. return $tags; 
  2732.  
  2733. /** 
  2734. * Retrieve a number of recent posts. 
  2735. * 
  2736. * @since 1.0.0 
  2737. * 
  2738. * @see get_posts() 
  2739. * 
  2740. * @param array $args Optional. Arguments to retrieve posts. Default empty array. 
  2741. * @param string $output Optional. The required return type. One of OBJECT or ARRAY_A, which correspond to 
  2742. * a WP_Post object or an associative array, respectively. Default ARRAY_A. 
  2743. * @return array|false Array of recent posts, where the type of each element is determined by $output parameter. 
  2744. * Empty array on failure. 
  2745. */ 
  2746. function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { 
  2747.  
  2748. if ( is_numeric( $args ) ) { 
  2749. _deprecated_argument( __FUNCTION__, '3.1.0', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) ); 
  2750. $args = array( 'numberposts' => absint( $args ) ); 
  2751.  
  2752. // Set default arguments. 
  2753. $defaults = array( 
  2754. 'numberposts' => 10, 'offset' => 0,  
  2755. 'category' => 0, 'orderby' => 'post_date',  
  2756. 'order' => 'DESC', 'include' => '',  
  2757. 'exclude' => '', 'meta_key' => '',  
  2758. 'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private',  
  2759. 'suppress_filters' => true 
  2760. ); 
  2761.  
  2762. $r = wp_parse_args( $args, $defaults ); 
  2763.  
  2764. $results = get_posts( $r ); 
  2765.  
  2766. // Backward compatibility. Prior to 3.1 expected posts to be returned in array. 
  2767. if ( ARRAY_A == $output ) { 
  2768. foreach ( $results as $key => $result ) { 
  2769. $results[$key] = get_object_vars( $result ); 
  2770. return $results ? $results : array(); 
  2771.  
  2772. return $results ? $results : false; 
  2773.  
  2774.  
  2775. /** 
  2776. * Insert or update a post. 
  2777. * 
  2778. * If the $postarr parameter has 'ID' set to a value, then post will be updated. 
  2779. * 
  2780. * You can set the post date manually, by setting the values for 'post_date' 
  2781. * and 'post_date_gmt' keys. You can close the comments or open the comments by 
  2782. * setting the value for 'comment_status' key. 
  2783. * 
  2784. * @since 1.0.0 
  2785. * @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt. 
  2786. * @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data. 
  2787. * 
  2788. * @see sanitize_post() 
  2789. * @global wpdb $wpdb WordPress database abstraction object. 
  2790. * 
  2791. * @param array $postarr { 
  2792. * An array of elements that make up a post to update or insert. 
  2793. * 
  2794. * @type int $ID The post ID. If equal to something other than 0,  
  2795. * the post with that ID will be updated. Default 0. 
  2796. * @type int $post_author The ID of the user who added the post. Default is 
  2797. * the current user ID. 
  2798. * @type string $post_date The date of the post. Default is the current time. 
  2799. * @type string $post_date_gmt The date of the post in the GMT timezone. Default is 
  2800. * the value of `$post_date`. 
  2801. * @type mixed $post_content The post content. Default empty. 
  2802. * @type string $post_content_filtered The filtered post content. Default empty. 
  2803. * @type string $post_title The post title. Default empty. 
  2804. * @type string $post_excerpt The post excerpt. Default empty. 
  2805. * @type string $post_status The post status. Default 'draft'. 
  2806. * @type string $post_type The post type. Default 'post'. 
  2807. * @type string $comment_status Whether the post can accept comments. Accepts 'open' or 'closed'. 
  2808. * Default is the value of 'default_comment_status' option. 
  2809. * @type string $ping_status Whether the post can accept pings. Accepts 'open' or 'closed'. 
  2810. * Default is the value of 'default_ping_status' option. 
  2811. * @type string $post_password The password to access the post. Default empty. 
  2812. * @type string $post_name The post name. Default is the sanitized post title 
  2813. * when creating a new post. 
  2814. * @type string $to_ping Space or carriage return-separated list of URLs to ping. 
  2815. * Default empty. 
  2816. * @type string $pinged Space or carriage return-separated list of URLs that have 
  2817. * been pinged. Default empty. 
  2818. * @type string $post_modified The date when the post was last modified. Default is 
  2819. * the current time. 
  2820. * @type string $post_modified_gmt The date when the post was last modified in the GMT 
  2821. * timezone. Default is the current time. 
  2822. * @type int $post_parent Set this for the post it belongs to, if any. Default 0. 
  2823. * @type int $menu_order The order the post should be displayed in. Default 0. 
  2824. * @type string $post_mime_type The mime type of the post. Default empty. 
  2825. * @type string $guid Global Unique ID for referencing the post. Default empty. 
  2826. * @type array $post_category Array of category names, slugs, or IDs. 
  2827. * Defaults to value of the 'default_category' option. 
  2828. * @type array $tax_input Array of taxonomy terms keyed by their taxonomy name. Default empty. 
  2829. * @type array $meta_input Array of post meta values keyed by their post meta key. Default empty. 
  2830. * } 
  2831. * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. 
  2832. * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. 
  2833. */ 
  2834. function wp_insert_post( $postarr, $wp_error = false ) { 
  2835. global $wpdb; 
  2836.  
  2837. $user_id = get_current_user_id(); 
  2838.  
  2839. $defaults = array( 
  2840. 'post_author' => $user_id,  
  2841. 'post_content' => '',  
  2842. 'post_content_filtered' => '',  
  2843. 'post_title' => '',  
  2844. 'post_excerpt' => '',  
  2845. 'post_status' => 'draft',  
  2846. 'post_type' => 'post',  
  2847. 'comment_status' => '',  
  2848. 'ping_status' => '',  
  2849. 'post_password' => '',  
  2850. 'to_ping' => '',  
  2851. 'pinged' => '',  
  2852. 'post_parent' => 0,  
  2853. 'menu_order' => 0,  
  2854. 'guid' => '',  
  2855. 'import_id' => 0,  
  2856. 'context' => '',  
  2857. ); 
  2858.  
  2859. $postarr = wp_parse_args($postarr, $defaults); 
  2860.  
  2861. unset( $postarr[ 'filter' ] ); 
  2862.  
  2863. $postarr = sanitize_post($postarr, 'db'); 
  2864.  
  2865. // Are we updating or creating? 
  2866. $post_ID = 0; 
  2867. $update = false; 
  2868. $guid = $postarr['guid']; 
  2869.  
  2870. if ( ! empty( $postarr['ID'] ) ) { 
  2871. $update = true; 
  2872.  
  2873. // Get the post ID and GUID. 
  2874. $post_ID = $postarr['ID']; 
  2875. $post_before = get_post( $post_ID ); 
  2876. if ( is_null( $post_before ) ) { 
  2877. if ( $wp_error ) { 
  2878. return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); 
  2879. return 0; 
  2880.  
  2881. $guid = get_post_field( 'guid', $post_ID ); 
  2882. $previous_status = get_post_field('post_status', $post_ID ); 
  2883. } else { 
  2884. $previous_status = 'new'; 
  2885.  
  2886. $post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type']; 
  2887.  
  2888. $post_title = $postarr['post_title']; 
  2889. $post_content = $postarr['post_content']; 
  2890. $post_excerpt = $postarr['post_excerpt']; 
  2891. if ( isset( $postarr['post_name'] ) ) { 
  2892. $post_name = $postarr['post_name']; 
  2893. } elseif ( $update ) { 
  2894. // For an update, don't modify the post_name if it wasn't supplied as an argument. 
  2895. $post_name = $post_before->post_name; 
  2896.  
  2897. $maybe_empty = 'attachment' !== $post_type 
  2898. && ! $post_content && ! $post_title && ! $post_excerpt 
  2899. && post_type_supports( $post_type, 'editor' ) 
  2900. && post_type_supports( $post_type, 'title' ) 
  2901. && post_type_supports( $post_type, 'excerpt' ); 
  2902.  
  2903. /** 
  2904. * Filters whether the post should be considered "empty". 
  2905. * 
  2906. * The post is considered "empty" if both: 
  2907. * 1. The post type supports the title, editor, and excerpt fields 
  2908. * 2. The title, editor, and excerpt fields are all empty 
  2909. * 
  2910. * Returning a truthy value to the filter will effectively short-circuit 
  2911. * the new post being inserted, returning 0. If $wp_error is true, a WP_Error 
  2912. * will be returned instead. 
  2913. * 
  2914. * @since 3.3.0 
  2915. * 
  2916. * @param bool $maybe_empty Whether the post should be considered "empty". 
  2917. * @param array $postarr Array of post data. 
  2918. */ 
  2919. if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) { 
  2920. if ( $wp_error ) { 
  2921. return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) ); 
  2922. } else { 
  2923. return 0; 
  2924.  
  2925. $post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status']; 
  2926. if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) { 
  2927. $post_status = 'inherit'; 
  2928.  
  2929. if ( ! empty( $postarr['post_category'] ) ) { 
  2930. // Filter out empty terms. 
  2931. $post_category = array_filter( $postarr['post_category'] ); 
  2932.  
  2933. // Make sure we set a valid category. 
  2934. if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) { 
  2935. // 'post' requires at least one category. 
  2936. if ( 'post' == $post_type && 'auto-draft' != $post_status ) { 
  2937. $post_category = array( get_option('default_category') ); 
  2938. } else { 
  2939. $post_category = array(); 
  2940.  
  2941. // Don't allow contributors to set the post slug for pending review posts. 
  2942. if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) ) { 
  2943. $post_name = ''; 
  2944.  
  2945. /** 
  2946. * Create a valid post name. Drafts and pending posts are allowed to have 
  2947. * an empty post name. 
  2948. */ 
  2949. if ( empty($post_name) ) { 
  2950. if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) { 
  2951. $post_name = sanitize_title($post_title); 
  2952. } else { 
  2953. $post_name = ''; 
  2954. } else { 
  2955. // On updates, we need to check to see if it's using the old, fixed sanitization context. 
  2956. $check_name = sanitize_title( $post_name, '', 'old-save' ); 
  2957. if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) { 
  2958. $post_name = $check_name; 
  2959. } else { // new post, or slug has changed. 
  2960. $post_name = sanitize_title($post_name); 
  2961.  
  2962. /** 
  2963. * If the post date is empty (due to having been new or a draft) and status 
  2964. * is not 'draft' or 'pending', set date to now. 
  2965. */ 
  2966. if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' == $postarr['post_date'] ) { 
  2967. if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) { 
  2968. $post_date = current_time( 'mysql' ); 
  2969. } else { 
  2970. $post_date = get_date_from_gmt( $postarr['post_date_gmt'] ); 
  2971. } else { 
  2972. $post_date = $postarr['post_date']; 
  2973.  
  2974. // Validate the date. 
  2975. $mm = substr( $post_date, 5, 2 ); 
  2976. $jj = substr( $post_date, 8, 2 ); 
  2977. $aa = substr( $post_date, 0, 4 ); 
  2978. $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date ); 
  2979. if ( ! $valid_date ) { 
  2980. if ( $wp_error ) { 
  2981. return new WP_Error( 'invalid_date', __( 'Invalid date.' ) ); 
  2982. } else { 
  2983. return 0; 
  2984.  
  2985. if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) { 
  2986. if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) { 
  2987. $post_date_gmt = get_gmt_from_date( $post_date ); 
  2988. } else { 
  2989. $post_date_gmt = '0000-00-00 00:00:00'; 
  2990. } else { 
  2991. $post_date_gmt = $postarr['post_date_gmt']; 
  2992.  
  2993. if ( $update || '0000-00-00 00:00:00' == $post_date ) { 
  2994. $post_modified = current_time( 'mysql' ); 
  2995. $post_modified_gmt = current_time( 'mysql', 1 ); 
  2996. } else { 
  2997. $post_modified = $post_date; 
  2998. $post_modified_gmt = $post_date_gmt; 
  2999.  
  3000. if ( 'attachment' !== $post_type ) { 
  3001. if ( 'publish' == $post_status ) { 
  3002. $now = gmdate('Y-m-d H:i:59'); 
  3003. if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) ) { 
  3004. $post_status = 'future'; 
  3005. } elseif ( 'future' == $post_status ) { 
  3006. $now = gmdate('Y-m-d H:i:59'); 
  3007. if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) ) { 
  3008. $post_status = 'publish'; 
  3009.  
  3010. // Comment status. 
  3011. if ( empty( $postarr['comment_status'] ) ) { 
  3012. if ( $update ) { 
  3013. $comment_status = 'closed'; 
  3014. } else { 
  3015. $comment_status = get_default_comment_status( $post_type ); 
  3016. } else { 
  3017. $comment_status = $postarr['comment_status']; 
  3018.  
  3019. // These variables are needed by compact() later. 
  3020. $post_content_filtered = $postarr['post_content_filtered']; 
  3021. $post_author = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id; 
  3022. $ping_status = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status']; 
  3023. $to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : ''; 
  3024. $pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : ''; 
  3025. $import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0; 
  3026.  
  3027. /** 
  3028. * The 'wp_insert_post_parent' filter expects all variables to be present. 
  3029. * Previously, these variables would have already been extracted 
  3030. */ 
  3031. if ( isset( $postarr['menu_order'] ) ) { 
  3032. $menu_order = (int) $postarr['menu_order']; 
  3033. } else { 
  3034. $menu_order = 0; 
  3035.  
  3036. $post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : ''; 
  3037. if ( 'private' == $post_status ) { 
  3038. $post_password = ''; 
  3039.  
  3040. if ( isset( $postarr['post_parent'] ) ) { 
  3041. $post_parent = (int) $postarr['post_parent']; 
  3042. } else { 
  3043. $post_parent = 0; 
  3044.  
  3045. /** 
  3046. * Filters the post parent -- used to check for and prevent hierarchy loops. 
  3047. * 
  3048. * @since 3.1.0 
  3049. * 
  3050. * @param int $post_parent Post parent ID. 
  3051. * @param int $post_ID Post ID. 
  3052. * @param array $new_postarr Array of parsed post data. 
  3053. * @param array $postarr Array of sanitized, but otherwise unmodified post data. 
  3054. */ 
  3055. $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr ); 
  3056.  
  3057. /** 
  3058. * If the post is being untrashed and it has a desired slug stored in post meta,  
  3059. * reassign it. 
  3060. */ 
  3061. if ( 'trash' === $previous_status && 'trash' !== $post_status ) { 
  3062. $desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true ); 
  3063. if ( $desired_post_slug ) { 
  3064. delete_post_meta( $post_ID, '_wp_desired_post_slug' ); 
  3065. $post_name = $desired_post_slug; 
  3066.  
  3067. // If a trashed post has the desired slug, change it and let this post have it. 
  3068. if ( 'trash' !== $post_status && $post_name ) { 
  3069. wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID ); 
  3070.  
  3071. // When trashing an existing post, change its slug to allow non-trashed posts to use it. 
  3072. if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) { 
  3073. $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID ); 
  3074.  
  3075. $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent ); 
  3076.  
  3077. // Don't unslash. 
  3078. $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : ''; 
  3079.  
  3080. // Expected_slashed (everything!). 
  3081. $data = compact( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' ); 
  3082.  
  3083. $emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' ); 
  3084.  
  3085. foreach ( $emoji_fields as $emoji_field ) { 
  3086. if ( isset( $data[ $emoji_field ] ) ) { 
  3087. $charset = $wpdb->get_col_charset( $wpdb->posts, $emoji_field ); 
  3088. if ( 'utf8' === $charset ) { 
  3089. $data[ $emoji_field ] = wp_encode_emoji( $data[ $emoji_field ] ); 
  3090.  
  3091. if ( 'attachment' === $post_type ) { 
  3092. /** 
  3093. * Filters attachment post data before it is updated in or added to the database. 
  3094. * 
  3095. * @since 3.9.0 
  3096. * 
  3097. * @param array $data An array of sanitized attachment post data. 
  3098. * @param array $postarr An array of unsanitized attachment post data. 
  3099. */ 
  3100. $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr ); 
  3101. } else { 
  3102. /** 
  3103. * Filters slashed post data just before it is inserted into the database. 
  3104. * 
  3105. * @since 2.7.0 
  3106. * 
  3107. * @param array $data An array of slashed post data. 
  3108. * @param array $postarr An array of sanitized, but otherwise unmodified post data. 
  3109. */ 
  3110. $data = apply_filters( 'wp_insert_post_data', $data, $postarr ); 
  3111. $data = wp_unslash( $data ); 
  3112. $where = array( 'ID' => $post_ID ); 
  3113.  
  3114. if ( $update ) { 
  3115. /** 
  3116. * Fires immediately before an existing post is updated in the database. 
  3117. * 
  3118. * @since 2.5.0 
  3119. * 
  3120. * @param int $post_ID Post ID. 
  3121. * @param array $data Array of unslashed post data. 
  3122. */ 
  3123. do_action( 'pre_post_update', $post_ID, $data ); 
  3124. if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) { 
  3125. if ( $wp_error ) { 
  3126. return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error); 
  3127. } else { 
  3128. return 0; 
  3129. } else { 
  3130. // If there is a suggested ID, use it if not already present. 
  3131. if ( ! empty( $import_id ) ) { 
  3132. $import_id = (int) $import_id; 
  3133. if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) { 
  3134. $data['ID'] = $import_id; 
  3135. if ( false === $wpdb->insert( $wpdb->posts, $data ) ) { 
  3136. if ( $wp_error ) { 
  3137. return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error); 
  3138. } else { 
  3139. return 0; 
  3140. $post_ID = (int) $wpdb->insert_id; 
  3141.  
  3142. // Use the newly generated $post_ID. 
  3143. $where = array( 'ID' => $post_ID ); 
  3144.  
  3145. if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) { 
  3146. $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent ); 
  3147. $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); 
  3148. clean_post_cache( $post_ID ); 
  3149.  
  3150. if ( is_object_in_taxonomy( $post_type, 'category' ) ) { 
  3151. wp_set_post_categories( $post_ID, $post_category ); 
  3152.  
  3153. if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) { 
  3154. wp_set_post_tags( $post_ID, $postarr['tags_input'] ); 
  3155.  
  3156. // New-style support for all custom taxonomies. 
  3157. if ( ! empty( $postarr['tax_input'] ) ) { 
  3158. foreach ( $postarr['tax_input'] as $taxonomy => $tags ) { 
  3159. $taxonomy_obj = get_taxonomy($taxonomy); 
  3160. if ( ! $taxonomy_obj ) { 
  3161. /** translators: %s: taxonomy name */ 
  3162. _doing_it_wrong( __FUNCTION__, sprintf( __( 'Invalid taxonomy: %s.' ), $taxonomy ), '4.4.0' ); 
  3163. continue; 
  3164.  
  3165. // array = hierarchical, string = non-hierarchical. 
  3166. if ( is_array( $tags ) ) { 
  3167. $tags = array_filter($tags); 
  3168. if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { 
  3169. wp_set_post_terms( $post_ID, $tags, $taxonomy ); 
  3170.  
  3171. if ( ! empty( $postarr['meta_input'] ) ) { 
  3172. foreach ( $postarr['meta_input'] as $field => $value ) { 
  3173. update_post_meta( $post_ID, $field, $value ); 
  3174.  
  3175. $current_guid = get_post_field( 'guid', $post_ID ); 
  3176.  
  3177. // Set GUID. 
  3178. if ( ! $update && '' == $current_guid ) { 
  3179. $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where ); 
  3180.  
  3181. if ( 'attachment' === $postarr['post_type'] ) { 
  3182. if ( ! empty( $postarr['file'] ) ) { 
  3183. update_attached_file( $post_ID, $postarr['file'] ); 
  3184.  
  3185. if ( ! empty( $postarr['context'] ) ) { 
  3186. add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true ); 
  3187.  
  3188. // Set or remove featured image. 
  3189. if ( isset( $postarr['_thumbnail_id'] ) ) { 
  3190. $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type; 
  3191. if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) { 
  3192. if ( wp_attachment_is( 'audio', $post_ID ) ) { 
  3193. $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); 
  3194. } elseif ( wp_attachment_is( 'video', $post_ID ) ) { 
  3195. $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); 
  3196.  
  3197. if ( $thumbnail_support ) { 
  3198. $thumbnail_id = intval( $postarr['_thumbnail_id'] ); 
  3199. if ( -1 === $thumbnail_id ) { 
  3200. delete_post_thumbnail( $post_ID ); 
  3201. } else { 
  3202. set_post_thumbnail( $post_ID, $thumbnail_id ); 
  3203.  
  3204. clean_post_cache( $post_ID ); 
  3205.  
  3206. $post = get_post( $post_ID ); 
  3207.  
  3208. if ( ! empty( $postarr['page_template'] ) ) { 
  3209. $post->page_template = $postarr['page_template']; 
  3210. $page_templates = wp_get_theme()->get_page_templates( $post ); 
  3211. if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) { 
  3212. if ( $wp_error ) { 
  3213. return new WP_Error( 'invalid_page_template', __( 'Invalid page template.' ) ); 
  3214. update_post_meta( $post_ID, '_wp_page_template', 'default' ); 
  3215. } else { 
  3216. update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] ); 
  3217.  
  3218. if ( 'attachment' !== $postarr['post_type'] ) { 
  3219. wp_transition_post_status( $data['post_status'], $previous_status, $post ); 
  3220. } else { 
  3221. if ( $update ) { 
  3222. /** 
  3223. * Fires once an existing attachment has been updated. 
  3224. * 
  3225. * @since 2.0.0 
  3226. * 
  3227. * @param int $post_ID Attachment ID. 
  3228. */ 
  3229. do_action( 'edit_attachment', $post_ID ); 
  3230. $post_after = get_post( $post_ID ); 
  3231.  
  3232. /** 
  3233. * Fires once an existing attachment has been updated. 
  3234. * 
  3235. * @since 4.4.0 
  3236. * 
  3237. * @param int $post_ID Post ID. 
  3238. * @param WP_Post $post_after Post object following the update. 
  3239. * @param WP_Post $post_before Post object before the update. 
  3240. */ 
  3241. do_action( 'attachment_updated', $post_ID, $post_after, $post_before ); 
  3242. } else { 
  3243.  
  3244. /** 
  3245. * Fires once an attachment has been added. 
  3246. * 
  3247. * @since 2.0.0 
  3248. * 
  3249. * @param int $post_ID Attachment ID. 
  3250. */ 
  3251. do_action( 'add_attachment', $post_ID ); 
  3252.  
  3253. return $post_ID; 
  3254.  
  3255. if ( $update ) { 
  3256. /** 
  3257. * Fires once an existing post has been updated. 
  3258. * 
  3259. * @since 1.2.0 
  3260. * 
  3261. * @param int $post_ID Post ID. 
  3262. * @param WP_Post $post Post object. 
  3263. */ 
  3264. do_action( 'edit_post', $post_ID, $post ); 
  3265. $post_after = get_post($post_ID); 
  3266.  
  3267. /** 
  3268. * Fires once an existing post has been updated. 
  3269. * 
  3270. * @since 3.0.0 
  3271. * 
  3272. * @param int $post_ID Post ID. 
  3273. * @param WP_Post $post_after Post object following the update. 
  3274. * @param WP_Post $post_before Post object before the update. 
  3275. */ 
  3276. do_action( 'post_updated', $post_ID, $post_after, $post_before); 
  3277.  
  3278. /** 
  3279. * Fires once a post has been saved. 
  3280. * 
  3281. * The dynamic portion of the hook name, `$post->post_type`, refers to 
  3282. * the post type slug. 
  3283. * 
  3284. * @since 3.7.0 
  3285. * 
  3286. * @param int $post_ID Post ID. 
  3287. * @param WP_Post $post Post object. 
  3288. * @param bool $update Whether this is an existing post being updated or not. 
  3289. */ 
  3290. do_action( "save_post_{$post->post_type}", $post_ID, $post, $update ); 
  3291.  
  3292. /** 
  3293. * Fires once a post has been saved. 
  3294. * 
  3295. * @since 1.5.0 
  3296. * 
  3297. * @param int $post_ID Post ID. 
  3298. * @param WP_Post $post Post object. 
  3299. * @param bool $update Whether this is an existing post being updated or not. 
  3300. */ 
  3301. do_action( 'save_post', $post_ID, $post, $update ); 
  3302.  
  3303. /** 
  3304. * Fires once a post has been saved. 
  3305. * 
  3306. * @since 2.0.0 
  3307. * 
  3308. * @param int $post_ID Post ID. 
  3309. * @param WP_Post $post Post object. 
  3310. * @param bool $update Whether this is an existing post being updated or not. 
  3311. */ 
  3312. do_action( 'wp_insert_post', $post_ID, $post, $update ); 
  3313.  
  3314. return $post_ID; 
  3315.  
  3316. /** 
  3317. * Update a post with new post data. 
  3318. * 
  3319. * The date does not have to be set for drafts. You can set the date and it will 
  3320. * not be overridden. 
  3321. * 
  3322. * @since 1.0.0 
  3323. * 
  3324. * @param array|object $postarr Optional. Post data. Arrays are expected to be escaped,  
  3325. * objects are not. Default array. 
  3326. * @param bool $wp_error Optional. Allow return of WP_Error on failure. Default false. 
  3327. * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success. 
  3328. */ 
  3329. function wp_update_post( $postarr = array(), $wp_error = false ) { 
  3330. if ( is_object($postarr) ) { 
  3331. // Non-escaped post was passed. 
  3332. $postarr = get_object_vars($postarr); 
  3333. $postarr = wp_slash($postarr); 
  3334.  
  3335. // First, get all of the original fields. 
  3336. $post = get_post($postarr['ID'], ARRAY_A); 
  3337.  
  3338. if ( is_null( $post ) ) { 
  3339. if ( $wp_error ) 
  3340. return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); 
  3341. return 0; 
  3342.  
  3343. // Escape data pulled from DB. 
  3344. $post = wp_slash($post); 
  3345.  
  3346. // Passed post category list overwrites existing category list if not empty. 
  3347. if ( isset($postarr['post_category']) && is_array($postarr['post_category']) 
  3348. && 0 != count($postarr['post_category']) ) 
  3349. $post_cats = $postarr['post_category']; 
  3350. else 
  3351. $post_cats = $post['post_category']; 
  3352.  
  3353. // Drafts shouldn't be assigned a date unless explicitly done so by the user. 
  3354. if ( isset( $post['post_status'] ) && in_array($post['post_status'], array('draft', 'pending', 'auto-draft')) && empty($postarr['edit_date']) && 
  3355. ('0000-00-00 00:00:00' == $post['post_date_gmt']) ) 
  3356. $clear_date = true; 
  3357. else 
  3358. $clear_date = false; 
  3359.  
  3360. // Merge old and new fields with new fields overwriting old ones. 
  3361. $postarr = array_merge($post, $postarr); 
  3362. $postarr['post_category'] = $post_cats; 
  3363. if ( $clear_date ) { 
  3364. $postarr['post_date'] = current_time('mysql'); 
  3365. $postarr['post_date_gmt'] = ''; 
  3366.  
  3367. if ($postarr['post_type'] == 'attachment') 
  3368. return wp_insert_attachment($postarr); 
  3369.  
  3370. return wp_insert_post( $postarr, $wp_error ); 
  3371.  
  3372. /** 
  3373. * Publish a post by transitioning the post status. 
  3374. * 
  3375. * @since 2.1.0 
  3376. * 
  3377. * @global wpdb $wpdb WordPress database abstraction object. 
  3378. * 
  3379. * @param int|WP_Post $post Post ID or post object. 
  3380. */ 
  3381. function wp_publish_post( $post ) { 
  3382. global $wpdb; 
  3383.  
  3384. if ( ! $post = get_post( $post ) ) 
  3385. return; 
  3386.  
  3387. if ( 'publish' == $post->post_status ) 
  3388. return; 
  3389.  
  3390. $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) ); 
  3391.  
  3392. clean_post_cache( $post->ID ); 
  3393.  
  3394. $old_status = $post->post_status; 
  3395. $post->post_status = 'publish'; 
  3396. wp_transition_post_status( 'publish', $old_status, $post ); 
  3397.  
  3398. /** This action is documented in wp-includes/post.php */ 
  3399. do_action( 'edit_post', $post->ID, $post ); 
  3400.  
  3401. /** This action is documented in wp-includes/post.php */ 
  3402. do_action( "save_post_{$post->post_type}", $post->ID, $post, true ); 
  3403.  
  3404. /** This action is documented in wp-includes/post.php */ 
  3405. do_action( 'save_post', $post->ID, $post, true ); 
  3406.  
  3407. /** This action is documented in wp-includes/post.php */ 
  3408. do_action( 'wp_insert_post', $post->ID, $post, true ); 
  3409.  
  3410. /** 
  3411. * Publish future post and make sure post ID has future post status. 
  3412. * 
  3413. * Invoked by cron 'publish_future_post' event. This safeguard prevents cron 
  3414. * from publishing drafts, etc. 
  3415. * 
  3416. * @since 2.5.0 
  3417. * 
  3418. * @param int|WP_Post $post_id Post ID or post object. 
  3419. */ 
  3420. function check_and_publish_future_post( $post_id ) { 
  3421. $post = get_post($post_id); 
  3422.  
  3423. if ( empty($post) ) 
  3424. return; 
  3425.  
  3426. if ( 'future' != $post->post_status ) 
  3427. return; 
  3428.  
  3429. $time = strtotime( $post->post_date_gmt . ' GMT' ); 
  3430.  
  3431. // Uh oh, someone jumped the gun! 
  3432. if ( $time > time() ) { 
  3433. wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); // clear anything else in the system 
  3434. wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) ); 
  3435. return; 
  3436.  
  3437. // wp_publish_post() returns no meaningful value. 
  3438. wp_publish_post( $post_id ); 
  3439.  
  3440. /** 
  3441. * Computes a unique slug for the post, when given the desired slug and some post details. 
  3442. * 
  3443. * @since 2.8.0 
  3444. * 
  3445. * @global wpdb $wpdb WordPress database abstraction object. 
  3446. * @global WP_Rewrite $wp_rewrite 
  3447. * 
  3448. * @param string $slug The desired slug (post_name). 
  3449. * @param int $post_ID Post ID. 
  3450. * @param string $post_status No uniqueness checks are made if the post is still draft or pending. 
  3451. * @param string $post_type Post type. 
  3452. * @param int $post_parent Post parent ID. 
  3453. * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) 
  3454. */ 
  3455. function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) { 
  3456. if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) ) 
  3457. return $slug; 
  3458.  
  3459. global $wpdb, $wp_rewrite; 
  3460.  
  3461. $original_slug = $slug; 
  3462.  
  3463. $feeds = $wp_rewrite->feeds; 
  3464. if ( ! is_array( $feeds ) ) 
  3465. $feeds = array(); 
  3466.  
  3467. if ( 'attachment' == $post_type ) { 
  3468. // Attachment slugs must be unique across all types. 
  3469. $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; 
  3470. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) ); 
  3471.  
  3472. /** 
  3473. * Filters whether the post slug would make a bad attachment slug. 
  3474. * 
  3475. * @since 3.1.0 
  3476. * 
  3477. * @param bool $bad_slug Whether the slug would be bad as an attachment slug. 
  3478. * @param string $slug The post slug. 
  3479. */ 
  3480. if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) { 
  3481. $suffix = 2; 
  3482. do { 
  3483. $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 
  3484. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) ); 
  3485. $suffix++; 
  3486. } while ( $post_name_check ); 
  3487. $slug = $alt_post_name; 
  3488. } elseif ( is_post_type_hierarchical( $post_type ) ) { 
  3489. if ( 'nav_menu_item' == $post_type ) 
  3490. return $slug; 
  3491.  
  3492. /** 
  3493. * Page slugs must be unique within their own trees. Pages are in a separate 
  3494. * namespace than posts so page slugs are allowed to overlap post slugs. 
  3495. */ 
  3496. $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1"; 
  3497. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) ); 
  3498.  
  3499. /** 
  3500. * Filters whether the post slug would make a bad hierarchical post slug. 
  3501. * 
  3502. * @since 3.1.0 
  3503. * 
  3504. * @param bool $bad_slug Whether the post slug would be bad in a hierarchical post context. 
  3505. * @param string $slug The post slug. 
  3506. * @param string $post_type Post type. 
  3507. * @param int $post_parent Post parent ID. 
  3508. */ 
  3509. if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) { 
  3510. $suffix = 2; 
  3511. do { 
  3512. $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 
  3513. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) ); 
  3514. $suffix++; 
  3515. } while ( $post_name_check ); 
  3516. $slug = $alt_post_name; 
  3517. } else { 
  3518. // Post slugs must be unique across all posts. 
  3519. $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; 
  3520. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); 
  3521.  
  3522. // Prevent new post slugs that could result in URLs that conflict with date archives. 
  3523. $post = get_post( $post_ID ); 
  3524. $conflicts_with_date_archive = false; 
  3525. if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) && $slug_num = intval( $slug ) ) { 
  3526. $permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) ); 
  3527. $postname_index = array_search( '%postname%', $permastructs ); 
  3528.  
  3529. /** 
  3530. * Potential date clashes are as follows: 
  3531. * 
  3532. * - Any integer in the first permastruct position could be a year. 
  3533. * - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'. 
  3534. * - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'. 
  3535. */ 
  3536. if ( 0 === $postname_index || 
  3537. ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && 13 > $slug_num ) || 
  3538. ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && 32 > $slug_num ) 
  3539. ) { 
  3540. $conflicts_with_date_archive = true; 
  3541.  
  3542. /** 
  3543. * Filters whether the post slug would be bad as a flat slug. 
  3544. * 
  3545. * @since 3.1.0 
  3546. * 
  3547. * @param bool $bad_slug Whether the post slug would be bad as a flat slug. 
  3548. * @param string $slug The post slug. 
  3549. * @param string $post_type Post type. 
  3550. */ 
  3551. if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || $conflicts_with_date_archive || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) { 
  3552. $suffix = 2; 
  3553. do { 
  3554. $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; 
  3555. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) ); 
  3556. $suffix++; 
  3557. } while ( $post_name_check ); 
  3558. $slug = $alt_post_name; 
  3559.  
  3560. /** 
  3561. * Filters the unique post slug. 
  3562. * 
  3563. * @since 3.3.0 
  3564. * 
  3565. * @param string $slug The post slug. 
  3566. * @param int $post_ID Post ID. 
  3567. * @param string $post_status The post status. 
  3568. * @param string $post_type Post type. 
  3569. * @param int $post_parent Post parent ID 
  3570. * @param string $original_slug The original post slug. 
  3571. */ 
  3572. return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ); 
  3573.  
  3574. /** 
  3575. * Truncate a post slug. 
  3576. * 
  3577. * @since 3.6.0 
  3578. * @access private 
  3579. * 
  3580. * @see utf8_uri_encode() 
  3581. * 
  3582. * @param string $slug The slug to truncate. 
  3583. * @param int $length Optional. Max length of the slug. Default 200 (characters). 
  3584. * @return string The truncated slug. 
  3585. */ 
  3586. function _truncate_post_slug( $slug, $length = 200 ) { 
  3587. if ( strlen( $slug ) > $length ) { 
  3588. $decoded_slug = urldecode( $slug ); 
  3589. if ( $decoded_slug === $slug ) 
  3590. $slug = substr( $slug, 0, $length ); 
  3591. else 
  3592. $slug = utf8_uri_encode( $decoded_slug, $length ); 
  3593.  
  3594. return rtrim( $slug, '-' ); 
  3595.  
  3596. /** 
  3597. * Add tags to a post. 
  3598. * 
  3599. * @see wp_set_post_tags() 
  3600. * 
  3601. * @since 2.3.0 
  3602. * 
  3603. * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. 
  3604. * @param string|array $tags Optional. An array of tags to set for the post, or a string of tags 
  3605. * separated by commas. Default empty. 
  3606. * @return array|false|WP_Error Array of affected term IDs. WP_Error or false on failure. 
  3607. */ 
  3608. function wp_add_post_tags( $post_id = 0, $tags = '' ) { 
  3609. return wp_set_post_tags($post_id, $tags, true); 
  3610.  
  3611. /** 
  3612. * Set the tags for a post. 
  3613. * 
  3614. * @since 2.3.0 
  3615. * 
  3616. * @see wp_set_object_terms() 
  3617. * 
  3618. * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. 
  3619. * @param string|array $tags Optional. An array of tags to set for the post, or a string of tags 
  3620. * separated by commas. Default empty. 
  3621. * @param bool $append Optional. If true, don't delete existing tags, just add on. If false,  
  3622. * replace the tags with the new tags. Default false. 
  3623. * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. 
  3624. */ 
  3625. function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { 
  3626. return wp_set_post_terms( $post_id, $tags, 'post_tag', $append); 
  3627.  
  3628. /** 
  3629. * Set the terms for a post. 
  3630. * 
  3631. * @since 2.8.0 
  3632. * 
  3633. * @see wp_set_object_terms() 
  3634. * 
  3635. * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. 
  3636. * @param string|array $tags Optional. An array of terms to set for the post, or a string of terms 
  3637. * separated by commas. Default empty. 
  3638. * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. 
  3639. * @param bool $append Optional. If true, don't delete existing terms, just add on. If false,  
  3640. * replace the terms with the new terms. Default false. 
  3641. * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. 
  3642. */ 
  3643. function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) { 
  3644. $post_id = (int) $post_id; 
  3645.  
  3646. if ( !$post_id ) 
  3647. return false; 
  3648.  
  3649. if ( empty($tags) ) 
  3650. $tags = array(); 
  3651.  
  3652. if ( ! is_array( $tags ) ) { 
  3653. $comma = _x( ', ', 'tag delimiter' ); 
  3654. if ( ', ' !== $comma ) 
  3655. $tags = str_replace( $comma, ', ', $tags ); 
  3656. $tags = explode( ', ', trim( $tags, " \n\t\r\0\x0B, " ) ); 
  3657.  
  3658. /** 
  3659. * Hierarchical taxonomies must always pass IDs rather than names so that 
  3660. * children with the same names but different parents aren't confused. 
  3661. */ 
  3662. if ( is_taxonomy_hierarchical( $taxonomy ) ) { 
  3663. $tags = array_unique( array_map( 'intval', $tags ) ); 
  3664.  
  3665. return wp_set_object_terms( $post_id, $tags, $taxonomy, $append ); 
  3666.  
  3667. /** 
  3668. * Set categories for a post. 
  3669. * 
  3670. * If the post categories parameter is not set, then the default category is 
  3671. * going used. 
  3672. * 
  3673. * @since 2.1.0 
  3674. * 
  3675. * @param int $post_ID Optional. The Post ID. Does not default to the ID 
  3676. * of the global $post. Default 0. 
  3677. * @param array|int $post_categories Optional. List of categories or ID of category. 
  3678. * Default empty array. 
  3679. * @param bool $append If true, don't delete existing categories, just add on. 
  3680. * If false, replace the categories with the new categories. 
  3681. * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. 
  3682. */ 
  3683. function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) { 
  3684. $post_ID = (int) $post_ID; 
  3685. $post_type = get_post_type( $post_ID ); 
  3686. $post_status = get_post_status( $post_ID ); 
  3687. // If $post_categories isn't already an array, make it one: 
  3688. $post_categories = (array) $post_categories; 
  3689. if ( empty( $post_categories ) ) { 
  3690. if ( 'post' == $post_type && 'auto-draft' != $post_status ) { 
  3691. $post_categories = array( get_option('default_category') ); 
  3692. $append = false; 
  3693. } else { 
  3694. $post_categories = array(); 
  3695. } elseif ( 1 == count( $post_categories ) && '' == reset( $post_categories ) ) { 
  3696. return true; 
  3697.  
  3698. return wp_set_post_terms( $post_ID, $post_categories, 'category', $append ); 
  3699.  
  3700. /** 
  3701. * Fires actions related to the transitioning of a post's status. 
  3702. * 
  3703. * When a post is saved, the post status is "transitioned" from one status to another,  
  3704. * though this does not always mean the status has actually changed before and after 
  3705. * the save. This function fires a number of action hooks related to that transition: 
  3706. * the generic {@see 'transition_post_status'} action, as well as the dynamic hooks 
  3707. * {@see '$old_status_to_$new_status'} and {@see '$new_status_$post->post_type'}. Note 
  3708. * that the function does not transition the post object in the database. 
  3709. * 
  3710. * For instance: When publishing a post for the first time, the post status may transition 
  3711. * from 'draft' * or some other status * to 'publish'. However, if a post is already 
  3712. * published and is simply being updated, the "old" and "new" statuses may both be 'publish' 
  3713. * before and after the transition. 
  3714. * 
  3715. * @since 2.3.0 
  3716. * 
  3717. * @param string $new_status Transition to this post status. 
  3718. * @param string $old_status Previous post status. 
  3719. * @param WP_Post $post Post data. 
  3720. */ 
  3721. function wp_transition_post_status( $new_status, $old_status, $post ) { 
  3722. /** 
  3723. * Fires when a post is transitioned from one status to another. 
  3724. * 
  3725. * @since 2.3.0 
  3726. * 
  3727. * @param string $new_status New post status. 
  3728. * @param string $old_status Old post status. 
  3729. * @param WP_Post $post Post object. 
  3730. */ 
  3731. do_action( 'transition_post_status', $new_status, $old_status, $post ); 
  3732.  
  3733. /** 
  3734. * Fires when a post is transitioned from one status to another. 
  3735. * 
  3736. * The dynamic portions of the hook name, `$new_status` and `$old status`,  
  3737. * refer to the old and new post statuses, respectively. 
  3738. * 
  3739. * @since 2.3.0 
  3740. * 
  3741. * @param WP_Post $post Post object. 
  3742. */ 
  3743. do_action( "{$old_status}_to_{$new_status}", $post ); 
  3744.  
  3745. /** 
  3746. * Fires when a post is transitioned from one status to another. 
  3747. * 
  3748. * The dynamic portions of the hook name, `$new_status` and `$post->post_type`,  
  3749. * refer to the new post status and post type, respectively. 
  3750. * 
  3751. * Please note: When this action is hooked using a particular post status (like 
  3752. * 'publish', as `publish_{$post->post_type}`), it will fire both when a post is 
  3753. * first transitioned to that status from something else, as well as upon 
  3754. * subsequent post updates (old and new status are both the same). 
  3755. * 
  3756. * Therefore, if you are looking to only fire a callback when a post is first 
  3757. * transitioned to a status, use the {@see 'transition_post_status'} hook instead. 
  3758. * 
  3759. * @since 2.3.0 
  3760. * 
  3761. * @param int $post_id Post ID. 
  3762. * @param WP_Post $post Post object. 
  3763. */ 
  3764. do_action( "{$new_status}_{$post->post_type}", $post->ID, $post ); 
  3765.  
  3766. // 
  3767.  // Comment, trackback, and pingback functions. 
  3768. // 
  3769.   
  3770. /** 
  3771. * Add a URL to those already pinged. 
  3772. * 
  3773. * @since 1.5.0 
  3774. * @since 4.7.0 $post_id can be a WP_Post object. 
  3775. * @since 4.7.0 $uri can be an array of URIs. 
  3776. * 
  3777. * @global wpdb $wpdb WordPress database abstraction object. 
  3778. * 
  3779. * @param int|WP_Post $post_id Post object or ID. 
  3780. * @param string|array $uri Ping URI or array of URIs. 
  3781. * @return int|false How many rows were updated. 
  3782. */ 
  3783. function add_ping( $post_id, $uri ) { 
  3784. global $wpdb; 
  3785.  
  3786. $post = get_post( $post_id ); 
  3787. if ( ! $post ) { 
  3788. return false; 
  3789.  
  3790. $pung = trim( $post->pinged ); 
  3791. $pung = preg_split( '/\s/', $pung ); 
  3792.  
  3793. if ( is_array( $uri ) ) { 
  3794. $pung = array_merge( $pung, $uri ); 
  3795. else { 
  3796. $pung[] = $uri; 
  3797. $new = implode("\n", $pung); 
  3798.  
  3799. /** 
  3800. * Filters the new ping URL to add for the given post. 
  3801. * 
  3802. * @since 2.0.0 
  3803. * 
  3804. * @param string $new New ping URL to add. 
  3805. */ 
  3806. $new = apply_filters( 'add_ping', $new ); 
  3807.  
  3808. $return = $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post->ID ) ); 
  3809. clean_post_cache( $post->ID ); 
  3810. return $return; 
  3811.  
  3812. /** 
  3813. * Retrieve enclosures already enclosed for a post. 
  3814. * 
  3815. * @since 1.5.0 
  3816. * 
  3817. * @param int $post_id Post ID. 
  3818. * @return array List of enclosures. 
  3819. */ 
  3820. function get_enclosed( $post_id ) { 
  3821. $custom_fields = get_post_custom( $post_id ); 
  3822. $pung = array(); 
  3823. if ( !is_array( $custom_fields ) ) 
  3824. return $pung; 
  3825.  
  3826. foreach ( $custom_fields as $key => $val ) { 
  3827. if ( 'enclosure' != $key || !is_array( $val ) ) 
  3828. continue; 
  3829. foreach ( $val as $enc ) { 
  3830. $enclosure = explode( "\n", $enc ); 
  3831. $pung[] = trim( $enclosure[ 0 ] ); 
  3832.  
  3833. /** 
  3834. * Filters the list of enclosures already enclosed for the given post. 
  3835. * 
  3836. * @since 2.0.0 
  3837. * 
  3838. * @param array $pung Array of enclosures for the given post. 
  3839. * @param int $post_id Post ID. 
  3840. */ 
  3841. return apply_filters( 'get_enclosed', $pung, $post_id ); 
  3842.  
  3843. /** 
  3844. * Retrieve URLs already pinged for a post. 
  3845. * 
  3846. * @since 1.5.0 
  3847. * 
  3848. * @since 4.7.0 $post_id can be a WP_Post object. 
  3849. * 
  3850. * @param int|WP_Post $post_id Post ID or object. 
  3851. * @return array 
  3852. */ 
  3853. function get_pung( $post_id ) { 
  3854. $post = get_post( $post_id ); 
  3855. if ( ! $post ) { 
  3856. return false; 
  3857.  
  3858. $pung = trim( $post->pinged ); 
  3859. $pung = preg_split( '/\s/', $pung ); 
  3860.  
  3861. /** 
  3862. * Filters the list of already-pinged URLs for the given post. 
  3863. * 
  3864. * @since 2.0.0 
  3865. * 
  3866. * @param array $pung Array of URLs already pinged for the given post. 
  3867. */ 
  3868. return apply_filters( 'get_pung', $pung ); 
  3869.  
  3870. /** 
  3871. * Retrieve URLs that need to be pinged. 
  3872. * 
  3873. * @since 1.5.0 
  3874. * @since 4.7.0 $post_id can be a WP_Post object. 
  3875. * 
  3876. * @param int|WP_Post $post_id Post Object or ID 
  3877. * @return array 
  3878. */ 
  3879. function get_to_ping( $post_id ) { 
  3880. $post = get_post( $post_id ); 
  3881.  
  3882. if ( ! $post ) { 
  3883. return false; 
  3884.  
  3885. $to_ping = sanitize_trackback_urls( $post->to_ping ); 
  3886. $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY); 
  3887.  
  3888. /** 
  3889. * Filters the list of URLs yet to ping for the given post. 
  3890. * 
  3891. * @since 2.0.0 
  3892. * 
  3893. * @param array $to_ping List of URLs yet to ping. 
  3894. */ 
  3895. return apply_filters( 'get_to_ping', $to_ping ); 
  3896.  
  3897. /** 
  3898. * Do trackbacks for a list of URLs. 
  3899. * 
  3900. * @since 1.0.0 
  3901. * 
  3902. * @param string $tb_list Comma separated list of URLs. 
  3903. * @param int $post_id Post ID. 
  3904. */ 
  3905. function trackback_url_list( $tb_list, $post_id ) { 
  3906. if ( ! empty( $tb_list ) ) { 
  3907. // Get post data. 
  3908. $postdata = get_post( $post_id, ARRAY_A ); 
  3909.  
  3910. // Form an excerpt. 
  3911. $excerpt = strip_tags( $postdata['post_excerpt'] ? $postdata['post_excerpt'] : $postdata['post_content'] ); 
  3912.  
  3913. if ( strlen( $excerpt ) > 255 ) { 
  3914. $excerpt = substr( $excerpt, 0, 252 ) . '…'; 
  3915.  
  3916. $trackback_urls = explode( ', ', $tb_list ); 
  3917. foreach ( (array) $trackback_urls as $tb_url ) { 
  3918. $tb_url = trim( $tb_url ); 
  3919. trackback( $tb_url, wp_unslash( $postdata['post_title'] ), $excerpt, $post_id ); 
  3920.  
  3921. // 
  3922.  // Page functions 
  3923. // 
  3924.   
  3925. /** 
  3926. * Get a list of page IDs. 
  3927. * 
  3928. * @since 2.0.0 
  3929. * 
  3930. * @global wpdb $wpdb WordPress database abstraction object. 
  3931. * 
  3932. * @return array List of page IDs. 
  3933. */ 
  3934. function get_all_page_ids() { 
  3935. global $wpdb; 
  3936.  
  3937. $page_ids = wp_cache_get('all_page_ids', 'posts'); 
  3938. if ( ! is_array( $page_ids ) ) { 
  3939. $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'"); 
  3940. wp_cache_add('all_page_ids', $page_ids, 'posts'); 
  3941.  
  3942. return $page_ids; 
  3943.  
  3944. /** 
  3945. * Retrieves page data given a page ID or page object. 
  3946. * 
  3947. * Use get_post() instead of get_page(). 
  3948. * 
  3949. * @since 1.5.1 
  3950. * @deprecated 3.5.0 Use get_post() 
  3951. * 
  3952. * @param mixed $page Page object or page ID. Passed by reference. 
  3953. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to 
  3954. * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. 
  3955. * @param string $filter Optional. How the return value should be filtered. Accepts 'raw',  
  3956. * 'edit', 'db', 'display'. Default 'raw'. 
  3957. * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. 
  3958. */ 
  3959. function get_page( $page, $output = OBJECT, $filter = 'raw') { 
  3960. return get_post( $page, $output, $filter ); 
  3961.  
  3962. /** 
  3963. * Retrieves a page given its path. 
  3964. * 
  3965. * @since 2.1.0 
  3966. * 
  3967. * @global wpdb $wpdb WordPress database abstraction object. 
  3968. * 
  3969. * @param string $page_path Page path. 
  3970. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to 
  3971. * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. 
  3972. * @param string|array $post_type Optional. Post type or array of post types. Default 'page'. 
  3973. * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. 
  3974. */ 
  3975. function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { 
  3976. global $wpdb; 
  3977.  
  3978. $last_changed = wp_cache_get_last_changed( 'posts' ); 
  3979.  
  3980. $hash = md5( $page_path . serialize( $post_type ) ); 
  3981. $cache_key = "get_page_by_path:$hash:$last_changed"; 
  3982. $cached = wp_cache_get( $cache_key, 'posts' ); 
  3983. if ( false !== $cached ) { 
  3984. // Special case: '0' is a bad `$page_path`. 
  3985. if ( '0' === $cached || 0 === $cached ) { 
  3986. return; 
  3987. } else { 
  3988. return get_post( $cached, $output ); 
  3989.  
  3990. $page_path = rawurlencode(urldecode($page_path)); 
  3991. $page_path = str_replace('%2F', '/', $page_path); 
  3992. $page_path = str_replace('%20', ' ', $page_path); 
  3993. $parts = explode( '/', trim( $page_path, '/' ) ); 
  3994. $parts = esc_sql( $parts ); 
  3995. $parts = array_map( 'sanitize_title_for_query', $parts ); 
  3996.  
  3997. $in_string = "'" . implode( "', '", $parts ) . "'"; 
  3998.  
  3999. if ( is_array( $post_type ) ) { 
  4000. $post_types = $post_type; 
  4001. } else { 
  4002. $post_types = array( $post_type, 'attachment' ); 
  4003.  
  4004. $post_types = esc_sql( $post_types ); 
  4005. $post_type_in_string = "'" . implode( "', '", $post_types ) . "'"; 
  4006. $sql = " 
  4007. SELECT ID, post_name, post_parent, post_type 
  4008. FROM $wpdb->posts 
  4009. WHERE post_name IN ($in_string) 
  4010. AND post_type IN ($post_type_in_string) 
  4011. "; 
  4012.  
  4013. $pages = $wpdb->get_results( $sql, OBJECT_K ); 
  4014.  
  4015. $revparts = array_reverse( $parts ); 
  4016.  
  4017. $foundid = 0; 
  4018. foreach ( (array) $pages as $page ) { 
  4019. if ( $page->post_name == $revparts[0] ) { 
  4020. $count = 0; 
  4021. $p = $page; 
  4022.  
  4023. /** 
  4024. * Loop through the given path parts from right to left,  
  4025. * ensuring each matches the post ancestry. 
  4026. */ 
  4027. while ( $p->post_parent != 0 && isset( $pages[ $p->post_parent ] ) ) { 
  4028. $count++; 
  4029. $parent = $pages[ $p->post_parent ]; 
  4030. if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) 
  4031. break; 
  4032. $p = $parent; 
  4033.  
  4034. if ( $p->post_parent == 0 && $count+1 == count( $revparts ) && $p->post_name == $revparts[ $count ] ) { 
  4035. $foundid = $page->ID; 
  4036. if ( $page->post_type == $post_type ) 
  4037. break; 
  4038.  
  4039. // We cache misses as well as hits. 
  4040. wp_cache_set( $cache_key, $foundid, 'posts' ); 
  4041.  
  4042. if ( $foundid ) { 
  4043. return get_post( $foundid, $output ); 
  4044.  
  4045. /** 
  4046. * Retrieve a page given its title. 
  4047. * 
  4048. * @since 2.1.0 
  4049. * 
  4050. * @global wpdb $wpdb WordPress database abstraction object. 
  4051. * 
  4052. * @param string $page_title Page title 
  4053. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to 
  4054. * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. 
  4055. * @param string|array $post_type Optional. Post type or array of post types. Default 'page'. 
  4056. * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. 
  4057. */ 
  4058. function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) { 
  4059. global $wpdb; 
  4060.  
  4061. if ( is_array( $post_type ) ) { 
  4062. $post_type = esc_sql( $post_type ); 
  4063. $post_type_in_string = "'" . implode( "', '", $post_type ) . "'"; 
  4064. $sql = $wpdb->prepare( " 
  4065. SELECT ID 
  4066. FROM $wpdb->posts 
  4067. WHERE post_title = %s 
  4068. AND post_type IN ($post_type_in_string) 
  4069. ", $page_title ); 
  4070. } else { 
  4071. $sql = $wpdb->prepare( " 
  4072. SELECT ID 
  4073. FROM $wpdb->posts 
  4074. WHERE post_title = %s 
  4075. AND post_type = %s 
  4076. ", $page_title, $post_type ); 
  4077.  
  4078. $page = $wpdb->get_var( $sql ); 
  4079.  
  4080. if ( $page ) { 
  4081. return get_post( $page, $output ); 
  4082.  
  4083. /** 
  4084. * Identify descendants of a given page ID in a list of page objects. 
  4085. * 
  4086. * Descendants are identified from the `$pages` array passed to the function. No database queries are performed. 
  4087. * 
  4088. * @since 1.5.1 
  4089. * 
  4090. * @param int $page_id Page ID. 
  4091. * @param array $pages List of page objects from which descendants should be identified. 
  4092. * @return array List of page children. 
  4093. */ 
  4094. function get_page_children( $page_id, $pages ) { 
  4095. // Build a hash of ID -> children. 
  4096. $children = array(); 
  4097. foreach ( (array) $pages as $page ) { 
  4098. $children[ intval( $page->post_parent ) ][] = $page; 
  4099.  
  4100. $page_list = array(); 
  4101.  
  4102. // Start the search by looking at immediate children. 
  4103. if ( isset( $children[ $page_id ] ) ) { 
  4104. // Always start at the end of the stack in order to preserve original `$pages` order. 
  4105. $to_look = array_reverse( $children[ $page_id ] ); 
  4106.  
  4107. while ( $to_look ) { 
  4108. $p = array_pop( $to_look ); 
  4109. $page_list[] = $p; 
  4110. if ( isset( $children[ $p->ID ] ) ) { 
  4111. foreach ( array_reverse( $children[ $p->ID ] ) as $child ) { 
  4112. // Append to the `$to_look` stack to descend the tree. 
  4113. $to_look[] = $child; 
  4114.  
  4115. return $page_list; 
  4116.  
  4117. /** 
  4118. * Order the pages with children under parents in a flat list. 
  4119. * 
  4120. * It uses auxiliary structure to hold parent-children relationships and 
  4121. * runs in O(N) complexity 
  4122. * 
  4123. * @since 2.0.0 
  4124. * 
  4125. * @param array $pages Posts array, passed by reference. 
  4126. * @param int $page_id Optional. Parent page ID. Default 0. 
  4127. * @return array A list arranged by hierarchy. Children immediately follow their parents. 
  4128. */ 
  4129. function get_page_hierarchy( &$pages, $page_id = 0 ) { 
  4130. if ( empty( $pages ) ) { 
  4131. return array(); 
  4132.  
  4133. $children = array(); 
  4134. foreach ( (array) $pages as $p ) { 
  4135. $parent_id = intval( $p->post_parent ); 
  4136. $children[ $parent_id ][] = $p; 
  4137.  
  4138. $result = array(); 
  4139. _page_traverse_name( $page_id, $children, $result ); 
  4140.  
  4141. return $result; 
  4142.  
  4143. /** 
  4144. * Traverse and return all the nested children post names of a root page. 
  4145. * 
  4146. * $children contains parent-children relations 
  4147. * 
  4148. * @since 2.9.0 
  4149. * 
  4150. * @see _page_traverse_name() 
  4151. * 
  4152. * @param int $page_id Page ID. 
  4153. * @param array $children Parent-children relations, passed by reference. 
  4154. * @param array $result Result, passed by reference. 
  4155. */ 
  4156. function _page_traverse_name( $page_id, &$children, &$result ) { 
  4157. if ( isset( $children[ $page_id ] ) ) { 
  4158. foreach ( (array)$children[ $page_id ] as $child ) { 
  4159. $result[ $child->ID ] = $child->post_name; 
  4160. _page_traverse_name( $child->ID, $children, $result ); 
  4161.  
  4162. /** 
  4163. * Build the URI path for a page. 
  4164. * 
  4165. * Sub pages will be in the "directory" under the parent page post name. 
  4166. * 
  4167. * @since 1.5.0 
  4168. * @since 4.6.0 Converted the `$page` parameter to optional. 
  4169. * 
  4170. * @param WP_Post|object|int $page Optional. Page ID or WP_Post object. Default is global $post. 
  4171. * @return string|false Page URI, false on error. 
  4172. */ 
  4173. function get_page_uri( $page = 0 ) { 
  4174. if ( ! $page instanceof WP_Post ) { 
  4175. $page = get_post( $page ); 
  4176.  
  4177. if ( ! $page ) 
  4178. return false; 
  4179.  
  4180. $uri = $page->post_name; 
  4181.  
  4182. foreach ( $page->ancestors as $parent ) { 
  4183. $parent = get_post( $parent ); 
  4184. if ( $parent && $parent->post_name ) { 
  4185. $uri = $parent->post_name . '/' . $uri; 
  4186.  
  4187. /** 
  4188. * Filters the URI for a page. 
  4189. * 
  4190. * @since 4.4.0 
  4191. * 
  4192. * @param string $uri Page URI. 
  4193. * @param WP_Post $page Page object. 
  4194. */ 
  4195. return apply_filters( 'get_page_uri', $uri, $page ); 
  4196.  
  4197. /** 
  4198. * Retrieve a list of pages. 
  4199. * 
  4200. * @global wpdb $wpdb WordPress database abstraction object. 
  4201. * 
  4202. * @since 1.5.0 
  4203. * 
  4204. * @param array|string $args { 
  4205. * Optional. Array or string of arguments to retrieve pages. 
  4206. * 
  4207. * @type int $child_of Page ID to return child and grandchild pages of. Note: The value 
  4208. * of `$hierarchical` has no bearing on whether `$child_of` returns 
  4209. * hierarchical results. Default 0, or no restriction. 
  4210. * @type string $sort_order How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'. 
  4211. * @type string $sort_column What columns to sort pages by, comma-separated. Accepts 'post_author',  
  4212. * 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order',  
  4213. * 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'. 
  4214. * 'post_' can be omitted for any values that start with it. 
  4215. * Default 'post_title'. 
  4216. * @type bool $hierarchical Whether to return pages hierarchically. If false in conjunction with 
  4217. * `$child_of` also being false, both arguments will be disregarded. 
  4218. * Default true. 
  4219. * @type array $exclude Array of page IDs to exclude. Default empty array. 
  4220. * @type array $include Array of page IDs to include. Cannot be used with `$child_of`,  
  4221. * `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`. 
  4222. * Default empty array. 
  4223. * @type string $meta_key Only include pages with this meta key. Default empty. 
  4224. * @type string $meta_value Only include pages with this meta value. Requires `$meta_key`. 
  4225. * Default empty. 
  4226. * @type string $authors A comma-separated list of author IDs. Default empty. 
  4227. * @type int $parent Page ID to return direct children of. Default -1, or no restriction. 
  4228. * @type string|array $exclude_tree Comma-separated string or array of page IDs to exclude. 
  4229. * Default empty array. 
  4230. * @type int $number The number of pages to return. Default 0, or all pages. 
  4231. * @type int $offset The number of pages to skip before returning. Requires `$number`. 
  4232. * Default 0. 
  4233. * @type string $post_type The post type to query. Default 'page'. 
  4234. * @type string|array $post_status A comma-separated list or array of post statuses to include. 
  4235. * Default 'publish'. 
  4236. * } 
  4237. * @return array|false List of pages matching defaults or `$args`. 
  4238. */ 
  4239. function get_pages( $args = array() ) { 
  4240. global $wpdb; 
  4241.  
  4242. $defaults = array( 
  4243. 'child_of' => 0,  
  4244. 'sort_order' => 'ASC',  
  4245. 'sort_column' => 'post_title',  
  4246. 'hierarchical' => 1,  
  4247. 'exclude' => array(),  
  4248. 'include' => array(),  
  4249. 'meta_key' => '',  
  4250. 'meta_value' => '',  
  4251. 'authors' => '',  
  4252. 'parent' => -1,  
  4253. 'exclude_tree' => array(),  
  4254. 'number' => '',  
  4255. 'offset' => 0,  
  4256. 'post_type' => 'page',  
  4257. 'post_status' => 'publish',  
  4258. ); 
  4259.  
  4260. $r = wp_parse_args( $args, $defaults ); 
  4261.  
  4262. $number = (int) $r['number']; 
  4263. $offset = (int) $r['offset']; 
  4264. $child_of = (int) $r['child_of']; 
  4265. $hierarchical = $r['hierarchical']; 
  4266. $exclude = $r['exclude']; 
  4267. $meta_key = $r['meta_key']; 
  4268. $meta_value = $r['meta_value']; 
  4269. $parent = $r['parent']; 
  4270. $post_status = $r['post_status']; 
  4271.  
  4272. // Make sure the post type is hierarchical. 
  4273. $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) ); 
  4274. if ( ! in_array( $r['post_type'], $hierarchical_post_types ) ) { 
  4275. return false; 
  4276.  
  4277. if ( $parent > 0 && ! $child_of ) { 
  4278. $hierarchical = false; 
  4279.  
  4280. // Make sure we have a valid post status. 
  4281. if ( ! is_array( $post_status ) ) { 
  4282. $post_status = explode( ', ', $post_status ); 
  4283. if ( array_diff( $post_status, get_post_stati() ) ) { 
  4284. return false; 
  4285.  
  4286. // $args can be whatever, only use the args defined in defaults to compute the key. 
  4287. $key = md5( serialize( wp_array_slice_assoc( $r, array_keys( $defaults ) ) ) ); 
  4288. $last_changed = wp_cache_get_last_changed( 'posts' ); 
  4289.  
  4290. $cache_key = "get_pages:$key:$last_changed"; 
  4291. if ( $cache = wp_cache_get( $cache_key, 'posts' ) ) { 
  4292. // Convert to WP_Post instances. 
  4293. $pages = array_map( 'get_post', $cache ); 
  4294. /** This filter is documented in wp-includes/post.php */ 
  4295. $pages = apply_filters( 'get_pages', $pages, $r ); 
  4296. return $pages; 
  4297.  
  4298. $inclusions = ''; 
  4299. if ( ! empty( $r['include'] ) ) { 
  4300. $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include 
  4301. $parent = -1; 
  4302. $exclude = ''; 
  4303. $meta_key = ''; 
  4304. $meta_value = ''; 
  4305. $hierarchical = false; 
  4306. $incpages = wp_parse_id_list( $r['include'] ); 
  4307. if ( ! empty( $incpages ) ) { 
  4308. $inclusions = ' AND ID IN (' . implode( ', ', $incpages ) . ')'; 
  4309.  
  4310. $exclusions = ''; 
  4311. if ( ! empty( $exclude ) ) { 
  4312. $expages = wp_parse_id_list( $exclude ); 
  4313. if ( ! empty( $expages ) ) { 
  4314. $exclusions = ' AND ID NOT IN (' . implode( ', ', $expages ) . ')'; 
  4315.  
  4316. $author_query = ''; 
  4317. if ( ! empty( $r['authors'] ) ) { 
  4318. $post_authors = preg_split( '/[\s, ]+/', $r['authors'] ); 
  4319.  
  4320. if ( ! empty( $post_authors ) ) { 
  4321. foreach ( $post_authors as $post_author ) { 
  4322. //Do we have an author id or an author login? 
  4323. if ( 0 == intval($post_author) ) { 
  4324. $post_author = get_user_by('login', $post_author); 
  4325. if ( empty( $post_author ) ) { 
  4326. continue; 
  4327. if ( empty( $post_author->ID ) ) { 
  4328. continue; 
  4329. $post_author = $post_author->ID; 
  4330.  
  4331. if ( '' == $author_query ) { 
  4332. $author_query = $wpdb->prepare(' post_author = %d ', $post_author); 
  4333. } else { 
  4334. $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author); 
  4335. if ( '' != $author_query ) { 
  4336. $author_query = " AND ($author_query)"; 
  4337.  
  4338. $join = ''; 
  4339. $where = "$exclusions $inclusions "; 
  4340. if ( '' !== $meta_key || '' !== $meta_value ) { 
  4341. $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )"; 
  4342.  
  4343. // meta_key and meta_value might be slashed 
  4344. $meta_key = wp_unslash($meta_key); 
  4345. $meta_value = wp_unslash($meta_value); 
  4346. if ( '' !== $meta_key ) { 
  4347. $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key); 
  4348. if ( '' !== $meta_value ) { 
  4349. $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value); 
  4350.  
  4351.  
  4352. if ( is_array( $parent ) ) { 
  4353. $post_parent__in = implode( ', ', array_map( 'absint', (array) $parent ) ); 
  4354. if ( ! empty( $post_parent__in ) ) { 
  4355. $where .= " AND post_parent IN ($post_parent__in)"; 
  4356. } elseif ( $parent >= 0 ) { 
  4357. $where .= $wpdb->prepare(' AND post_parent = %d ', $parent); 
  4358.  
  4359. if ( 1 == count( $post_status ) ) { 
  4360. $where_post_type = $wpdb->prepare( "post_type = %s AND post_status = %s", $r['post_type'], reset( $post_status ) ); 
  4361. } else { 
  4362. $post_status = implode( "', '", $post_status ); 
  4363. $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $r['post_type'] ); 
  4364.  
  4365. $orderby_array = array(); 
  4366. $allowed_keys = array( 'author', 'post_author', 'date', 'post_date', 'title', 'post_title', 'name', 'post_name', 'modified',  
  4367. 'post_modified', 'modified_gmt', 'post_modified_gmt', 'menu_order', 'parent', 'post_parent',  
  4368. 'ID', 'rand', 'comment_count' ); 
  4369.  
  4370. foreach ( explode( ', ', $r['sort_column'] ) as $orderby ) { 
  4371. $orderby = trim( $orderby ); 
  4372. if ( ! in_array( $orderby, $allowed_keys ) ) { 
  4373. continue; 
  4374.  
  4375. switch ( $orderby ) { 
  4376. case 'menu_order': 
  4377. break; 
  4378. case 'ID': 
  4379. $orderby = "$wpdb->posts.ID"; 
  4380. break; 
  4381. case 'rand': 
  4382. $orderby = 'RAND()'; 
  4383. break; 
  4384. case 'comment_count': 
  4385. $orderby = "$wpdb->posts.comment_count"; 
  4386. break; 
  4387. default: 
  4388. if ( 0 === strpos( $orderby, 'post_' ) ) { 
  4389. $orderby = "$wpdb->posts." . $orderby; 
  4390. } else { 
  4391. $orderby = "$wpdb->posts.post_" . $orderby; 
  4392.  
  4393. $orderby_array[] = $orderby; 
  4394.  
  4395. $sort_column = ! empty( $orderby_array ) ? implode( ', ', $orderby_array ) : "$wpdb->posts.post_title"; 
  4396.  
  4397. $sort_order = strtoupper( $r['sort_order'] ); 
  4398. if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ) ) ) { 
  4399. $sort_order = 'ASC'; 
  4400.  
  4401. $query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where "; 
  4402. $query .= $author_query; 
  4403. $query .= " ORDER BY " . $sort_column . " " . $sort_order ; 
  4404.  
  4405. if ( ! empty( $number ) ) { 
  4406. $query .= ' LIMIT ' . $offset . ', ' . $number; 
  4407.  
  4408. $pages = $wpdb->get_results($query); 
  4409.  
  4410. if ( empty($pages) ) { 
  4411. /** This filter is documented in wp-includes/post.php */ 
  4412. $pages = apply_filters( 'get_pages', array(), $r ); 
  4413. return $pages; 
  4414.  
  4415. // Sanitize before caching so it'll only get done once. 
  4416. $num_pages = count($pages); 
  4417. for ($i = 0; $i < $num_pages; $i++) { 
  4418. $pages[$i] = sanitize_post($pages[$i], 'raw'); 
  4419.  
  4420. // Update cache. 
  4421. update_post_cache( $pages ); 
  4422.  
  4423. if ( $child_of || $hierarchical ) { 
  4424. $pages = get_page_children($child_of, $pages); 
  4425.  
  4426. if ( ! empty( $r['exclude_tree'] ) ) { 
  4427. $exclude = wp_parse_id_list( $r['exclude_tree'] ); 
  4428. foreach ( $exclude as $id ) { 
  4429. $children = get_page_children( $id, $pages ); 
  4430. foreach ( $children as $child ) { 
  4431. $exclude[] = $child->ID; 
  4432.  
  4433. $num_pages = count( $pages ); 
  4434. for ( $i = 0; $i < $num_pages; $i++ ) { 
  4435. if ( in_array( $pages[$i]->ID, $exclude ) ) { 
  4436. unset( $pages[$i] ); 
  4437.  
  4438. $page_structure = array(); 
  4439. foreach ( $pages as $page ) { 
  4440. $page_structure[] = $page->ID; 
  4441.  
  4442. wp_cache_set( $cache_key, $page_structure, 'posts' ); 
  4443.  
  4444. // Convert to WP_Post instances 
  4445. $pages = array_map( 'get_post', $pages ); 
  4446.  
  4447. /** 
  4448. * Filters the retrieved list of pages. 
  4449. * 
  4450. * @since 2.1.0 
  4451. * 
  4452. * @param array $pages List of pages to retrieve. 
  4453. * @param array $r Array of get_pages() arguments. 
  4454. */ 
  4455. return apply_filters( 'get_pages', $pages, $r ); 
  4456.  
  4457. // 
  4458.  // Attachment functions 
  4459. // 
  4460.   
  4461. /** 
  4462. * Check if the attachment URI is local one and is really an attachment. 
  4463. * 
  4464. * @since 2.0.0 
  4465. * 
  4466. * @param string $url URL to check 
  4467. * @return bool True on success, false on failure. 
  4468. */ 
  4469. function is_local_attachment($url) { 
  4470. if (strpos($url, home_url()) === false) 
  4471. return false; 
  4472. if (strpos($url, home_url('/?attachment_id=')) !== false) 
  4473. return true; 
  4474. if ( $id = url_to_postid($url) ) { 
  4475. $post = get_post($id); 
  4476. if ( 'attachment' == $post->post_type ) 
  4477. return true; 
  4478. return false; 
  4479.  
  4480. /** 
  4481. * Insert an attachment. 
  4482. * 
  4483. * If you set the 'ID' in the $args parameter, it will mean that you are 
  4484. * updating and attempt to update the attachment. You can also set the 
  4485. * attachment name or title by setting the key 'post_name' or 'post_title'. 
  4486. * 
  4487. * You can set the dates for the attachment manually by setting the 'post_date' 
  4488. * and 'post_date_gmt' keys' values. 
  4489. * 
  4490. * By default, the comments will use the default settings for whether the 
  4491. * comments are allowed. You can close them manually or keep them open by 
  4492. * setting the value for the 'comment_status' key. 
  4493. * 
  4494. * @since 2.0.0 
  4495. * @since 4.7.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure. 
  4496. * 
  4497. * @see wp_insert_post() 
  4498. * 
  4499. * @param string|array $args Arguments for inserting an attachment. 
  4500. * @param string $file Optional. Filename. 
  4501. * @param int $parent Optional. Parent post ID. 
  4502. * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. 
  4503. * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure. 
  4504. */ 
  4505. function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false ) { 
  4506. $defaults = array( 
  4507. 'file' => $file,  
  4508. 'post_parent' => 0 
  4509. ); 
  4510.  
  4511. $data = wp_parse_args( $args, $defaults ); 
  4512.  
  4513. if ( ! empty( $parent ) ) { 
  4514. $data['post_parent'] = $parent; 
  4515.  
  4516. $data['post_type'] = 'attachment'; 
  4517.  
  4518. return wp_insert_post( $data, $wp_error ); 
  4519.  
  4520. /** 
  4521. * Trash or delete an attachment. 
  4522. * 
  4523. * When an attachment is permanently deleted, the file will also be removed. 
  4524. * Deletion removes all post meta fields, taxonomy, comments, etc. associated 
  4525. * with the attachment (except the main post). 
  4526. * 
  4527. * The attachment is moved to the trash instead of permanently deleted unless trash 
  4528. * for media is disabled, item is already in the trash, or $force_delete is true. 
  4529. * 
  4530. * @since 2.0.0 
  4531. * 
  4532. * @global wpdb $wpdb WordPress database abstraction object. 
  4533. * 
  4534. * @param int $post_id Attachment ID. 
  4535. * @param bool $force_delete Optional. Whether to bypass trash and force deletion. 
  4536. * Default false. 
  4537. * @return mixed False on failure. Post data on success. 
  4538. */ 
  4539. function wp_delete_attachment( $post_id, $force_delete = false ) { 
  4540. global $wpdb; 
  4541.  
  4542. if ( !$post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) ) ) 
  4543. return $post; 
  4544.  
  4545. if ( 'attachment' != $post->post_type ) 
  4546. return false; 
  4547.  
  4548. if ( !$force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' != $post->post_status ) 
  4549. return wp_trash_post( $post_id ); 
  4550.  
  4551. delete_post_meta($post_id, '_wp_trash_meta_status'); 
  4552. delete_post_meta($post_id, '_wp_trash_meta_time'); 
  4553.  
  4554. $meta = wp_get_attachment_metadata( $post_id ); 
  4555. $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); 
  4556. $file = get_attached_file( $post_id ); 
  4557.  
  4558. if ( is_multisite() ) 
  4559. delete_transient( 'dirsize_cache' ); 
  4560.  
  4561. /** 
  4562. * Fires before an attachment is deleted, at the start of wp_delete_attachment(). 
  4563. * 
  4564. * @since 2.0.0 
  4565. * 
  4566. * @param int $post_id Attachment ID. 
  4567. */ 
  4568. do_action( 'delete_attachment', $post_id ); 
  4569.  
  4570. wp_delete_object_term_relationships($post_id, array('category', 'post_tag')); 
  4571. wp_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type)); 
  4572.  
  4573. // Delete all for any posts. 
  4574. delete_metadata( 'post', null, '_thumbnail_id', $post_id, true ); 
  4575.  
  4576. wp_defer_comment_counting( true ); 
  4577.  
  4578. $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id )); 
  4579. foreach ( $comment_ids as $comment_id ) { 
  4580. wp_delete_comment( $comment_id, true ); 
  4581.  
  4582. wp_defer_comment_counting( false ); 
  4583.  
  4584. $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id )); 
  4585. foreach ( $post_meta_ids as $mid ) 
  4586. delete_metadata_by_mid( 'post', $mid ); 
  4587.  
  4588. /** This action is documented in wp-includes/post.php */ 
  4589. do_action( 'delete_post', $post_id ); 
  4590. $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) ); 
  4591. if ( ! $result ) { 
  4592. return false; 
  4593. /** This action is documented in wp-includes/post.php */ 
  4594. do_action( 'deleted_post', $post_id ); 
  4595.  
  4596. $uploadpath = wp_get_upload_dir(); 
  4597.  
  4598. if ( ! empty($meta['thumb']) ) { 
  4599. // Don't delete the thumb if another attachment uses it. 
  4600. if (! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id)) ) { 
  4601. $thumbfile = str_replace(basename($file), $meta['thumb'], $file); 
  4602. /** This filter is documented in wp-includes/functions.php */ 
  4603. $thumbfile = apply_filters( 'wp_delete_file', $thumbfile ); 
  4604. @ unlink( path_join($uploadpath['basedir'], $thumbfile) ); 
  4605.  
  4606. // Remove intermediate and backup images if there are any. 
  4607. if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) { 
  4608. foreach ( $meta['sizes'] as $size => $sizeinfo ) { 
  4609. $intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file ); 
  4610. /** This filter is documented in wp-includes/functions.php */ 
  4611. $intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file ); 
  4612. @ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) ); 
  4613.  
  4614. if ( is_array($backup_sizes) ) { 
  4615. foreach ( $backup_sizes as $size ) { 
  4616. $del_file = path_join( dirname($meta['file']), $size['file'] ); 
  4617. /** This filter is documented in wp-includes/functions.php */ 
  4618. $del_file = apply_filters( 'wp_delete_file', $del_file ); 
  4619. @ unlink( path_join($uploadpath['basedir'], $del_file) ); 
  4620.  
  4621. wp_delete_file( $file ); 
  4622.  
  4623. clean_post_cache( $post ); 
  4624.  
  4625. return $post; 
  4626.  
  4627. /** 
  4628. * Retrieve attachment meta field for attachment ID. 
  4629. * 
  4630. * @since 2.1.0 
  4631. * 
  4632. * @param int $post_id Attachment ID. Default 0. 
  4633. * @param bool $unfiltered Optional. If true, filters are not run. Default false. 
  4634. * @return mixed Attachment meta field. False on failure. 
  4635. */ 
  4636. function wp_get_attachment_metadata( $post_id = 0, $unfiltered = false ) { 
  4637. $post_id = (int) $post_id; 
  4638. if ( !$post = get_post( $post_id ) ) 
  4639. return false; 
  4640.  
  4641. $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true ); 
  4642.  
  4643. if ( $unfiltered ) 
  4644. return $data; 
  4645.  
  4646. /** 
  4647. * Filters the attachment meta data. 
  4648. * 
  4649. * @since 2.1.0 
  4650. * 
  4651. * @param array|bool $data Array of meta data for the given attachment, or false 
  4652. * if the object does not exist. 
  4653. * @param int $post_id Attachment ID. 
  4654. */ 
  4655. return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID ); 
  4656.  
  4657. /** 
  4658. * Update metadata for an attachment. 
  4659. * 
  4660. * @since 2.1.0 
  4661. * 
  4662. * @param int $post_id Attachment ID. 
  4663. * @param array $data Attachment data. 
  4664. * @return int|bool False if $post is invalid. 
  4665. */ 
  4666. function wp_update_attachment_metadata( $post_id, $data ) { 
  4667. $post_id = (int) $post_id; 
  4668. if ( !$post = get_post( $post_id ) ) 
  4669. return false; 
  4670.  
  4671. /** 
  4672. * Filters the updated attachment meta data. 
  4673. * 
  4674. * @since 2.1.0 
  4675. * 
  4676. * @param array $data Array of updated attachment meta data. 
  4677. * @param int $post_id Attachment ID. 
  4678. */ 
  4679. if ( $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ) ) 
  4680. return update_post_meta( $post->ID, '_wp_attachment_metadata', $data ); 
  4681. else 
  4682. return delete_post_meta( $post->ID, '_wp_attachment_metadata' ); 
  4683.  
  4684. /** 
  4685. * Retrieve the URL for an attachment. 
  4686. * 
  4687. * @since 2.1.0 
  4688. * 
  4689. * @global string $pagenow 
  4690. * 
  4691. * @param int $post_id Optional. Attachment ID. Default 0. 
  4692. * @return string|false Attachment URL, otherwise false. 
  4693. */ 
  4694. function wp_get_attachment_url( $post_id = 0 ) { 
  4695. $post_id = (int) $post_id; 
  4696. if ( !$post = get_post( $post_id ) ) 
  4697. return false; 
  4698.  
  4699. if ( 'attachment' != $post->post_type ) 
  4700. return false; 
  4701.  
  4702. $url = ''; 
  4703. // Get attached file. 
  4704. if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true ) ) { 
  4705. // Get upload directory. 
  4706. if ( ( $uploads = wp_get_upload_dir() ) && false === $uploads['error'] ) { 
  4707. // Check that the upload base exists in the file location. 
  4708. if ( 0 === strpos( $file, $uploads['basedir'] ) ) { 
  4709. // Replace file location with url location. 
  4710. $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); 
  4711. } elseif ( false !== strpos($file, 'wp-content/uploads') ) { 
  4712. // Get the directory name relative to the basedir (back compat for pre-2.7 uploads) 
  4713. $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . basename( $file ); 
  4714. } else { 
  4715. // It's a newly-uploaded file, therefore $file is relative to the basedir. 
  4716. $url = $uploads['baseurl'] . "/$file"; 
  4717.  
  4718. /** 
  4719. * If any of the above options failed, Fallback on the GUID as used pre-2.7,  
  4720. * not recommended to rely upon this. 
  4721. */ 
  4722. if ( empty($url) ) { 
  4723. $url = get_the_guid( $post->ID ); 
  4724.  
  4725. // On SSL front end, URLs should be HTTPS. 
  4726. if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] ) { 
  4727. $url = set_url_scheme( $url ); 
  4728.  
  4729. /** 
  4730. * Filters the attachment URL. 
  4731. * 
  4732. * @since 2.1.0 
  4733. * 
  4734. * @param string $url URL for the given attachment. 
  4735. * @param int $post_id Attachment ID. 
  4736. */ 
  4737. $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID ); 
  4738.  
  4739. if ( empty( $url ) ) 
  4740. return false; 
  4741.  
  4742. return $url; 
  4743.  
  4744. /** 
  4745. * Retrieves the caption for an attachment. 
  4746. * 
  4747. * @since 4.6.0 
  4748. * 
  4749. * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. 
  4750. * @return string|false False on failure. Attachment caption on success. 
  4751. */ 
  4752. function wp_get_attachment_caption( $post_id = 0 ) { 
  4753. $post_id = (int) $post_id; 
  4754. if ( ! $post = get_post( $post_id ) ) { 
  4755. return false; 
  4756.  
  4757. if ( 'attachment' !== $post->post_type ) { 
  4758. return false; 
  4759.  
  4760. $caption = $post->post_excerpt; 
  4761.  
  4762. /** 
  4763. * Filters the attachment caption. 
  4764. * 
  4765. * @since 4.6.0 
  4766. * 
  4767. * @param string $caption Caption for the given attachment. 
  4768. * @param int $post_id Attachment ID. 
  4769. */ 
  4770. return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID ); 
  4771.  
  4772. /** 
  4773. * Retrieve thumbnail for an attachment. 
  4774. * 
  4775. * @since 2.1.0 
  4776. * 
  4777. * @param int $post_id Optional. Attachment ID. Default 0. 
  4778. * @return string|false False on failure. Thumbnail file path on success. 
  4779. */ 
  4780. function wp_get_attachment_thumb_file( $post_id = 0 ) { 
  4781. $post_id = (int) $post_id; 
  4782. if ( !$post = get_post( $post_id ) ) 
  4783. return false; 
  4784. if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) ) 
  4785. return false; 
  4786.  
  4787. $file = get_attached_file( $post->ID ); 
  4788.  
  4789. if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) ) { 
  4790. /** 
  4791. * Filters the attachment thumbnail file path. 
  4792. * 
  4793. * @since 2.1.0 
  4794. * 
  4795. * @param string $thumbfile File path to the attachment thumbnail. 
  4796. * @param int $post_id Attachment ID. 
  4797. */ 
  4798. return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); 
  4799. return false; 
  4800.  
  4801. /** 
  4802. * Retrieve URL for an attachment thumbnail. 
  4803. * 
  4804. * @since 2.1.0 
  4805. * 
  4806. * @param int $post_id Optional. Attachment ID. Default 0. 
  4807. * @return string|false False on failure. Thumbnail URL on success. 
  4808. */ 
  4809. function wp_get_attachment_thumb_url( $post_id = 0 ) { 
  4810. $post_id = (int) $post_id; 
  4811. if ( !$post = get_post( $post_id ) ) 
  4812. return false; 
  4813. if ( !$url = wp_get_attachment_url( $post->ID ) ) 
  4814. return false; 
  4815.  
  4816. $sized = image_downsize( $post_id, 'thumbnail' ); 
  4817. if ( $sized ) 
  4818. return $sized[0]; 
  4819.  
  4820. if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) ) 
  4821. return false; 
  4822.  
  4823. $url = str_replace(basename($url), basename($thumb), $url); 
  4824.  
  4825. /** 
  4826. * Filters the attachment thumbnail URL. 
  4827. * 
  4828. * @since 2.1.0 
  4829. * 
  4830. * @param string $url URL for the attachment thumbnail. 
  4831. * @param int $post_id Attachment ID. 
  4832. */ 
  4833. return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); 
  4834.  
  4835. /** 
  4836. * Verifies an attachment is of a given type. 
  4837. * 
  4838. * @since 4.2.0 
  4839. * 
  4840. * @param string $type Attachment type. Accepts 'image', 'audio', or 'video'. 
  4841. * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. 
  4842. * @return bool True if one of the accepted types, false otherwise. 
  4843. */ 
  4844. function wp_attachment_is( $type, $post = null ) { 
  4845. if ( ! $post = get_post( $post ) ) { 
  4846. return false; 
  4847.  
  4848. if ( ! $file = get_attached_file( $post->ID ) ) { 
  4849. return false; 
  4850.  
  4851. if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) { 
  4852. return true; 
  4853.  
  4854. $check = wp_check_filetype( $file ); 
  4855. if ( empty( $check['ext'] ) ) { 
  4856. return false; 
  4857.  
  4858. $ext = $check['ext']; 
  4859.  
  4860. if ( 'import' !== $post->post_mime_type ) { 
  4861. return $type === $ext; 
  4862.  
  4863. switch ( $type ) { 
  4864. case 'image': 
  4865. $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ); 
  4866. return in_array( $ext, $image_exts ); 
  4867.  
  4868. case 'audio': 
  4869. return in_array( $ext, wp_get_audio_extensions() ); 
  4870.  
  4871. case 'video': 
  4872. return in_array( $ext, wp_get_video_extensions() ); 
  4873.  
  4874. default: 
  4875. return $type === $ext; 
  4876.  
  4877. /** 
  4878. * Checks if the attachment is an image. 
  4879. * 
  4880. * @since 2.1.0 
  4881. * @since 4.2.0 Modified into wrapper for wp_attachment_is() and 
  4882. * allowed WP_Post object to be passed. 
  4883. * 
  4884. * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. 
  4885. * @return bool Whether the attachment is an image. 
  4886. */ 
  4887. function wp_attachment_is_image( $post = null ) { 
  4888. return wp_attachment_is( 'image', $post ); 
  4889.  
  4890. /** 
  4891. * Retrieve the icon for a MIME type. 
  4892. * 
  4893. * @since 2.1.0 
  4894. * 
  4895. * @param string|int $mime MIME type or attachment ID. 
  4896. * @return string|false Icon, false otherwise. 
  4897. */ 
  4898. function wp_mime_type_icon( $mime = 0 ) { 
  4899. if ( !is_numeric($mime) ) 
  4900. $icon = wp_cache_get("mime_type_icon_$mime"); 
  4901.  
  4902. $post_id = 0; 
  4903. if ( empty($icon) ) { 
  4904. $post_mimes = array(); 
  4905. if ( is_numeric($mime) ) { 
  4906. $mime = (int) $mime; 
  4907. if ( $post = get_post( $mime ) ) { 
  4908. $post_id = (int) $post->ID; 
  4909. $file = get_attached_file( $post_id ); 
  4910. $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $file); 
  4911. if ( !empty($ext) ) { 
  4912. $post_mimes[] = $ext; 
  4913. if ( $ext_type = wp_ext2type( $ext ) ) 
  4914. $post_mimes[] = $ext_type; 
  4915. $mime = $post->post_mime_type; 
  4916. } else { 
  4917. $mime = 0; 
  4918. } else { 
  4919. $post_mimes[] = $mime; 
  4920.  
  4921. $icon_files = wp_cache_get('icon_files'); 
  4922.  
  4923. if ( !is_array($icon_files) ) { 
  4924. /** 
  4925. * Filters the icon directory path. 
  4926. * 
  4927. * @since 2.0.0 
  4928. * 
  4929. * @param string $path Icon directory absolute path. 
  4930. */ 
  4931. $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); 
  4932.  
  4933. /** 
  4934. * Filters the icon directory URI. 
  4935. * 
  4936. * @since 2.0.0 
  4937. * 
  4938. * @param string $uri Icon directory URI. 
  4939. */ 
  4940. $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) ); 
  4941.  
  4942. /** 
  4943. * Filters the list of icon directory URIs. 
  4944. * 
  4945. * @since 2.5.0 
  4946. * 
  4947. * @param array $uris List of icon directory URIs. 
  4948. */ 
  4949. $dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) ); 
  4950. $icon_files = array(); 
  4951. while ( $dirs ) { 
  4952. $keys = array_keys( $dirs ); 
  4953. $dir = array_shift( $keys ); 
  4954. $uri = array_shift($dirs); 
  4955. if ( $dh = opendir($dir) ) { 
  4956. while ( false !== $file = readdir($dh) ) { 
  4957. $file = basename($file); 
  4958. if ( substr($file, 0, 1) == '.' ) 
  4959. continue; 
  4960. if ( !in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg') ) ) { 
  4961. if ( is_dir("$dir/$file") ) 
  4962. $dirs["$dir/$file"] = "$uri/$file"; 
  4963. continue; 
  4964. $icon_files["$dir/$file"] = "$uri/$file"; 
  4965. closedir($dh); 
  4966. wp_cache_add( 'icon_files', $icon_files, 'default', 600 ); 
  4967.  
  4968. $types = array(); 
  4969. // Icon basename - extension = MIME wildcard. 
  4970. foreach ( $icon_files as $file => $uri ) 
  4971. $types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file]; 
  4972.  
  4973. if ( ! empty($mime) ) { 
  4974. $post_mimes[] = substr($mime, 0, strpos($mime, '/')); 
  4975. $post_mimes[] = substr($mime, strpos($mime, '/') + 1); 
  4976. $post_mimes[] = str_replace('/', '_', $mime); 
  4977.  
  4978. $matches = wp_match_mime_types(array_keys($types), $post_mimes); 
  4979. $matches['default'] = array('default'); 
  4980.  
  4981. foreach ( $matches as $match => $wilds ) { 
  4982. foreach ( $wilds as $wild ) { 
  4983. if ( ! isset( $types[ $wild ] ) ) { 
  4984. continue; 
  4985.  
  4986. $icon = $types[ $wild ]; 
  4987. if ( ! is_numeric( $mime ) ) { 
  4988. wp_cache_add( "mime_type_icon_$mime", $icon ); 
  4989. break 2; 
  4990.  
  4991. /** 
  4992. * Filters the mime type icon. 
  4993. * 
  4994. * @since 2.1.0 
  4995. * 
  4996. * @param string $icon Path to the mime type icon. 
  4997. * @param string $mime Mime type. 
  4998. * @param int $post_id Attachment ID. Will equal 0 if the function passed 
  4999. * the mime type. 
  5000. */ 
  5001. return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); 
  5002.  
  5003. /** 
  5004. * Check for changed slugs for published post objects and save the old slug. 
  5005. * 
  5006. * The function is used when a post object of any type is updated,  
  5007. * by comparing the current and previous post objects. 
  5008. * 
  5009. * If the slug was changed and not already part of the old slugs then it will be 
  5010. * added to the post meta field ('_wp_old_slug') for storing old slugs for that 
  5011. * post. 
  5012. * 
  5013. * The most logically usage of this function is redirecting changed post objects, so 
  5014. * that those that linked to an changed post will be redirected to the new post. 
  5015. * 
  5016. * @since 2.1.0 
  5017. * 
  5018. * @param int $post_id Post ID. 
  5019. * @param WP_Post $post The Post Object 
  5020. * @param WP_Post $post_before The Previous Post Object 
  5021. */ 
  5022. function wp_check_for_changed_slugs( $post_id, $post, $post_before ) { 
  5023. // Don't bother if it hasn't changed. 
  5024. if ( $post->post_name == $post_before->post_name ) { 
  5025. return; 
  5026.  
  5027. // We're only concerned with published, non-hierarchical objects. 
  5028. if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) { 
  5029. return; 
  5030.  
  5031. $old_slugs = (array) get_post_meta( $post_id, '_wp_old_slug' ); 
  5032.  
  5033. // If we haven't added this old slug before, add it now. 
  5034. if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs ) ) { 
  5035. add_post_meta( $post_id, '_wp_old_slug', $post_before->post_name ); 
  5036.  
  5037. // If the new slug was used previously, delete it from the list. 
  5038. if ( in_array( $post->post_name, $old_slugs ) ) { 
  5039. delete_post_meta( $post_id, '_wp_old_slug', $post->post_name ); 
  5040.  
  5041. /** 
  5042. * Retrieve the private post SQL based on capability. 
  5043. * 
  5044. * This function provides a standardized way to appropriately select on the 
  5045. * post_status of a post type. The function will return a piece of SQL code 
  5046. * that can be added to a WHERE clause; this SQL is constructed to allow all 
  5047. * published posts, and all private posts to which the user has access. 
  5048. * 
  5049. * @since 2.2.0 
  5050. * @since 4.3.0 Added the ability to pass an array to `$post_type`. 
  5051. * 
  5052. * @param string|array $post_type Single post type or an array of post types. Currently only supports 'post' or 'page'. 
  5053. * @return string SQL code that can be added to a where clause. 
  5054. */ 
  5055. function get_private_posts_cap_sql( $post_type ) { 
  5056. return get_posts_by_author_sql( $post_type, false ); 
  5057.  
  5058. /** 
  5059. * Retrieve the post SQL based on capability, author, and type. 
  5060. * 
  5061. * @since 3.0.0 
  5062. * @since 4.3.0 Introduced the ability to pass an array of post types to `$post_type`. 
  5063. * 
  5064. * @see get_private_posts_cap_sql() 
  5065. * @global wpdb $wpdb WordPress database abstraction object. 
  5066. * 
  5067. * @param array|string $post_type Single post type or an array of post types. 
  5068. * @param bool $full Optional. Returns a full WHERE statement instead of just 
  5069. * an 'andalso' term. Default true. 
  5070. * @param int $post_author Optional. Query posts having a single author ID. Default null. 
  5071. * @param bool $public_only Optional. Only return public posts. Skips cap checks for 
  5072. * $current_user. Default false. 
  5073. * @return string SQL WHERE code that can be added to a query. 
  5074. */ 
  5075. function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) { 
  5076. global $wpdb; 
  5077.  
  5078. if ( is_array( $post_type ) ) { 
  5079. $post_types = $post_type; 
  5080. } else { 
  5081. $post_types = array( $post_type ); 
  5082.  
  5083. $post_type_clauses = array(); 
  5084. foreach ( $post_types as $post_type ) { 
  5085. $post_type_obj = get_post_type_object( $post_type ); 
  5086. if ( ! $post_type_obj ) { 
  5087. continue; 
  5088.  
  5089. /** 
  5090. * Filters the capability to read private posts for a custom post type 
  5091. * when generating SQL for getting posts by author. 
  5092. * 
  5093. * @since 2.2.0 
  5094. * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless". 
  5095. * 
  5096. * @param string $cap Capability. 
  5097. */ 
  5098. if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) ) { 
  5099. $cap = current_user_can( $post_type_obj->cap->read_private_posts ); 
  5100.  
  5101. // Only need to check the cap if $public_only is false. 
  5102. $post_status_sql = "post_status = 'publish'"; 
  5103. if ( false === $public_only ) { 
  5104. if ( $cap ) { 
  5105. // Does the user have the capability to view private posts? Guess so. 
  5106. $post_status_sql .= " OR post_status = 'private'"; 
  5107. } elseif ( is_user_logged_in() ) { 
  5108. // Users can view their own private posts. 
  5109. $id = get_current_user_id(); 
  5110. if ( null === $post_author || ! $full ) { 
  5111. $post_status_sql .= " OR post_status = 'private' AND post_author = $id"; 
  5112. } elseif ( $id == (int) $post_author ) { 
  5113. $post_status_sql .= " OR post_status = 'private'"; 
  5114. } // else none 
  5115. } // else none 
  5116.  
  5117. $post_type_clauses[] = "( post_type = '" . $post_type . "' AND ( $post_status_sql ) )"; 
  5118.  
  5119. if ( empty( $post_type_clauses ) ) { 
  5120. return $full ? 'WHERE 1 = 0' : '1 = 0'; 
  5121.  
  5122. $sql = '( '. implode( ' OR ', $post_type_clauses ) . ' )'; 
  5123.  
  5124. if ( null !== $post_author ) { 
  5125. $sql .= $wpdb->prepare( ' AND post_author = %d', $post_author ); 
  5126.  
  5127. if ( $full ) { 
  5128. $sql = 'WHERE ' . $sql; 
  5129.  
  5130. return $sql; 
  5131.  
  5132. /** 
  5133. * Retrieve the date that the last post was published. 
  5134. * 
  5135. * The server timezone is the default and is the difference between GMT and 
  5136. * server time. The 'blog' value is the date when the last post was posted. The 
  5137. * 'gmt' is when the last post was posted in GMT formatted date. 
  5138. * 
  5139. * @since 0.71 
  5140. * @since 4.4.0 The `$post_type` argument was added. 
  5141. * 
  5142. * @param string $timezone Optional. The timezone for the timestamp. Accepts 'server', 'blog', or 'gmt'. 
  5143. * 'server' uses the server's internal timezone. 
  5144. * 'blog' uses the `post_modified` field, which proxies to the timezone set for the site. 
  5145. * 'gmt' uses the `post_modified_gmt` field. 
  5146. * Default 'server'. 
  5147. * @param string $post_type Optional. The post type to check. Default 'any'. 
  5148. * @return string The date of the last post. 
  5149. */ 
  5150. function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) { 
  5151. /** 
  5152. * Filters the date the last post was published. 
  5153. * 
  5154. * @since 2.3.0 
  5155. * 
  5156. * @param string $date Date the last post was published. 
  5157. * @param string $timezone Location to use for getting the post published date. 
  5158. * See get_lastpostdate() for accepted `$timezone` values. 
  5159. */ 
  5160. return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date', $post_type ), $timezone ); 
  5161.  
  5162. /** 
  5163. * Get the timestamp of the last time any post was modified. 
  5164. * 
  5165. * The server timezone is the default and is the difference between GMT and 
  5166. * server time. The 'blog' value is just when the last post was modified. The 
  5167. * 'gmt' is when the last post was modified in GMT time. 
  5168. * 
  5169. * @since 1.2.0 
  5170. * @since 4.4.0 The `$post_type` argument was added. 
  5171. * 
  5172. * @param string $timezone Optional. The timezone for the timestamp. See get_lastpostdate() 
  5173. * for information on accepted values. 
  5174. * Default 'server'. 
  5175. * @param string $post_type Optional. The post type to check. Default 'any'. 
  5176. * @return string The timestamp. 
  5177. */ 
  5178. function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) { 
  5179. /** 
  5180. * Pre-filter the return value of get_lastpostmodified() before the query is run. 
  5181. * 
  5182. * @since 4.4.0 
  5183. * 
  5184. * @param string $lastpostmodified Date the last post was modified. 
  5185. * Returning anything other than false will short-circuit the function. 
  5186. * @param string $timezone Location to use for getting the post modified date. 
  5187. * See get_lastpostdate() for accepted `$timezone` values. 
  5188. * @param string $post_type The post type to check. 
  5189. */ 
  5190. $lastpostmodified = apply_filters( 'pre_get_lastpostmodified', false, $timezone, $post_type ); 
  5191. if ( false !== $lastpostmodified ) { 
  5192. return $lastpostmodified; 
  5193.  
  5194. $lastpostmodified = _get_last_post_time( $timezone, 'modified', $post_type ); 
  5195.  
  5196. $lastpostdate = get_lastpostdate($timezone); 
  5197. if ( $lastpostdate > $lastpostmodified ) { 
  5198. $lastpostmodified = $lastpostdate; 
  5199.  
  5200. /** 
  5201. * Filters the date the last post was modified. 
  5202. * 
  5203. * @since 2.3.0 
  5204. * 
  5205. * @param string $lastpostmodified Date the last post was modified. 
  5206. * @param string $timezone Location to use for getting the post modified date. 
  5207. * See get_lastpostdate() for accepted `$timezone` values. 
  5208. */ 
  5209. return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone ); 
  5210.  
  5211. /** 
  5212. * Get the timestamp of the last time any post was modified or published. 
  5213. * 
  5214. * @since 3.1.0 
  5215. * @since 4.4.0 The `$post_type` argument was added. 
  5216. * @access private 
  5217. * 
  5218. * @global wpdb $wpdb WordPress database abstraction object. 
  5219. * 
  5220. * @param string $timezone The timezone for the timestamp. See get_lastpostdate(). 
  5221. * for information on accepted values. 
  5222. * @param string $field Post field to check. Accepts 'date' or 'modified'. 
  5223. * @param string $post_type Optional. The post type to check. Default 'any'. 
  5224. * @return string|false The timestamp. 
  5225. */ 
  5226. function _get_last_post_time( $timezone, $field, $post_type = 'any' ) { 
  5227. global $wpdb; 
  5228.  
  5229. if ( ! in_array( $field, array( 'date', 'modified' ) ) ) { 
  5230. return false; 
  5231.  
  5232. $timezone = strtolower( $timezone ); 
  5233.  
  5234. $key = "lastpost{$field}:$timezone"; 
  5235. if ( 'any' !== $post_type ) { 
  5236. $key .= ':' . sanitize_key( $post_type ); 
  5237.  
  5238. $date = wp_cache_get( $key, 'timeinfo' ); 
  5239. if ( false !== $date ) { 
  5240. return $date; 
  5241.  
  5242. if ( 'any' === $post_type ) { 
  5243. $post_types = get_post_types( array( 'public' => true ) ); 
  5244. array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) ); 
  5245. $post_types = "'" . implode( "', '", $post_types ) . "'"; 
  5246. } else { 
  5247. $post_types = "'" . sanitize_key( $post_type ) . "'"; 
  5248.  
  5249. switch ( $timezone ) { 
  5250. case 'gmt': 
  5251. $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1"); 
  5252. break; 
  5253. case 'blog': 
  5254. $date = $wpdb->get_var("SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1"); 
  5255. break; 
  5256. case 'server': 
  5257. $add_seconds_server = date( 'Z' ); 
  5258. $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1"); 
  5259. break; 
  5260.  
  5261. if ( $date ) { 
  5262. wp_cache_set( $key, $date, 'timeinfo' ); 
  5263.  
  5264. return $date; 
  5265.  
  5266. return false; 
  5267.  
  5268. /** 
  5269. * Updates posts in cache. 
  5270. * 
  5271. * @since 1.5.1 
  5272. * 
  5273. * @param array $posts Array of post objects, passed by reference. 
  5274. */ 
  5275. function update_post_cache( &$posts ) { 
  5276. if ( ! $posts ) 
  5277. return; 
  5278.  
  5279. foreach ( $posts as $post ) 
  5280. wp_cache_add( $post->ID, $post, 'posts' ); 
  5281.  
  5282. /** 
  5283. * Will clean the post in the cache. 
  5284. * 
  5285. * Cleaning means delete from the cache of the post. Will call to clean the term 
  5286. * object cache associated with the post ID. 
  5287. * 
  5288. * This function not run if $_wp_suspend_cache_invalidation is not empty. See 
  5289. * wp_suspend_cache_invalidation(). 
  5290. * 
  5291. * @since 2.0.0 
  5292. * 
  5293. * @global bool $_wp_suspend_cache_invalidation 
  5294. * 
  5295. * @param int|WP_Post $post Post ID or post object to remove from the cache. 
  5296. */ 
  5297. function clean_post_cache( $post ) { 
  5298. global $_wp_suspend_cache_invalidation; 
  5299.  
  5300. if ( ! empty( $_wp_suspend_cache_invalidation ) ) 
  5301. return; 
  5302.  
  5303. $post = get_post( $post ); 
  5304. if ( empty( $post ) ) 
  5305. return; 
  5306.  
  5307. wp_cache_delete( $post->ID, 'posts' ); 
  5308. wp_cache_delete( $post->ID, 'post_meta' ); 
  5309.  
  5310. clean_object_term_cache( $post->ID, $post->post_type ); 
  5311.  
  5312. wp_cache_delete( 'wp_get_archives', 'general' ); 
  5313.  
  5314. /** 
  5315. * Fires immediately after the given post's cache is cleaned. 
  5316. * 
  5317. * @since 2.5.0 
  5318. * 
  5319. * @param int $post_id Post ID. 
  5320. * @param WP_Post $post Post object. 
  5321. */ 
  5322. do_action( 'clean_post_cache', $post->ID, $post ); 
  5323.  
  5324. if ( 'page' == $post->post_type ) { 
  5325. wp_cache_delete( 'all_page_ids', 'posts' ); 
  5326.  
  5327. /** 
  5328. * Fires immediately after the given page's cache is cleaned. 
  5329. * 
  5330. * @since 2.5.0 
  5331. * 
  5332. * @param int $post_id Post ID. 
  5333. */ 
  5334. do_action( 'clean_page_cache', $post->ID ); 
  5335.  
  5336. wp_cache_set( 'last_changed', microtime(), 'posts' ); 
  5337.  
  5338. /** 
  5339. * Call major cache updating functions for list of Post objects. 
  5340. * 
  5341. * @since 1.5.0 
  5342. * 
  5343. * @param array $posts Array of Post objects 
  5344. * @param string $post_type Optional. Post type. Default 'post'. 
  5345. * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. 
  5346. * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 
  5347. */ 
  5348. function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) { 
  5349. // No point in doing all this work if we didn't match any posts. 
  5350. if ( !$posts ) 
  5351. return; 
  5352.  
  5353. update_post_cache($posts); 
  5354.  
  5355. $post_ids = array(); 
  5356. foreach ( $posts as $post ) 
  5357. $post_ids[] = $post->ID; 
  5358.  
  5359. if ( ! $post_type ) 
  5360. $post_type = 'any'; 
  5361.  
  5362. if ( $update_term_cache ) { 
  5363. if ( is_array($post_type) ) { 
  5364. $ptypes = $post_type; 
  5365. } elseif ( 'any' == $post_type ) { 
  5366. $ptypes = array(); 
  5367. // Just use the post_types in the supplied posts. 
  5368. foreach ( $posts as $post ) { 
  5369. $ptypes[] = $post->post_type; 
  5370. $ptypes = array_unique($ptypes); 
  5371. } else { 
  5372. $ptypes = array($post_type); 
  5373.  
  5374. if ( ! empty($ptypes) ) 
  5375. update_object_term_cache($post_ids, $ptypes); 
  5376.  
  5377. if ( $update_meta_cache ) 
  5378. update_postmeta_cache($post_ids); 
  5379.  
  5380. /** 
  5381. * Updates metadata cache for list of post IDs. 
  5382. * 
  5383. * Performs SQL query to retrieve the metadata for the post IDs and updates the 
  5384. * metadata cache for the posts. Therefore, the functions, which call this 
  5385. * function, do not need to perform SQL queries on their own. 
  5386. * 
  5387. * @since 2.1.0 
  5388. * 
  5389. * @param array $post_ids List of post IDs. 
  5390. * @return array|false Returns false if there is nothing to update or an array 
  5391. * of metadata. 
  5392. */ 
  5393. function update_postmeta_cache( $post_ids ) { 
  5394. return update_meta_cache('post', $post_ids); 
  5395.  
  5396. /** 
  5397. * Will clean the attachment in the cache. 
  5398. * 
  5399. * Cleaning means delete from the cache. Optionally will clean the term 
  5400. * object cache associated with the attachment ID. 
  5401. * 
  5402. * This function will not run if $_wp_suspend_cache_invalidation is not empty. 
  5403. * 
  5404. * @since 3.0.0 
  5405. * 
  5406. * @global bool $_wp_suspend_cache_invalidation 
  5407. * 
  5408. * @param int $id The attachment ID in the cache to clean. 
  5409. * @param bool $clean_terms Optional. Whether to clean terms cache. Default false. 
  5410. */ 
  5411. function clean_attachment_cache( $id, $clean_terms = false ) { 
  5412. global $_wp_suspend_cache_invalidation; 
  5413.  
  5414. if ( !empty($_wp_suspend_cache_invalidation) ) 
  5415. return; 
  5416.  
  5417. $id = (int) $id; 
  5418.  
  5419. wp_cache_delete($id, 'posts'); 
  5420. wp_cache_delete($id, 'post_meta'); 
  5421.  
  5422. if ( $clean_terms ) 
  5423. clean_object_term_cache($id, 'attachment'); 
  5424.  
  5425. /** 
  5426. * Fires after the given attachment's cache is cleaned. 
  5427. * 
  5428. * @since 3.0.0 
  5429. * 
  5430. * @param int $id Attachment ID. 
  5431. */ 
  5432. do_action( 'clean_attachment_cache', $id ); 
  5433.  
  5434. // 
  5435.  // Hooks 
  5436. // 
  5437.   
  5438. /** 
  5439. * Hook for managing future post transitions to published. 
  5440. * 
  5441. * @since 2.3.0 
  5442. * @access private 
  5443. * 
  5444. * @see wp_clear_scheduled_hook() 
  5445. * @global wpdb $wpdb WordPress database abstraction object. 
  5446. * 
  5447. * @param string $new_status New post status. 
  5448. * @param string $old_status Previous post status. 
  5449. * @param WP_Post $post Post object. 
  5450. */ 
  5451. function _transition_post_status( $new_status, $old_status, $post ) { 
  5452. global $wpdb; 
  5453.  
  5454. if ( $old_status != 'publish' && $new_status == 'publish' ) { 
  5455. // Reset GUID if transitioning to publish and it is empty. 
  5456. if ( '' == get_the_guid($post->ID) ) 
  5457. $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) ); 
  5458.  
  5459. /** 
  5460. * Fires when a post's status is transitioned from private to published. 
  5461. * 
  5462. * @since 1.5.0 
  5463. * @deprecated 2.3.0 Use 'private_to_publish' instead. 
  5464. * 
  5465. * @param int $post_id Post ID. 
  5466. */ 
  5467. do_action('private_to_published', $post->ID); 
  5468.  
  5469. // If published posts changed clear the lastpostmodified cache. 
  5470. if ( 'publish' == $new_status || 'publish' == $old_status) { 
  5471. foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { 
  5472. wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' ); 
  5473. wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' ); 
  5474. wp_cache_delete( "lastpostdate:$timezone:{$post->post_type}", 'timeinfo' ); 
  5475.  
  5476. if ( $new_status !== $old_status ) { 
  5477. wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' ); 
  5478. wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' ); 
  5479.  
  5480. // Always clears the hook in case the post status bounced from future to draft. 
  5481. wp_clear_scheduled_hook('publish_future_post', array( $post->ID ) ); 
  5482.  
  5483. /** 
  5484. * Hook used to schedule publication for a post marked for the future. 
  5485. * 
  5486. * The $post properties used and must exist are 'ID' and 'post_date_gmt'. 
  5487. * 
  5488. * @since 2.3.0 
  5489. * @access private 
  5490. * 
  5491. * @param int $deprecated Not used. Can be set to null. Never implemented. Not marked 
  5492. * as deprecated with _deprecated_argument() as it conflicts with 
  5493. * wp_transition_post_status() and the default filter for _future_post_hook(). 
  5494. * @param WP_Post $post Post object. 
  5495. */ 
  5496. function _future_post_hook( $deprecated, $post ) { 
  5497. wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); 
  5498. wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT') , 'publish_future_post', array( $post->ID ) ); 
  5499.  
  5500. /** 
  5501. * Hook to schedule pings and enclosures when a post is published. 
  5502. * 
  5503. * Uses XMLRPC_REQUEST and WP_IMPORTING constants. 
  5504. * 
  5505. * @since 2.3.0 
  5506. * @access private 
  5507. * 
  5508. * @param int $post_id The ID in the database table of the post being published. 
  5509. */ 
  5510. function _publish_post_hook( $post_id ) { 
  5511. if ( defined( 'XMLRPC_REQUEST' ) ) { 
  5512. /** 
  5513. * Fires when _publish_post_hook() is called during an XML-RPC request. 
  5514. * 
  5515. * @since 2.1.0 
  5516. * 
  5517. * @param int $post_id Post ID. 
  5518. */ 
  5519. do_action( 'xmlrpc_publish_post', $post_id ); 
  5520.  
  5521. if ( defined('WP_IMPORTING') ) 
  5522. return; 
  5523.  
  5524. if ( get_option('default_pingback_flag') ) 
  5525. add_post_meta( $post_id, '_pingme', '1' ); 
  5526. add_post_meta( $post_id, '_encloseme', '1' ); 
  5527.  
  5528. wp_schedule_single_event(time(), 'do_pings'); 
  5529.  
  5530. /** 
  5531. * Return the post's parent's post_ID 
  5532. * 
  5533. * @since 3.1.0 
  5534. * 
  5535. * @param int $post_ID 
  5536. * 
  5537. * @return int|false Post parent ID, otherwise false. 
  5538. */ 
  5539. function wp_get_post_parent_id( $post_ID ) { 
  5540. $post = get_post( $post_ID ); 
  5541. if ( !$post || is_wp_error( $post ) ) 
  5542. return false; 
  5543. return (int) $post->post_parent; 
  5544.  
  5545. /** 
  5546. * Check the given subset of the post hierarchy for hierarchy loops. 
  5547. * 
  5548. * Prevents loops from forming and breaks those that it finds. Attached 
  5549. * to the {@see 'wp_insert_post_parent'} filter. 
  5550. * 
  5551. * @since 3.1.0 
  5552. * 
  5553. * @see wp_find_hierarchy_loop() 
  5554. * 
  5555. * @param int $post_parent ID of the parent for the post we're checking. 
  5556. * @param int $post_ID ID of the post we're checking. 
  5557. * @return int The new post_parent for the post, 0 otherwise. 
  5558. */ 
  5559. function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) { 
  5560. // Nothing fancy here - bail. 
  5561. if ( !$post_parent ) 
  5562. return 0; 
  5563.  
  5564. // New post can't cause a loop. 
  5565. if ( empty( $post_ID ) ) 
  5566. return $post_parent; 
  5567.  
  5568. // Can't be its own parent. 
  5569. if ( $post_parent == $post_ID ) 
  5570. return 0; 
  5571.  
  5572. // Now look for larger loops. 
  5573. if ( !$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) ) 
  5574. return $post_parent; // No loop 
  5575.  
  5576. // Setting $post_parent to the given value causes a loop. 
  5577. if ( isset( $loop[$post_ID] ) ) 
  5578. return 0; 
  5579.  
  5580. // There's a loop, but it doesn't contain $post_ID. Break the loop. 
  5581. foreach ( array_keys( $loop ) as $loop_member ) 
  5582. wp_update_post( array( 'ID' => $loop_member, 'post_parent' => 0 ) ); 
  5583.  
  5584. return $post_parent; 
  5585.  
  5586. /** 
  5587. * Set a post thumbnail. 
  5588. * 
  5589. * @since 3.1.0 
  5590. * 
  5591. * @param int|WP_Post $post Post ID or post object where thumbnail should be attached. 
  5592. * @param int $thumbnail_id Thumbnail to attach. 
  5593. * @return int|bool True on success, false on failure. 
  5594. */ 
  5595. function set_post_thumbnail( $post, $thumbnail_id ) { 
  5596. $post = get_post( $post ); 
  5597. $thumbnail_id = absint( $thumbnail_id ); 
  5598. if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) { 
  5599. if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) 
  5600. return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id ); 
  5601. else 
  5602. return delete_post_meta( $post->ID, '_thumbnail_id' ); 
  5603. return false; 
  5604.  
  5605. /** 
  5606. * Remove a post thumbnail. 
  5607. * 
  5608. * @since 3.3.0 
  5609. * 
  5610. * @param int|WP_Post $post Post ID or post object where thumbnail should be removed from. 
  5611. * @return bool True on success, false on failure. 
  5612. */ 
  5613. function delete_post_thumbnail( $post ) { 
  5614. $post = get_post( $post ); 
  5615. if ( $post ) 
  5616. return delete_post_meta( $post->ID, '_thumbnail_id' ); 
  5617. return false; 
  5618.  
  5619. /** 
  5620. * Delete auto-drafts for new posts that are > 7 days old. 
  5621. * 
  5622. * @since 3.4.0 
  5623. * 
  5624. * @global wpdb $wpdb WordPress database abstraction object. 
  5625. */ 
  5626. function wp_delete_auto_drafts() { 
  5627. global $wpdb; 
  5628.  
  5629. // Cleanup old auto-drafts more than 7 days old. 
  5630. $old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" ); 
  5631. foreach ( (array) $old_posts as $delete ) { 
  5632. // Force delete. 
  5633. wp_delete_post( $delete, true ); 
  5634.  
  5635. /** 
  5636. * Queues posts for lazy-loading of term meta. 
  5637. * 
  5638. * @since 4.5.0 
  5639. * 
  5640. * @param array $posts Array of WP_Post objects. 
  5641. */ 
  5642. function wp_queue_posts_for_term_meta_lazyload( $posts ) { 
  5643. $post_type_taxonomies = $term_ids = array(); 
  5644. foreach ( $posts as $post ) { 
  5645. if ( ! ( $post instanceof WP_Post ) ) { 
  5646. continue; 
  5647.  
  5648. if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) { 
  5649. $post_type_taxonomies[ $post->post_type ] = get_object_taxonomies( $post->post_type ); 
  5650.  
  5651. foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) { 
  5652. // Term cache should already be primed by `update_post_term_cache()`. 
  5653. $terms = get_object_term_cache( $post->ID, $taxonomy ); 
  5654. if ( false !== $terms ) { 
  5655. foreach ( $terms as $term ) { 
  5656. if ( ! isset( $term_ids[ $term->term_id ] ) ) { 
  5657. $term_ids[] = $term->term_id; 
  5658.  
  5659. if ( $term_ids ) { 
  5660. $lazyloader = wp_metadata_lazyloader(); 
  5661. $lazyloader->queue_objects( 'term', $term_ids ); 
  5662.  
  5663. /** 
  5664. * Update the custom taxonomies' term counts when a post's status is changed. 
  5665. * 
  5666. * For example, default posts term counts (for custom taxonomies) don't include 
  5667. * private / draft posts. 
  5668. * 
  5669. * @since 3.3.0 
  5670. * @access private 
  5671. * 
  5672. * @param string $new_status New post status. 
  5673. * @param string $old_status Old post status. 
  5674. * @param WP_Post $post Post object. 
  5675. */ 
  5676. function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) { 
  5677. // Update counts for the post's terms. 
  5678. foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) { 
  5679. $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) ); 
  5680. wp_update_term_count( $tt_ids, $taxonomy ); 
  5681.  
  5682. /** 
  5683. * Adds any posts from the given ids to the cache that do not already exist in cache 
  5684. * 
  5685. * @since 3.4.0 
  5686. * @access private 
  5687. * 
  5688. * @see update_post_caches() 
  5689. * 
  5690. * @global wpdb $wpdb WordPress database abstraction object. 
  5691. * 
  5692. * @param array $ids ID list. 
  5693. * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. 
  5694. * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. 
  5695. */ 
  5696. function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) { 
  5697. global $wpdb; 
  5698.  
  5699. $non_cached_ids = _get_non_cached_ids( $ids, 'posts' ); 
  5700. if ( !empty( $non_cached_ids ) ) { 
  5701. $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", join( ", ", $non_cached_ids ) ) ); 
  5702.  
  5703. update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache ); 
  5704.  
  5705. /** 
  5706. * Adds a suffix if any trashed posts have a given slug. 
  5707. * 
  5708. * Store its desired (i.e. current) slug so it can try to reclaim it 
  5709. * if the post is untrashed. 
  5710. * 
  5711. * For internal use. 
  5712. * 
  5713. * @since 4.5.0 
  5714. * @access private 
  5715. * 
  5716. * @param string $post_name Slug. 
  5717. * @param string $post_ID Optional. Post ID that should be ignored. Default 0. 
  5718. */ 
  5719. function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) { 
  5720. $trashed_posts_with_desired_slug = get_posts( array( 
  5721. 'name' => $post_name,  
  5722. 'post_status' => 'trash',  
  5723. 'post_type' => 'any',  
  5724. 'nopaging' => true,  
  5725. 'post__not_in' => array( $post_ID ) 
  5726. ) ); 
  5727.  
  5728. if ( ! empty( $trashed_posts_with_desired_slug ) ) { 
  5729. foreach ( $trashed_posts_with_desired_slug as $_post ) { 
  5730. wp_add_trashed_suffix_to_post_name_for_post( $_post ); 
  5731.  
  5732. /** 
  5733. * Adds a trashed suffix for a given post. 
  5734. * 
  5735. * Store its desired (i.e. current) slug so it can try to reclaim it 
  5736. * if the post is untrashed. 
  5737. * 
  5738. * For internal use. 
  5739. * 
  5740. * @since 4.5.0 
  5741. * @access private 
  5742. * 
  5743. * @param WP_Post $post The post. 
  5744. * @return string New slug for the post. 
  5745. */ 
  5746. function wp_add_trashed_suffix_to_post_name_for_post( $post ) { 
  5747. global $wpdb; 
  5748.  
  5749. $post = get_post( $post ); 
  5750.  
  5751. if ( '__trashed' === substr( $post->post_name, -9 ) ) { 
  5752. return $post->post_name; 
  5753. add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name ); 
  5754. $post_name = _truncate_post_slug( $post->post_name, 191 ) . '__trashed'; 
  5755. $wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) ); 
  5756. clean_post_cache( $post->ID ); 
  5757. return $post_name; 
  5758.  
  5759. /** 
  5760. * Filter the SQL clauses of an attachment query to include filenames. 
  5761. * 
  5762. * @since 4.7.0 
  5763. * @access private 
  5764. * 
  5765. * @global wpdb $wpdb WordPress database abstraction object. 
  5766. * 
  5767. * @param array $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY,  
  5768. * DISTINCT, fields (SELECT), and LIMITS clauses. 
  5769. * @return array The modified clauses. 
  5770. */ 
  5771. function _filter_query_attachment_filenames( $clauses ) { 
  5772. global $wpdb; 
  5773. remove_filter( 'posts_clauses', __FUNCTION__ ); 
  5774.  
  5775. // Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs. 
  5776. $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )"; 
  5777.  
  5778. $clauses['groupby'] = "{$wpdb->posts}.ID"; 
  5779.  
  5780. $clauses['where'] = preg_replace( 
  5781. "/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/",  
  5782. "$0 OR ( sq1.meta_value $1 $2 )",  
  5783. $clauses['where'] ); 
  5784.  
  5785. return $clauses; 
.