mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Post functions and post utility function.
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Post
mp-wp_genesis 7 * @since 1.5.0
mp-wp_genesis 8 */
mp-wp_genesis 9
mp-wp_genesis 10 /**
mp-wp_genesis 11 * Retrieve attached file path based on attachment ID.
mp-wp_genesis 12 *
mp-wp_genesis 13 * You can optionally send it through the 'get_attached_file' filter, but by
mp-wp_genesis 14 * default it will just return the file path unfiltered.
mp-wp_genesis 15 *
mp-wp_genesis 16 * The function works by getting the single post meta name, named
mp-wp_genesis 17 * '_wp_attached_file' and returning it. This is a convenience function to
mp-wp_genesis 18 * prevent looking up the meta name and provide a mechanism for sending the
mp-wp_genesis 19 * attached filename through a filter.
mp-wp_genesis 20 *
mp-wp_genesis 21 * @since 2.0.0
mp-wp_genesis 22 * @uses apply_filters() Calls 'get_attached_file' on file path and attachment ID.
mp-wp_genesis 23 *
mp-wp_genesis 24 * @param int $attachment_id Attachment ID.
mp-wp_genesis 25 * @param bool $unfiltered Whether to apply filters or not.
mp-wp_genesis 26 * @return string The file path to the attached file.
mp-wp_genesis 27 */
mp-wp_genesis 28 function get_attached_file( $attachment_id, $unfiltered = false ) {
mp-wp_genesis 29 $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
mp-wp_genesis 30 // If the file is relative, prepend upload dir
mp-wp_genesis 31 if ( 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) )
mp-wp_genesis 32 $file = $uploads['basedir'] . "/$file";
mp-wp_genesis 33 if ( $unfiltered )
mp-wp_genesis 34 return $file;
mp-wp_genesis 35 return apply_filters( 'get_attached_file', $file, $attachment_id );
mp-wp_genesis 36 }
mp-wp_genesis 37
mp-wp_genesis 38 /**
mp-wp_genesis 39 * Update attachment file path based on attachment ID.
mp-wp_genesis 40 *
mp-wp_genesis 41 * Used to update the file path of the attachment, which uses post meta name
mp-wp_genesis 42 * '_wp_attached_file' to store the path of the attachment.
mp-wp_genesis 43 *
mp-wp_genesis 44 * @since 2.1.0
mp-wp_genesis 45 * @uses apply_filters() Calls 'update_attached_file' on file path and attachment ID.
mp-wp_genesis 46 *
mp-wp_genesis 47 * @param int $attachment_id Attachment ID
mp-wp_genesis 48 * @param string $file File path for the attachment
mp-wp_genesis 49 * @return bool False on failure, true on success.
mp-wp_genesis 50 */
mp-wp_genesis 51 function update_attached_file( $attachment_id, $file ) {
mp-wp_genesis 52 if ( !get_post( $attachment_id ) )
mp-wp_genesis 53 return false;
mp-wp_genesis 54
mp-wp_genesis 55 $file = apply_filters( 'update_attached_file', $file, $attachment_id );
mp-wp_genesis 56
mp-wp_genesis 57 // Make the file path relative to the upload dir
mp-wp_genesis 58 if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory
mp-wp_genesis 59 if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path
mp-wp_genesis 60 $file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path
mp-wp_genesis 61 $file = ltrim($file, '/');
mp-wp_genesis 62 }
mp-wp_genesis 63 }
mp-wp_genesis 64
mp-wp_genesis 65 return update_post_meta( $attachment_id, '_wp_attached_file', $file );
mp-wp_genesis 66 }
mp-wp_genesis 67
mp-wp_genesis 68 /**
mp-wp_genesis 69 * Retrieve all children of the post parent ID.
mp-wp_genesis 70 *
mp-wp_genesis 71 * Normally, without any enhancements, the children would apply to pages. In the
mp-wp_genesis 72 * context of the inner workings of WordPress, pages, posts, and attachments
mp-wp_genesis 73 * share the same table, so therefore the functionality could apply to any one
mp-wp_genesis 74 * of them. It is then noted that while this function does not work on posts, it
mp-wp_genesis 75 * does not mean that it won't work on posts. It is recommended that you know
mp-wp_genesis 76 * what context you wish to retrieve the children of.
mp-wp_genesis 77 *
mp-wp_genesis 78 * Attachments may also be made the child of a post, so if that is an accurate
mp-wp_genesis 79 * statement (which needs to be verified), it would then be possible to get
mp-wp_genesis 80 * all of the attachments for a post. Attachments have since changed since
mp-wp_genesis 81 * version 2.5, so this is most likely unaccurate, but serves generally as an
mp-wp_genesis 82 * example of what is possible.
mp-wp_genesis 83 *
mp-wp_genesis 84 * The arguments listed as defaults are for this function and also of the
mp-wp_genesis 85 * {@link get_posts()} function. The arguments are combined with the
mp-wp_genesis 86 * get_children defaults and are then passed to the {@link get_posts()}
mp-wp_genesis 87 * function, which accepts additional arguments. You can replace the defaults in
mp-wp_genesis 88 * this function, listed below and the additional arguments listed in the
mp-wp_genesis 89 * {@link get_posts()} function.
mp-wp_genesis 90 *
mp-wp_genesis 91 * The 'post_parent' is the most important argument and important attention
mp-wp_genesis 92 * needs to be paid to the $args parameter. If you pass either an object or an
mp-wp_genesis 93 * integer (number), then just the 'post_parent' is grabbed and everything else
mp-wp_genesis 94 * is lost. If you don't specify any arguments, then it is assumed that you are
mp-wp_genesis 95 * in The Loop and the post parent will be grabbed for from the current post.
mp-wp_genesis 96 *
mp-wp_genesis 97 * The 'post_parent' argument is the ID to get the children. The 'numberposts'
mp-wp_genesis 98 * is the amount of posts to retrieve that has a default of '-1', which is
mp-wp_genesis 99 * used to get all of the posts. Giving a number higher than 0 will only
mp-wp_genesis 100 * retrieve that amount of posts.
mp-wp_genesis 101 *
mp-wp_genesis 102 * The 'post_type' and 'post_status' arguments can be used to choose what
mp-wp_genesis 103 * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress
mp-wp_genesis 104 * post types are 'post', 'pages', and 'attachments'. The 'post_status'
mp-wp_genesis 105 * argument will accept any post status within the write administration panels.
mp-wp_genesis 106 *
mp-wp_genesis 107 * @see get_posts() Has additional arguments that can be replaced.
mp-wp_genesis 108 * @internal Claims made in the long description might be inaccurate.
mp-wp_genesis 109 *
mp-wp_genesis 110 * @since 2.0.0
mp-wp_genesis 111 *
mp-wp_genesis 112 * @param mixed $args Optional. User defined arguments for replacing the defaults.
mp-wp_genesis 113 * @param string $output Optional. Constant for return type, either OBJECT (default), ARRAY_A, ARRAY_N.
mp-wp_genesis 114 * @return array|bool False on failure and the type will be determined by $output parameter.
mp-wp_genesis 115 */
mp-wp_genesis 116 function &get_children($args = '', $output = OBJECT) {
mp-wp_genesis 117 if ( empty( $args ) ) {
mp-wp_genesis 118 if ( isset( $GLOBALS['post'] ) ) {
mp-wp_genesis 119 $args = array('post_parent' => (int) $GLOBALS['post']->post_parent );
mp-wp_genesis 120 } else {
mp-wp_genesis 121 return false;
mp-wp_genesis 122 }
mp-wp_genesis 123 } elseif ( is_object( $args ) ) {
mp-wp_genesis 124 $args = array('post_parent' => (int) $args->post_parent );
mp-wp_genesis 125 } elseif ( is_numeric( $args ) ) {
mp-wp_genesis 126 $args = array('post_parent' => (int) $args);
mp-wp_genesis 127 }
mp-wp_genesis 128
mp-wp_genesis 129 $defaults = array(
mp-wp_genesis 130 'numberposts' => -1, 'post_type' => 'any',
mp-wp_genesis 131 'post_status' => 'any', 'post_parent' => 0,
mp-wp_genesis 132 );
mp-wp_genesis 133
mp-wp_genesis 134 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 135
mp-wp_genesis 136 $children = get_posts( $r );
mp-wp_genesis 137 if ( !$children ) {
mp-wp_genesis 138 $kids = false;
mp-wp_genesis 139 return $kids;
mp-wp_genesis 140 }
mp-wp_genesis 141
mp-wp_genesis 142 update_post_cache($children);
mp-wp_genesis 143
mp-wp_genesis 144 foreach ( $children as $key => $child )
mp-wp_genesis 145 $kids[$child->ID] =& $children[$key];
mp-wp_genesis 146
mp-wp_genesis 147 if ( $output == OBJECT ) {
mp-wp_genesis 148 return $kids;
mp-wp_genesis 149 } elseif ( $output == ARRAY_A ) {
mp-wp_genesis 150 foreach ( (array) $kids as $kid )
mp-wp_genesis 151 $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
mp-wp_genesis 152 return $weeuns;
mp-wp_genesis 153 } elseif ( $output == ARRAY_N ) {
mp-wp_genesis 154 foreach ( (array) $kids as $kid )
mp-wp_genesis 155 $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
mp-wp_genesis 156 return $babes;
mp-wp_genesis 157 } else {
mp-wp_genesis 158 return $kids;
mp-wp_genesis 159 }
mp-wp_genesis 160 }
mp-wp_genesis 161
mp-wp_genesis 162 /**
mp-wp_genesis 163 * Get extended entry info (<!--more-->).
mp-wp_genesis 164 *
mp-wp_genesis 165 * There should not be any space after the second dash and before the word
mp-wp_genesis 166 * 'more'. There can be text or space(s) after the word 'more', but won't be
mp-wp_genesis 167 * referenced.
mp-wp_genesis 168 *
mp-wp_genesis 169 * The returned array has 'main' and 'extended' keys. Main has the text before
mp-wp_genesis 170 * the <code><!--more--></code>. The 'extended' key has the content after the
mp-wp_genesis 171 * <code><!--more--></code> comment.
mp-wp_genesis 172 *
mp-wp_genesis 173 * @since 1.0.0
mp-wp_genesis 174 *
mp-wp_genesis 175 * @param string $post Post content.
mp-wp_genesis 176 * @return array Post before ('main') and after ('extended').
mp-wp_genesis 177 */
mp-wp_genesis 178 function get_extended($post) {
mp-wp_genesis 179 //Match the new style more links
mp-wp_genesis 180 if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
mp-wp_genesis 181 list($main, $extended) = explode($matches[0], $post, 2);
mp-wp_genesis 182 } else {
mp-wp_genesis 183 $main = $post;
mp-wp_genesis 184 $extended = '';
mp-wp_genesis 185 }
mp-wp_genesis 186
mp-wp_genesis 187 // Strip leading and trailing whitespace
mp-wp_genesis 188 $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
mp-wp_genesis 189 $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
mp-wp_genesis 190
mp-wp_genesis 191 return array('main' => $main, 'extended' => $extended);
mp-wp_genesis 192 }
mp-wp_genesis 193
mp-wp_genesis 194 /**
mp-wp_genesis 195 * Retrieves post data given a post ID or post object.
mp-wp_genesis 196 *
mp-wp_genesis 197 * See {@link sanitize_post()} for optional $filter values. Also, the parameter
mp-wp_genesis 198 * $post, must be given as a variable, since it is passed by reference.
mp-wp_genesis 199 *
mp-wp_genesis 200 * @since 1.5.1
mp-wp_genesis 201 * @uses $wpdb
mp-wp_genesis 202 * @link http://codex.wordpress.org/Function_Reference/get_post
mp-wp_genesis 203 *
mp-wp_genesis 204 * @param int|object $post Post ID or post object.
mp-wp_genesis 205 * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
mp-wp_genesis 206 * @param string $filter Optional, default is raw.
mp-wp_genesis 207 * @return mixed Post data
mp-wp_genesis 208 */
mp-wp_genesis 209 function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
mp-wp_genesis 210 global $wpdb;
mp-wp_genesis 211 $null = null;
mp-wp_genesis 212
mp-wp_genesis 213 if ( empty($post) ) {
mp-wp_genesis 214 if ( isset($GLOBALS['post']) )
mp-wp_genesis 215 $_post = & $GLOBALS['post'];
mp-wp_genesis 216 else
mp-wp_genesis 217 return $null;
mp-wp_genesis 218 } elseif ( is_object($post) ) {
mp-wp_genesis 219 _get_post_ancestors($post);
mp-wp_genesis 220 wp_cache_add($post->ID, $post, 'posts');
mp-wp_genesis 221 $_post = &$post;
mp-wp_genesis 222 } else {
mp-wp_genesis 223 $post = (int) $post;
mp-wp_genesis 224 if ( ! $_post = wp_cache_get($post, 'posts') ) {
mp-wp_genesis 225 $_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post));
mp-wp_genesis 226 if ( ! $_post )
mp-wp_genesis 227 return $null;
mp-wp_genesis 228 _get_post_ancestors($_post);
mp-wp_genesis 229 wp_cache_add($_post->ID, $_post, 'posts');
mp-wp_genesis 230 }
mp-wp_genesis 231 }
mp-wp_genesis 232
mp-wp_genesis 233 $_post = sanitize_post($_post, $filter);
mp-wp_genesis 234
mp-wp_genesis 235 if ( $output == OBJECT ) {
mp-wp_genesis 236 return $_post;
mp-wp_genesis 237 } elseif ( $output == ARRAY_A ) {
mp-wp_genesis 238 $__post = get_object_vars($_post);
mp-wp_genesis 239 return $__post;
mp-wp_genesis 240 } elseif ( $output == ARRAY_N ) {
mp-wp_genesis 241 $__post = array_values(get_object_vars($_post));
mp-wp_genesis 242 return $__post;
mp-wp_genesis 243 } else {
mp-wp_genesis 244 return $_post;
mp-wp_genesis 245 }
mp-wp_genesis 246 }
mp-wp_genesis 247
mp-wp_genesis 248 /**
mp-wp_genesis 249 * Retrieve ancestors of a post.
mp-wp_genesis 250 *
mp-wp_genesis 251 * @since 2.5.0
mp-wp_genesis 252 *
mp-wp_genesis 253 * @param int|object $post Post ID or post object
mp-wp_genesis 254 * @return array Ancestor IDs or empty array if none are found.
mp-wp_genesis 255 */
mp-wp_genesis 256 function get_post_ancestors($post) {
mp-wp_genesis 257 $post = get_post($post);
mp-wp_genesis 258
mp-wp_genesis 259 if ( !empty($post->ancestors) )
mp-wp_genesis 260 return $post->ancestors;
mp-wp_genesis 261
mp-wp_genesis 262 return array();
mp-wp_genesis 263 }
mp-wp_genesis 264
mp-wp_genesis 265 /**
mp-wp_genesis 266 * Retrieve data from a post field based on Post ID.
mp-wp_genesis 267 *
mp-wp_genesis 268 * Examples of the post field will be, 'post_type', 'post_status', 'content',
mp-wp_genesis 269 * etc and based off of the post object property or key names.
mp-wp_genesis 270 *
mp-wp_genesis 271 * The context values are based off of the taxonomy filter functions and
mp-wp_genesis 272 * supported values are found within those functions.
mp-wp_genesis 273 *
mp-wp_genesis 274 * @since 2.3.0
mp-wp_genesis 275 * @uses sanitize_post_field() See for possible $context values.
mp-wp_genesis 276 *
mp-wp_genesis 277 * @param string $field Post field name
mp-wp_genesis 278 * @param id $post Post ID
mp-wp_genesis 279 * @param string $context Optional. How to filter the field. Default is display.
mp-wp_genesis 280 * @return WP_Error|string Value in post field or WP_Error on failure
mp-wp_genesis 281 */
mp-wp_genesis 282 function get_post_field( $field, $post, $context = 'display' ) {
mp-wp_genesis 283 $post = (int) $post;
mp-wp_genesis 284 $post = get_post( $post );
mp-wp_genesis 285
mp-wp_genesis 286 if ( is_wp_error($post) )
mp-wp_genesis 287 return $post;
mp-wp_genesis 288
mp-wp_genesis 289 if ( !is_object($post) )
mp-wp_genesis 290 return '';
mp-wp_genesis 291
mp-wp_genesis 292 if ( !isset($post->$field) )
mp-wp_genesis 293 return '';
mp-wp_genesis 294
mp-wp_genesis 295 return sanitize_post_field($field, $post->$field, $post->ID, $context);
mp-wp_genesis 296 }
mp-wp_genesis 297
mp-wp_genesis 298 /**
mp-wp_genesis 299 * Retrieve the mime type of an attachment based on the ID.
mp-wp_genesis 300 *
mp-wp_genesis 301 * This function can be used with any post type, but it makes more sense with
mp-wp_genesis 302 * attachments.
mp-wp_genesis 303 *
mp-wp_genesis 304 * @since 2.0.0
mp-wp_genesis 305 *
mp-wp_genesis 306 * @param int $ID Optional. Post ID.
mp-wp_genesis 307 * @return bool|string False on failure or returns the mime type
mp-wp_genesis 308 */
mp-wp_genesis 309 function get_post_mime_type($ID = '') {
mp-wp_genesis 310 $post = & get_post($ID);
mp-wp_genesis 311
mp-wp_genesis 312 if ( is_object($post) )
mp-wp_genesis 313 return $post->post_mime_type;
mp-wp_genesis 314
mp-wp_genesis 315 return false;
mp-wp_genesis 316 }
mp-wp_genesis 317
mp-wp_genesis 318 /**
mp-wp_genesis 319 * Retrieve the post status based on the Post ID.
mp-wp_genesis 320 *
mp-wp_genesis 321 * If the post ID is of an attachment, then the parent post status will be given
mp-wp_genesis 322 * instead.
mp-wp_genesis 323 *
mp-wp_genesis 324 * @since 2.0.0
mp-wp_genesis 325 *
mp-wp_genesis 326 * @param int $ID Post ID
mp-wp_genesis 327 * @return string|bool Post status or false on failure.
mp-wp_genesis 328 */
mp-wp_genesis 329 function get_post_status($ID = '') {
mp-wp_genesis 330 $post = get_post($ID);
mp-wp_genesis 331
mp-wp_genesis 332 if ( is_object($post) ) {
mp-wp_genesis 333 if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
mp-wp_genesis 334 return get_post_status($post->post_parent);
mp-wp_genesis 335 else
mp-wp_genesis 336 return $post->post_status;
mp-wp_genesis 337 }
mp-wp_genesis 338
mp-wp_genesis 339 return false;
mp-wp_genesis 340 }
mp-wp_genesis 341
mp-wp_genesis 342 /**
mp-wp_genesis 343 * Retrieve all of the WordPress supported post statuses.
mp-wp_genesis 344 *
mp-wp_genesis 345 * Posts have a limited set of valid status values, this provides the
mp-wp_genesis 346 * post_status values and descriptions.
mp-wp_genesis 347 *
mp-wp_genesis 348 * @since 2.5.0
mp-wp_genesis 349 *
mp-wp_genesis 350 * @return array List of post statuses.
mp-wp_genesis 351 */
mp-wp_genesis 352 function get_post_statuses( ) {
mp-wp_genesis 353 $status = array(
mp-wp_genesis 354 'draft' => __('Draft'),
mp-wp_genesis 355 'pending' => __('Pending Review'),
mp-wp_genesis 356 'private' => __('Private'),
mp-wp_genesis 357 'publish' => __('Published')
mp-wp_genesis 358 );
mp-wp_genesis 359
mp-wp_genesis 360 return $status;
mp-wp_genesis 361 }
mp-wp_genesis 362
mp-wp_genesis 363 /**
mp-wp_genesis 364 * Retrieve all of the WordPress support page statuses.
mp-wp_genesis 365 *
mp-wp_genesis 366 * Pages have a limited set of valid status values, this provides the
mp-wp_genesis 367 * post_status values and descriptions.
mp-wp_genesis 368 *
mp-wp_genesis 369 * @since 2.5.0
mp-wp_genesis 370 *
mp-wp_genesis 371 * @return array List of page statuses.
mp-wp_genesis 372 */
mp-wp_genesis 373 function get_page_statuses( ) {
mp-wp_genesis 374 $status = array(
mp-wp_genesis 375 'draft' => __('Draft'),
mp-wp_genesis 376 'private' => __('Private'),
mp-wp_genesis 377 'publish' => __('Published')
mp-wp_genesis 378 );
mp-wp_genesis 379
mp-wp_genesis 380 return $status;
mp-wp_genesis 381 }
mp-wp_genesis 382
mp-wp_genesis 383 /**
mp-wp_genesis 384 * Retrieve the post type of the current post or of a given post.
mp-wp_genesis 385 *
mp-wp_genesis 386 * @since 2.1.0
mp-wp_genesis 387 *
mp-wp_genesis 388 * @uses $wpdb
mp-wp_genesis 389 * @uses $posts The Loop post global
mp-wp_genesis 390 *
mp-wp_genesis 391 * @param mixed $post Optional. Post object or post ID.
mp-wp_genesis 392 * @return bool|string post type or false on failure.
mp-wp_genesis 393 */
mp-wp_genesis 394 function get_post_type($post = false) {
mp-wp_genesis 395 global $posts;
mp-wp_genesis 396
mp-wp_genesis 397 if ( false === $post )
mp-wp_genesis 398 $post = $posts[0];
mp-wp_genesis 399 elseif ( (int) $post )
mp-wp_genesis 400 $post = get_post($post, OBJECT);
mp-wp_genesis 401
mp-wp_genesis 402 if ( is_object($post) )
mp-wp_genesis 403 return $post->post_type;
mp-wp_genesis 404
mp-wp_genesis 405 return false;
mp-wp_genesis 406 }
mp-wp_genesis 407
mp-wp_genesis 408 /**
mp-wp_genesis 409 * Updates the post type for the post ID.
mp-wp_genesis 410 *
mp-wp_genesis 411 * The page or post cache will be cleaned for the post ID.
mp-wp_genesis 412 *
mp-wp_genesis 413 * @since 2.5.0
mp-wp_genesis 414 *
mp-wp_genesis 415 * @uses $wpdb
mp-wp_genesis 416 *
mp-wp_genesis 417 * @param int $post_id Post ID to change post type. Not actually optional.
mp-wp_genesis 418 * @param string $post_type Optional, default is post. Supported values are 'post' or 'page' to name a few.
mp-wp_genesis 419 * @return int Amount of rows changed. Should be 1 for success and 0 for failure.
mp-wp_genesis 420 */
mp-wp_genesis 421 function set_post_type( $post_id = 0, $post_type = 'post' ) {
mp-wp_genesis 422 global $wpdb;
mp-wp_genesis 423
mp-wp_genesis 424 $post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db');
mp-wp_genesis 425 $return = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_type = %s WHERE ID = %d", $post_type, $post_id) );
mp-wp_genesis 426
mp-wp_genesis 427 if ( 'page' == $post_type )
mp-wp_genesis 428 clean_page_cache($post_id);
mp-wp_genesis 429 else
mp-wp_genesis 430 clean_post_cache($post_id);
mp-wp_genesis 431
mp-wp_genesis 432 return $return;
mp-wp_genesis 433 }
mp-wp_genesis 434
mp-wp_genesis 435 /**
mp-wp_genesis 436 * Retrieve list of latest posts or posts matching criteria.
mp-wp_genesis 437 *
mp-wp_genesis 438 * The defaults are as follows:
mp-wp_genesis 439 * 'numberposts' - Default is 5. Total number of posts to retrieve.
mp-wp_genesis 440 * 'offset' - Default is 0. See {@link WP_Query::query()} for more.
mp-wp_genesis 441 * 'category' - What category to pull the posts from.
mp-wp_genesis 442 * 'orderby' - Default is 'post_date'. How to order the posts.
mp-wp_genesis 443 * 'order' - Default is 'DESC'. The order to retrieve the posts.
mp-wp_genesis 444 * 'include' - See {@link WP_Query::query()} for more.
mp-wp_genesis 445 * 'exclude' - See {@link WP_Query::query()} for more.
mp-wp_genesis 446 * 'meta_key' - See {@link WP_Query::query()} for more.
mp-wp_genesis 447 * 'meta_value' - See {@link WP_Query::query()} for more.
mp-wp_genesis 448 * 'post_type' - Default is 'post'. Can be 'page', or 'attachment' to name a few.
mp-wp_genesis 449 * 'post_parent' - The parent of the post or post type.
mp-wp_genesis 450 * 'post_status' - Default is 'published'. Post status to retrieve.
mp-wp_genesis 451 *
mp-wp_genesis 452 * @since 1.2.0
mp-wp_genesis 453 * @uses $wpdb
mp-wp_genesis 454 * @uses WP_Query::query() See for more default arguments and information.
mp-wp_genesis 455 * @link http://codex.wordpress.org/Template_Tags/get_posts
mp-wp_genesis 456 *
mp-wp_genesis 457 * @param array $args Optional. Override defaults.
mp-wp_genesis 458 * @return array List of posts.
mp-wp_genesis 459 */
mp-wp_genesis 460 function get_posts($args = null) {
mp-wp_genesis 461 $defaults = array(
mp-wp_genesis 462 'numberposts' => 5, 'offset' => 0,
mp-wp_genesis 463 'category' => 0, 'orderby' => 'post_date',
mp-wp_genesis 464 'order' => 'DESC', 'include' => '',
mp-wp_genesis 465 'exclude' => '', 'meta_key' => '',
mp-wp_genesis 466 'meta_value' =>'', 'post_type' => 'post',
mp-wp_genesis 467 'suppress_filters' => true
mp-wp_genesis 468 );
mp-wp_genesis 469
mp-wp_genesis 470 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 471 if ( empty( $r['post_status'] ) )
mp-wp_genesis 472 $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
mp-wp_genesis 473 if ( ! empty($r['numberposts']) )
mp-wp_genesis 474 $r['posts_per_page'] = $r['numberposts'];
mp-wp_genesis 475 if ( ! empty($r['category']) )
mp-wp_genesis 476 $r['cat'] = $r['category'];
mp-wp_genesis 477 if ( ! empty($r['include']) ) {
mp-wp_genesis 478 $incposts = preg_split('/[\s,]+/',$r['include']);
mp-wp_genesis 479 $r['posts_per_page'] = count($incposts); // only the number of posts included
mp-wp_genesis 480 $r['post__in'] = $incposts;
mp-wp_genesis 481 } elseif ( ! empty($r['exclude']) )
mp-wp_genesis 482 $r['post__not_in'] = preg_split('/[\s,]+/',$r['exclude']);
mp-wp_genesis 483
mp-wp_genesis 484 $r['caller_get_posts'] = true;
mp-wp_genesis 485
mp-wp_genesis 486 $get_posts = new WP_Query;
mp-wp_genesis 487 return $get_posts->query($r);
mp-wp_genesis 488
mp-wp_genesis 489 }
mp-wp_genesis 490
mp-wp_genesis 491 //
mp-wp_genesis 492 // Post meta functions
mp-wp_genesis 493 //
mp-wp_genesis 494
mp-wp_genesis 495 /**
mp-wp_genesis 496 * Add meta data field to a post.
mp-wp_genesis 497 *
mp-wp_genesis 498 * Post meta data is called "Custom Fields" on the Administration Panels.
mp-wp_genesis 499 *
mp-wp_genesis 500 * @since 1.5.0
mp-wp_genesis 501 * @uses $wpdb
mp-wp_genesis 502 * @link http://codex.wordpress.org/Function_Reference/add_post_meta
mp-wp_genesis 503 *
mp-wp_genesis 504 * @param int $post_id Post ID.
mp-wp_genesis 505 * @param string $key Metadata name.
mp-wp_genesis 506 * @param mixed $value Metadata value.
mp-wp_genesis 507 * @param bool $unique Optional, default is false. Whether the same key should not be added.
mp-wp_genesis 508 * @return bool False for failure. True for success.
mp-wp_genesis 509 */
mp-wp_genesis 510 function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) {
mp-wp_genesis 511 global $wpdb;
mp-wp_genesis 512
mp-wp_genesis 513 // make sure meta is added to the post, not a revision
mp-wp_genesis 514 if ( $the_post = wp_is_post_revision($post_id) )
mp-wp_genesis 515 $post_id = $the_post;
mp-wp_genesis 516
mp-wp_genesis 517 // expected_slashed ($meta_key)
mp-wp_genesis 518 $meta_key = stripslashes($meta_key);
mp-wp_genesis 519
mp-wp_genesis 520 if ( $unique && $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) )
mp-wp_genesis 521 return false;
mp-wp_genesis 522
mp-wp_genesis 523 $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
mp-wp_genesis 524
mp-wp_genesis 525 $wpdb->insert( $wpdb->postmeta, compact( 'post_id', 'meta_key', 'meta_value' ) );
mp-wp_genesis 526
mp-wp_genesis 527 wp_cache_delete($post_id, 'post_meta');
mp-wp_genesis 528
mp-wp_genesis 529 return true;
mp-wp_genesis 530 }
mp-wp_genesis 531
mp-wp_genesis 532 /**
mp-wp_genesis 533 * Remove metadata matching criteria from a post.
mp-wp_genesis 534 *
mp-wp_genesis 535 * You can match based on the key, or key and value. Removing based on key and
mp-wp_genesis 536 * value, will keep from removing duplicate metadata with the same key. It also
mp-wp_genesis 537 * allows removing all metadata matching key, if needed.
mp-wp_genesis 538 *
mp-wp_genesis 539 * @since 1.5.0
mp-wp_genesis 540 * @uses $wpdb
mp-wp_genesis 541 * @link http://codex.wordpress.org/Function_Reference/delete_post_meta
mp-wp_genesis 542 *
mp-wp_genesis 543 * @param int $post_id post ID
mp-wp_genesis 544 * @param string $meta_key Metadata name.
mp-wp_genesis 545 * @param mixed $meta_value Optional. Metadata value.
mp-wp_genesis 546 * @return bool False for failure. True for success.
mp-wp_genesis 547 */
mp-wp_genesis 548 function delete_post_meta($post_id, $meta_key, $meta_value = '') {
mp-wp_genesis 549 global $wpdb;
mp-wp_genesis 550
mp-wp_genesis 551 // make sure meta is added to the post, not a revision
mp-wp_genesis 552 if ( $the_post = wp_is_post_revision($post_id) )
mp-wp_genesis 553 $post_id = $the_post;
mp-wp_genesis 554
mp-wp_genesis 555 // expected_slashed ($meta_key, $meta_value)
mp-wp_genesis 556 $meta_key = stripslashes( $meta_key );
mp-wp_genesis 557 $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
mp-wp_genesis 558
mp-wp_genesis 559 if ( empty( $meta_value ) )
mp-wp_genesis 560 $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $meta_key ) );
mp-wp_genesis 561 else
mp-wp_genesis 562 $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $meta_key, $meta_value ) );
mp-wp_genesis 563
mp-wp_genesis 564 if ( !$meta_id )
mp-wp_genesis 565 return false;
mp-wp_genesis 566
mp-wp_genesis 567 if ( empty( $meta_value ) )
mp-wp_genesis 568 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $meta_key ) );
mp-wp_genesis 569 else
mp-wp_genesis 570 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $meta_key, $meta_value ) );
mp-wp_genesis 571
mp-wp_genesis 572 wp_cache_delete($post_id, 'post_meta');
mp-wp_genesis 573
mp-wp_genesis 574 return true;
mp-wp_genesis 575 }
mp-wp_genesis 576
mp-wp_genesis 577 /**
mp-wp_genesis 578 * Retrieve post meta field for a post.
mp-wp_genesis 579 *
mp-wp_genesis 580 * @since 1.5.0
mp-wp_genesis 581 * @uses $wpdb
mp-wp_genesis 582 * @link http://codex.wordpress.org/Function_Reference/get_post_meta
mp-wp_genesis 583 *
mp-wp_genesis 584 * @param int $post_id Post ID.
mp-wp_genesis 585 * @param string $key The meta key to retrieve.
mp-wp_genesis 586 * @param bool $single Whether to return a single value.
mp-wp_genesis 587 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true.
mp-wp_genesis 588 */
mp-wp_genesis 589 function get_post_meta($post_id, $key, $single = false) {
mp-wp_genesis 590 $post_id = (int) $post_id;
mp-wp_genesis 591
mp-wp_genesis 592 $meta_cache = wp_cache_get($post_id, 'post_meta');
mp-wp_genesis 593
mp-wp_genesis 594 if ( !$meta_cache ) {
mp-wp_genesis 595 update_postmeta_cache($post_id);
mp-wp_genesis 596 $meta_cache = wp_cache_get($post_id, 'post_meta');
mp-wp_genesis 597 }
mp-wp_genesis 598
mp-wp_genesis 599 if ( isset($meta_cache[$key]) ) {
mp-wp_genesis 600 if ( $single ) {
mp-wp_genesis 601 return maybe_unserialize( $meta_cache[$key][0] );
mp-wp_genesis 602 } else {
mp-wp_genesis 603 return array_map('maybe_unserialize', $meta_cache[$key]);
mp-wp_genesis 604 }
mp-wp_genesis 605 }
mp-wp_genesis 606
mp-wp_genesis 607 return '';
mp-wp_genesis 608 }
mp-wp_genesis 609
mp-wp_genesis 610 /**
mp-wp_genesis 611 * Update post meta field based on post ID.
mp-wp_genesis 612 *
mp-wp_genesis 613 * Use the $prev_value parameter to differentiate between meta fields with the
mp-wp_genesis 614 * same key and post ID.
mp-wp_genesis 615 *
mp-wp_genesis 616 * If the meta field for the post does not exist, it will be added.
mp-wp_genesis 617 *
mp-wp_genesis 618 * @since 1.5
mp-wp_genesis 619 * @uses $wpdb
mp-wp_genesis 620 * @link http://codex.wordpress.org/Function_Reference/update_post_meta
mp-wp_genesis 621 *
mp-wp_genesis 622 * @param int $post_id Post ID.
mp-wp_genesis 623 * @param string $key Metadata key.
mp-wp_genesis 624 * @param mixed $value Metadata value.
mp-wp_genesis 625 * @param mixed $prev_value Optional. Previous value to check before removing.
mp-wp_genesis 626 * @return bool False on failure, true if success.
mp-wp_genesis 627 */
mp-wp_genesis 628 function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') {
mp-wp_genesis 629 global $wpdb;
mp-wp_genesis 630
mp-wp_genesis 631 // make sure meta is added to the post, not a revision
mp-wp_genesis 632 if ( $the_post = wp_is_post_revision($post_id) )
mp-wp_genesis 633 $post_id = $the_post;
mp-wp_genesis 634
mp-wp_genesis 635 // expected_slashed ($meta_key)
mp-wp_genesis 636 $meta_key = stripslashes($meta_key);
mp-wp_genesis 637
mp-wp_genesis 638 if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) ) {
mp-wp_genesis 639 return add_post_meta($post_id, $meta_key, $meta_value);
mp-wp_genesis 640 }
mp-wp_genesis 641
mp-wp_genesis 642 $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
mp-wp_genesis 643
mp-wp_genesis 644 $data = compact( 'meta_value' );
mp-wp_genesis 645 $where = compact( 'meta_key', 'post_id' );
mp-wp_genesis 646
mp-wp_genesis 647 if ( !empty( $prev_value ) ) {
mp-wp_genesis 648 $prev_value = maybe_serialize($prev_value);
mp-wp_genesis 649 $where['meta_value'] = $prev_value;
mp-wp_genesis 650 }
mp-wp_genesis 651
mp-wp_genesis 652 $wpdb->update( $wpdb->postmeta, $data, $where );
mp-wp_genesis 653 wp_cache_delete($post_id, 'post_meta');
mp-wp_genesis 654 return true;
mp-wp_genesis 655 }
mp-wp_genesis 656
mp-wp_genesis 657 /**
mp-wp_genesis 658 * Delete everything from post meta matching meta key.
mp-wp_genesis 659 *
mp-wp_genesis 660 * @since 2.3.0
mp-wp_genesis 661 * @uses $wpdb
mp-wp_genesis 662 *
mp-wp_genesis 663 * @param string $post_meta_key Key to search for when deleting.
mp-wp_genesis 664 * @return bool Whether the post meta key was deleted from the database
mp-wp_genesis 665 */
mp-wp_genesis 666 function delete_post_meta_by_key($post_meta_key) {
mp-wp_genesis 667 global $wpdb;
mp-wp_genesis 668 if ( $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key)) ) {
mp-wp_genesis 669 /** @todo Get post_ids and delete cache */
mp-wp_genesis 670 // wp_cache_delete($post_id, 'post_meta');
mp-wp_genesis 671 return true;
mp-wp_genesis 672 }
mp-wp_genesis 673 return false;
mp-wp_genesis 674 }
mp-wp_genesis 675
mp-wp_genesis 676 /**
mp-wp_genesis 677 * Retrieve post meta fields, based on post ID.
mp-wp_genesis 678 *
mp-wp_genesis 679 * The post meta fields are retrieved from the cache, so the function is
mp-wp_genesis 680 * optimized to be called more than once. It also applies to the functions, that
mp-wp_genesis 681 * use this function.
mp-wp_genesis 682 *
mp-wp_genesis 683 * @since 1.2.0
mp-wp_genesis 684 * @link http://codex.wordpress.org/Function_Reference/get_post_custom
mp-wp_genesis 685 *
mp-wp_genesis 686 * @uses $id Current Loop Post ID
mp-wp_genesis 687 *
mp-wp_genesis 688 * @param int $post_id post ID
mp-wp_genesis 689 * @return array
mp-wp_genesis 690 */
mp-wp_genesis 691 function get_post_custom($post_id = 0) {
mp-wp_genesis 692 global $id;
mp-wp_genesis 693
mp-wp_genesis 694 if ( !$post_id )
mp-wp_genesis 695 $post_id = (int) $id;
mp-wp_genesis 696
mp-wp_genesis 697 $post_id = (int) $post_id;
mp-wp_genesis 698
mp-wp_genesis 699 if ( ! wp_cache_get($post_id, 'post_meta') )
mp-wp_genesis 700 update_postmeta_cache($post_id);
mp-wp_genesis 701
mp-wp_genesis 702 return wp_cache_get($post_id, 'post_meta');
mp-wp_genesis 703 }
mp-wp_genesis 704
mp-wp_genesis 705 /**
mp-wp_genesis 706 * Retrieve meta field names for a post.
mp-wp_genesis 707 *
mp-wp_genesis 708 * If there are no meta fields, then nothing (null) will be returned.
mp-wp_genesis 709 *
mp-wp_genesis 710 * @since 1.2.0
mp-wp_genesis 711 * @link http://codex.wordpress.org/Function_Reference/get_post_custom_keys
mp-wp_genesis 712 *
mp-wp_genesis 713 * @param int $post_id post ID
mp-wp_genesis 714 * @return array|null Either array of the keys, or null if keys could not be retrieved.
mp-wp_genesis 715 */
mp-wp_genesis 716 function get_post_custom_keys( $post_id = 0 ) {
mp-wp_genesis 717 $custom = get_post_custom( $post_id );
mp-wp_genesis 718
mp-wp_genesis 719 if ( !is_array($custom) )
mp-wp_genesis 720 return;
mp-wp_genesis 721
mp-wp_genesis 722 if ( $keys = array_keys($custom) )
mp-wp_genesis 723 return $keys;
mp-wp_genesis 724 }
mp-wp_genesis 725
mp-wp_genesis 726 /**
mp-wp_genesis 727 * Retrieve values for a custom post field.
mp-wp_genesis 728 *
mp-wp_genesis 729 * The parameters must not be considered optional. All of the post meta fields
mp-wp_genesis 730 * will be retrieved and only the meta field key values returned.
mp-wp_genesis 731 *
mp-wp_genesis 732 * @since 1.2.0
mp-wp_genesis 733 * @link http://codex.wordpress.org/Function_Reference/get_post_custom_values
mp-wp_genesis 734 *
mp-wp_genesis 735 * @param string $key Meta field key.
mp-wp_genesis 736 * @param int $post_id Post ID
mp-wp_genesis 737 * @return array Meta field values.
mp-wp_genesis 738 */
mp-wp_genesis 739 function get_post_custom_values( $key = '', $post_id = 0 ) {
mp-wp_genesis 740 $custom = get_post_custom($post_id);
mp-wp_genesis 741
mp-wp_genesis 742 return $custom[$key];
mp-wp_genesis 743 }
mp-wp_genesis 744
mp-wp_genesis 745 /**
mp-wp_genesis 746 * Check if post is sticky.
mp-wp_genesis 747 *
mp-wp_genesis 748 * Sticky posts should remain at the top of The Loop. If the post ID is not
mp-wp_genesis 749 * given, then The Loop ID for the current post will be used.
mp-wp_genesis 750 *
mp-wp_genesis 751 * @since 2.7.0
mp-wp_genesis 752 *
mp-wp_genesis 753 * @param int $post_id Optional. Post ID.
mp-wp_genesis 754 * @return bool Whether post is sticky (true) or not sticky (false).
mp-wp_genesis 755 */
mp-wp_genesis 756 function is_sticky($post_id = null) {
mp-wp_genesis 757 global $id;
mp-wp_genesis 758
mp-wp_genesis 759 $post_id = absint($post_id);
mp-wp_genesis 760
mp-wp_genesis 761 if ( !$post_id )
mp-wp_genesis 762 $post_id = absint($id);
mp-wp_genesis 763
mp-wp_genesis 764 $stickies = get_option('sticky_posts');
mp-wp_genesis 765
mp-wp_genesis 766 if ( !is_array($stickies) )
mp-wp_genesis 767 return false;
mp-wp_genesis 768
mp-wp_genesis 769 if ( in_array($post_id, $stickies) )
mp-wp_genesis 770 return true;
mp-wp_genesis 771
mp-wp_genesis 772 return false;
mp-wp_genesis 773 }
mp-wp_genesis 774
mp-wp_genesis 775 /**
mp-wp_genesis 776 * Sanitize every post field.
mp-wp_genesis 777 *
mp-wp_genesis 778 * If the context is 'raw', then the post object or array will just be returned.
mp-wp_genesis 779 *
mp-wp_genesis 780 * @since 2.3.0
mp-wp_genesis 781 * @uses sanitize_post_field() Used to sanitize the fields.
mp-wp_genesis 782 *
mp-wp_genesis 783 * @param object|array $post The Post Object or Array
mp-wp_genesis 784 * @param string $context Optional, default is 'display'. How to sanitize post fields.
mp-wp_genesis 785 * @return object|array The now sanitized Post Object or Array (will be the same type as $post)
mp-wp_genesis 786 */
mp-wp_genesis 787 function sanitize_post($post, $context = 'display') {
mp-wp_genesis 788 if ( 'raw' == $context )
mp-wp_genesis 789 return $post;
mp-wp_genesis 790 if ( is_object($post) ) {
mp-wp_genesis 791 if ( !isset($post->ID) )
mp-wp_genesis 792 $post->ID = 0;
mp-wp_genesis 793 foreach ( array_keys(get_object_vars($post)) as $field )
mp-wp_genesis 794 $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
mp-wp_genesis 795 } else {
mp-wp_genesis 796 if ( !isset($post['ID']) )
mp-wp_genesis 797 $post['ID'] = 0;
mp-wp_genesis 798 foreach ( array_keys($post) as $field )
mp-wp_genesis 799 $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
mp-wp_genesis 800 }
mp-wp_genesis 801 return $post;
mp-wp_genesis 802 }
mp-wp_genesis 803
mp-wp_genesis 804 /**
mp-wp_genesis 805 * Sanitize post field based on context.
mp-wp_genesis 806 *
mp-wp_genesis 807 * Possible context values are: raw, edit, db, attribute, js, and display. The
mp-wp_genesis 808 * display context is used by default.
mp-wp_genesis 809 *
mp-wp_genesis 810 * @since 2.3.0
mp-wp_genesis 811 *
mp-wp_genesis 812 * @param string $field The Post Object field name.
mp-wp_genesis 813 * @param mixed $value The Post Object value.
mp-wp_genesis 814 * @param int $post_id Post ID.
mp-wp_genesis 815 * @param string $context How to sanitize post fields.
mp-wp_genesis 816 * @return mixed Sanitized value.
mp-wp_genesis 817 */
mp-wp_genesis 818 function sanitize_post_field($field, $value, $post_id, $context) {
mp-wp_genesis 819 $int_fields = array('ID', 'post_parent', 'menu_order');
mp-wp_genesis 820 if ( in_array($field, $int_fields) )
mp-wp_genesis 821 $value = (int) $value;
mp-wp_genesis 822
mp-wp_genesis 823 if ( 'raw' == $context )
mp-wp_genesis 824 return $value;
mp-wp_genesis 825
mp-wp_genesis 826 $prefixed = false;
mp-wp_genesis 827 if ( false !== strpos($field, 'post_') ) {
mp-wp_genesis 828 $prefixed = true;
mp-wp_genesis 829 $field_no_prefix = str_replace('post_', '', $field);
mp-wp_genesis 830 }
mp-wp_genesis 831
mp-wp_genesis 832 if ( 'edit' == $context ) {
mp-wp_genesis 833 $format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password');
mp-wp_genesis 834
mp-wp_genesis 835 if ( $prefixed ) {
mp-wp_genesis 836 $value = apply_filters("edit_$field", $value, $post_id);
mp-wp_genesis 837 // Old school
mp-wp_genesis 838 $value = apply_filters("${field_no_prefix}_edit_pre", $value, $post_id);
mp-wp_genesis 839 } else {
mp-wp_genesis 840 $value = apply_filters("edit_post_$field", $value, $post_id);
mp-wp_genesis 841 }
mp-wp_genesis 842
mp-wp_genesis 843 if ( in_array($field, $format_to_edit) ) {
mp-wp_genesis 844 if ( 'post_content' == $field )
mp-wp_genesis 845 $value = format_to_edit($value, user_can_richedit());
mp-wp_genesis 846 else
mp-wp_genesis 847 $value = format_to_edit($value);
mp-wp_genesis 848 } else {
mp-wp_genesis 849 $value = attribute_escape($value);
mp-wp_genesis 850 }
mp-wp_genesis 851 } else if ( 'db' == $context ) {
mp-wp_genesis 852 if ( $prefixed ) {
mp-wp_genesis 853 $value = apply_filters("pre_$field", $value);
mp-wp_genesis 854 $value = apply_filters("${field_no_prefix}_save_pre", $value);
mp-wp_genesis 855 } else {
mp-wp_genesis 856 $value = apply_filters("pre_post_$field", $value);
mp-wp_genesis 857 $value = apply_filters("${field}_pre", $value);
mp-wp_genesis 858 }
mp-wp_genesis 859 } else {
mp-wp_genesis 860 // Use display filters by default.
mp-wp_genesis 861 if ( $prefixed )
mp-wp_genesis 862 $value = apply_filters($field, $value, $post_id, $context);
mp-wp_genesis 863 else
mp-wp_genesis 864 $value = apply_filters("post_$field", $value, $post_id, $context);
mp-wp_genesis 865 }
mp-wp_genesis 866
mp-wp_genesis 867 if ( 'attribute' == $context )
mp-wp_genesis 868 $value = attribute_escape($value);
mp-wp_genesis 869 else if ( 'js' == $context )
mp-wp_genesis 870 $value = js_escape($value);
mp-wp_genesis 871
mp-wp_genesis 872 return $value;
mp-wp_genesis 873 }
mp-wp_genesis 874
mp-wp_genesis 875 /**
mp-wp_genesis 876 * Make a post sticky.
mp-wp_genesis 877 *
mp-wp_genesis 878 * Sticky posts should be displayed at the top of the front page.
mp-wp_genesis 879 *
mp-wp_genesis 880 * @since 2.7.0
mp-wp_genesis 881 *
mp-wp_genesis 882 * @param int $post_id Post ID.
mp-wp_genesis 883 */
mp-wp_genesis 884 function stick_post($post_id) {
mp-wp_genesis 885 $stickies = get_option('sticky_posts');
mp-wp_genesis 886
mp-wp_genesis 887 if ( !is_array($stickies) )
mp-wp_genesis 888 $stickies = array($post_id);
mp-wp_genesis 889
mp-wp_genesis 890 if ( ! in_array($post_id, $stickies) )
mp-wp_genesis 891 $stickies[] = $post_id;
mp-wp_genesis 892
mp-wp_genesis 893 update_option('sticky_posts', $stickies);
mp-wp_genesis 894 }
mp-wp_genesis 895
mp-wp_genesis 896 /**
mp-wp_genesis 897 * Unstick a post.
mp-wp_genesis 898 *
mp-wp_genesis 899 * Sticky posts should be displayed at the top of the front page.
mp-wp_genesis 900 *
mp-wp_genesis 901 * @since 2.7.0
mp-wp_genesis 902 *
mp-wp_genesis 903 * @param int $post_id Post ID.
mp-wp_genesis 904 */
mp-wp_genesis 905 function unstick_post($post_id) {
mp-wp_genesis 906 $stickies = get_option('sticky_posts');
mp-wp_genesis 907
mp-wp_genesis 908 if ( !is_array($stickies) )
mp-wp_genesis 909 return;
mp-wp_genesis 910
mp-wp_genesis 911 if ( ! in_array($post_id, $stickies) )
mp-wp_genesis 912 return;
mp-wp_genesis 913
mp-wp_genesis 914 $offset = array_search($post_id, $stickies);
mp-wp_genesis 915 if ( false === $offset )
mp-wp_genesis 916 return;
mp-wp_genesis 917
mp-wp_genesis 918 array_splice($stickies, $offset, 1);
mp-wp_genesis 919
mp-wp_genesis 920 update_option('sticky_posts', $stickies);
mp-wp_genesis 921 }
mp-wp_genesis 922
mp-wp_genesis 923 /**
mp-wp_genesis 924 * Count number of posts of a post type and is user has permissions to view.
mp-wp_genesis 925 *
mp-wp_genesis 926 * This function provides an efficient method of finding the amount of post's
mp-wp_genesis 927 * type a blog has. Another method is to count the amount of items in
mp-wp_genesis 928 * get_posts(), but that method has a lot of overhead with doing so. Therefore,
mp-wp_genesis 929 * when developing for 2.5+, use this function instead.
mp-wp_genesis 930 *
mp-wp_genesis 931 * The $perm parameter checks for 'readable' value and if the user can read
mp-wp_genesis 932 * private posts, it will display that for the user that is signed in.
mp-wp_genesis 933 *
mp-wp_genesis 934 * @since 2.5.0
mp-wp_genesis 935 * @link http://codex.wordpress.org/Template_Tags/wp_count_posts
mp-wp_genesis 936 *
mp-wp_genesis 937 * @param string $type Optional. Post type to retrieve count
mp-wp_genesis 938 * @param string $perm Optional. 'readable' or empty.
mp-wp_genesis 939 * @return object Number of posts for each status
mp-wp_genesis 940 */
mp-wp_genesis 941 function wp_count_posts( $type = 'post', $perm = '' ) {
mp-wp_genesis 942 global $wpdb;
mp-wp_genesis 943
mp-wp_genesis 944 $user = wp_get_current_user();
mp-wp_genesis 945
mp-wp_genesis 946 $cache_key = $type;
mp-wp_genesis 947
mp-wp_genesis 948 $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
mp-wp_genesis 949 if ( 'readable' == $perm && is_user_logged_in() ) {
mp-wp_genesis 950 if ( !current_user_can("read_private_{$type}s") ) {
mp-wp_genesis 951 $cache_key .= '_' . $perm . '_' . $user->ID;
mp-wp_genesis 952 $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
mp-wp_genesis 953 }
mp-wp_genesis 954 }
mp-wp_genesis 955 $query .= ' GROUP BY post_status';
mp-wp_genesis 956
mp-wp_genesis 957 $count = wp_cache_get($cache_key, 'counts');
mp-wp_genesis 958 if ( false !== $count )
mp-wp_genesis 959 return $count;
mp-wp_genesis 960
mp-wp_genesis 961 $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
mp-wp_genesis 962
mp-wp_genesis 963 $stats = array( );
mp-wp_genesis 964 foreach( (array) $count as $row_num => $row ) {
mp-wp_genesis 965 $stats[$row['post_status']] = $row['num_posts'];
mp-wp_genesis 966 }
mp-wp_genesis 967
mp-wp_genesis 968 $stats = (object) $stats;
mp-wp_genesis 969 wp_cache_set($cache_key, $stats, 'counts');
mp-wp_genesis 970
mp-wp_genesis 971 return $stats;
mp-wp_genesis 972 }
mp-wp_genesis 973
mp-wp_genesis 974
mp-wp_genesis 975 /**
mp-wp_genesis 976 * Count number of attachments for the mime type(s).
mp-wp_genesis 977 *
mp-wp_genesis 978 * If you set the optional mime_type parameter, then an array will still be
mp-wp_genesis 979 * returned, but will only have the item you are looking for. It does not give
mp-wp_genesis 980 * you the number of attachments that are children of a post. You can get that
mp-wp_genesis 981 * by counting the number of children that post has.
mp-wp_genesis 982 *
mp-wp_genesis 983 * @since 2.5.0
mp-wp_genesis 984 *
mp-wp_genesis 985 * @param string|array $mime_type Optional. Array or comma-separated list of MIME patterns.
mp-wp_genesis 986 * @return array Number of posts for each mime type.
mp-wp_genesis 987 */
mp-wp_genesis 988 function wp_count_attachments( $mime_type = '' ) {
mp-wp_genesis 989 global $wpdb;
mp-wp_genesis 990
mp-wp_genesis 991 $and = wp_post_mime_type_where( $mime_type );
mp-wp_genesis 992 $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' $and GROUP BY post_mime_type", ARRAY_A );
mp-wp_genesis 993
mp-wp_genesis 994 $stats = array( );
mp-wp_genesis 995 foreach( (array) $count as $row ) {
mp-wp_genesis 996 $stats[$row['post_mime_type']] = $row['num_posts'];
mp-wp_genesis 997 }
mp-wp_genesis 998
mp-wp_genesis 999 return (object) $stats;
mp-wp_genesis 1000 }
mp-wp_genesis 1001
mp-wp_genesis 1002 /**
mp-wp_genesis 1003 * Check a MIME-Type against a list.
mp-wp_genesis 1004 *
mp-wp_genesis 1005 * If the wildcard_mime_types parameter is a string, it must be comma separated
mp-wp_genesis 1006 * list. If the real_mime_types is a string, it is also comma separated to
mp-wp_genesis 1007 * create the list.
mp-wp_genesis 1008 *
mp-wp_genesis 1009 * @since 2.5.0
mp-wp_genesis 1010 *
mp-wp_genesis 1011 * @param string|array $wildcard_mime_types e.g. audio/mpeg or image (same as image/*) or flash (same as *flash*)
mp-wp_genesis 1012 * @param string|array $real_mime_types post_mime_type values
mp-wp_genesis 1013 * @return array array(wildcard=>array(real types))
mp-wp_genesis 1014 */
mp-wp_genesis 1015 function wp_match_mime_types($wildcard_mime_types, $real_mime_types) {
mp-wp_genesis 1016 $matches = array();
mp-wp_genesis 1017 if ( is_string($wildcard_mime_types) )
mp-wp_genesis 1018 $wildcard_mime_types = array_map('trim', explode(',', $wildcard_mime_types));
mp-wp_genesis 1019 if ( is_string($real_mime_types) )
mp-wp_genesis 1020 $real_mime_types = array_map('trim', explode(',', $real_mime_types));
mp-wp_genesis 1021 $wild = '[-._a-z0-9]*';
mp-wp_genesis 1022 foreach ( (array) $wildcard_mime_types as $type ) {
mp-wp_genesis 1023 $type = str_replace('*', $wild, $type);
mp-wp_genesis 1024 $patternses[1][$type] = "^$type$";
mp-wp_genesis 1025 if ( false === strpos($type, '/') ) {
mp-wp_genesis 1026 $patternses[2][$type] = "^$type/";
mp-wp_genesis 1027 $patternses[3][$type] = $type;
mp-wp_genesis 1028 }
mp-wp_genesis 1029 }
mp-wp_genesis 1030 asort($patternses);
mp-wp_genesis 1031 foreach ( $patternses as $patterns )
mp-wp_genesis 1032 foreach ( $patterns as $type => $pattern )
mp-wp_genesis 1033 foreach ( (array) $real_mime_types as $real )
mp-wp_genesis 1034 if ( preg_match("#$pattern#", $real) && ( empty($matches[$type]) || false === array_search($real, $matches[$type]) ) )
mp-wp_genesis 1035 $matches[$type][] = $real;
mp-wp_genesis 1036 return $matches;
mp-wp_genesis 1037 }
mp-wp_genesis 1038
mp-wp_genesis 1039 /**
mp-wp_genesis 1040 * Convert MIME types into SQL.
mp-wp_genesis 1041 *
mp-wp_genesis 1042 * @since 2.5.0
mp-wp_genesis 1043 *
mp-wp_genesis 1044 * @param string|array $mime_types List of mime types or comma separated string of mime types.
mp-wp_genesis 1045 * @return string The SQL AND clause for mime searching.
mp-wp_genesis 1046 */
mp-wp_genesis 1047 function wp_post_mime_type_where($post_mime_types) {
mp-wp_genesis 1048 $where = '';
mp-wp_genesis 1049 $wildcards = array('', '%', '%/%');
mp-wp_genesis 1050 if ( is_string($post_mime_types) )
mp-wp_genesis 1051 $post_mime_types = array_map('trim', explode(',', $post_mime_types));
mp-wp_genesis 1052 foreach ( (array) $post_mime_types as $mime_type ) {
mp-wp_genesis 1053 $mime_type = preg_replace('/\s/', '', $mime_type);
mp-wp_genesis 1054 $slashpos = strpos($mime_type, '/');
mp-wp_genesis 1055 if ( false !== $slashpos ) {
mp-wp_genesis 1056 $mime_group = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, 0, $slashpos));
mp-wp_genesis 1057 $mime_subgroup = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, $slashpos + 1));
mp-wp_genesis 1058 if ( empty($mime_subgroup) )
mp-wp_genesis 1059 $mime_subgroup = '*';
mp-wp_genesis 1060 else
mp-wp_genesis 1061 $mime_subgroup = str_replace('/', '', $mime_subgroup);
mp-wp_genesis 1062 $mime_pattern = "$mime_group/$mime_subgroup";
mp-wp_genesis 1063 } else {
mp-wp_genesis 1064 $mime_pattern = preg_replace('/[^-*.a-zA-Z0-9]/', '', $mime_type);
mp-wp_genesis 1065 if ( false === strpos($mime_pattern, '*') )
mp-wp_genesis 1066 $mime_pattern .= '/*';
mp-wp_genesis 1067 }
mp-wp_genesis 1068
mp-wp_genesis 1069 $mime_pattern = preg_replace('/\*+/', '%', $mime_pattern);
mp-wp_genesis 1070
mp-wp_genesis 1071 if ( in_array( $mime_type, $wildcards ) )
mp-wp_genesis 1072 return '';
mp-wp_genesis 1073
mp-wp_genesis 1074 if ( false !== strpos($mime_pattern, '%') )
mp-wp_genesis 1075 $wheres[] = "post_mime_type LIKE '$mime_pattern'";
mp-wp_genesis 1076 else
mp-wp_genesis 1077 $wheres[] = "post_mime_type = '$mime_pattern'";
mp-wp_genesis 1078 }
mp-wp_genesis 1079 if ( !empty($wheres) )
mp-wp_genesis 1080 $where = ' AND (' . join(' OR ', $wheres) . ') ';
mp-wp_genesis 1081 return $where;
mp-wp_genesis 1082 }
mp-wp_genesis 1083
mp-wp_genesis 1084 /**
mp-wp_genesis 1085 * Removes a post, attachment, or page.
mp-wp_genesis 1086 *
mp-wp_genesis 1087 * When the post and page goes, everything that is tied to it is deleted also.
mp-wp_genesis 1088 * This includes comments, post meta fields, and terms associated with the post.
mp-wp_genesis 1089 *
mp-wp_genesis 1090 * @since 1.0.0
mp-wp_genesis 1091 * @uses do_action() Calls 'deleted_post' hook on post ID.
mp-wp_genesis 1092 *
mp-wp_genesis 1093 * @param int $postid Post ID.
mp-wp_genesis 1094 * @return mixed
mp-wp_genesis 1095 */
mp-wp_genesis 1096 function wp_delete_post($postid = 0) {
mp-wp_genesis 1097 global $wpdb, $wp_rewrite;
mp-wp_genesis 1098
mp-wp_genesis 1099 if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) )
mp-wp_genesis 1100 return $post;
mp-wp_genesis 1101
mp-wp_genesis 1102 if ( 'attachment' == $post->post_type )
mp-wp_genesis 1103 return wp_delete_attachment($postid);
mp-wp_genesis 1104
mp-wp_genesis 1105 do_action('delete_post', $postid);
mp-wp_genesis 1106
mp-wp_genesis 1107 /** @todo delete for pluggable post taxonomies too */
mp-wp_genesis 1108 wp_delete_object_term_relationships($postid, array('category', 'post_tag'));
mp-wp_genesis 1109
mp-wp_genesis 1110 $parent_data = array( 'post_parent' => $post->post_parent );
mp-wp_genesis 1111 $parent_where = array( 'post_parent' => $postid );
mp-wp_genesis 1112
mp-wp_genesis 1113 if ( 'page' == $post->post_type) {
mp-wp_genesis 1114 // if the page is defined in option page_on_front or post_for_posts,
mp-wp_genesis 1115 // adjust the corresponding options
mp-wp_genesis 1116 if ( get_option('page_on_front') == $postid ) {
mp-wp_genesis 1117 update_option('show_on_front', 'posts');
mp-wp_genesis 1118 delete_option('page_on_front');
mp-wp_genesis 1119 }
mp-wp_genesis 1120 if ( get_option('page_for_posts') == $postid ) {
mp-wp_genesis 1121 delete_option('page_for_posts');
mp-wp_genesis 1122 }
mp-wp_genesis 1123
mp-wp_genesis 1124 // Point children of this page to its parent, also clean the cache of affected children
mp-wp_genesis 1125 $children_query = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type='page'", $postid);
mp-wp_genesis 1126 $children = $wpdb->get_results($children_query);
mp-wp_genesis 1127
mp-wp_genesis 1128 $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'page' ) );
mp-wp_genesis 1129 }
mp-wp_genesis 1130
mp-wp_genesis 1131 // Do raw query. wp_get_post_revisions() is filtered
mp-wp_genesis 1132 $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) );
mp-wp_genesis 1133 // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up.
mp-wp_genesis 1134 foreach ( $revision_ids as $revision_id )
mp-wp_genesis 1135 wp_delete_post_revision( $revision_id );
mp-wp_genesis 1136
mp-wp_genesis 1137 // Point all attachments to this post up one level
mp-wp_genesis 1138 $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );
mp-wp_genesis 1139
mp-wp_genesis 1140 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid ));
mp-wp_genesis 1141
mp-wp_genesis 1142 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ));
mp-wp_genesis 1143
mp-wp_genesis 1144 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d", $postid ));
mp-wp_genesis 1145
mp-wp_genesis 1146 if ( 'page' == $post->post_type ) {
mp-wp_genesis 1147 clean_page_cache($postid);
mp-wp_genesis 1148
mp-wp_genesis 1149 foreach ( (array) $children as $child )
mp-wp_genesis 1150 clean_page_cache($child->ID);
mp-wp_genesis 1151
mp-wp_genesis 1152 $wp_rewrite->flush_rules();
mp-wp_genesis 1153 } else {
mp-wp_genesis 1154 clean_post_cache($postid);
mp-wp_genesis 1155 }
mp-wp_genesis 1156
mp-wp_genesis 1157 do_action('deleted_post', $postid);
mp-wp_genesis 1158
mp-wp_genesis 1159 return $post;
mp-wp_genesis 1160 }
mp-wp_genesis 1161
mp-wp_genesis 1162 /**
mp-wp_genesis 1163 * Retrieve the list of categories for a post.
mp-wp_genesis 1164 *
mp-wp_genesis 1165 * Compatibility layer for themes and plugins. Also an easy layer of abstraction
mp-wp_genesis 1166 * away from the complexity of the taxonomy layer.
mp-wp_genesis 1167 *
mp-wp_genesis 1168 * @since 2.1.0
mp-wp_genesis 1169 *
mp-wp_genesis 1170 * @uses wp_get_object_terms() Retrieves the categories. Args details can be found here.
mp-wp_genesis 1171 *
mp-wp_genesis 1172 * @param int $post_id Optional. The Post ID.
mp-wp_genesis 1173 * @param array $args Optional. Overwrite the defaults.
mp-wp_genesis 1174 * @return array
mp-wp_genesis 1175 */
mp-wp_genesis 1176 function wp_get_post_categories( $post_id = 0, $args = array() ) {
mp-wp_genesis 1177 $post_id = (int) $post_id;
mp-wp_genesis 1178
mp-wp_genesis 1179 $defaults = array('fields' => 'ids');
mp-wp_genesis 1180 $args = wp_parse_args( $args, $defaults );
mp-wp_genesis 1181
mp-wp_genesis 1182 $cats = wp_get_object_terms($post_id, 'category', $args);
mp-wp_genesis 1183 return $cats;
mp-wp_genesis 1184 }
mp-wp_genesis 1185
mp-wp_genesis 1186 /**
mp-wp_genesis 1187 * Retrieve the tags for a post.
mp-wp_genesis 1188 *
mp-wp_genesis 1189 * There is only one default for this function, called 'fields' and by default
mp-wp_genesis 1190 * is set to 'all'. There are other defaults that can be override in
mp-wp_genesis 1191 * {@link wp_get_object_terms()}.
mp-wp_genesis 1192 *
mp-wp_genesis 1193 * @package WordPress
mp-wp_genesis 1194 * @subpackage Post
mp-wp_genesis 1195 * @since 2.3.0
mp-wp_genesis 1196 *
mp-wp_genesis 1197 * @uses wp_get_object_terms() Gets the tags for returning. Args can be found here
mp-wp_genesis 1198 *
mp-wp_genesis 1199 * @param int $post_id Optional. The Post ID
mp-wp_genesis 1200 * @param array $args Optional. Overwrite the defaults
mp-wp_genesis 1201 * @return array List of post tags.
mp-wp_genesis 1202 */
mp-wp_genesis 1203 function wp_get_post_tags( $post_id = 0, $args = array() ) {
mp-wp_genesis 1204 $post_id = (int) $post_id;
mp-wp_genesis 1205
mp-wp_genesis 1206 $defaults = array('fields' => 'all');
mp-wp_genesis 1207 $args = wp_parse_args( $args, $defaults );
mp-wp_genesis 1208
mp-wp_genesis 1209 $tags = wp_get_object_terms($post_id, 'post_tag', $args);
mp-wp_genesis 1210
mp-wp_genesis 1211 return $tags;
mp-wp_genesis 1212 }
mp-wp_genesis 1213
mp-wp_genesis 1214 /**
mp-wp_genesis 1215 * Retrieve number of recent posts.
mp-wp_genesis 1216 *
mp-wp_genesis 1217 * @since 1.0.0
mp-wp_genesis 1218 * @uses $wpdb
mp-wp_genesis 1219 *
mp-wp_genesis 1220 * @param int $num Optional, default is 10. Number of posts to get.
mp-wp_genesis 1221 * @return array List of posts.
mp-wp_genesis 1222 */
mp-wp_genesis 1223 function wp_get_recent_posts($num = 10) {
mp-wp_genesis 1224 global $wpdb;
mp-wp_genesis 1225
mp-wp_genesis 1226 // Set the limit clause, if we got a limit
mp-wp_genesis 1227 $num = (int) $num;
mp-wp_genesis 1228 if ($num) {
mp-wp_genesis 1229 $limit = "LIMIT $num";
mp-wp_genesis 1230 }
mp-wp_genesis 1231
mp-wp_genesis 1232 $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC $limit";
mp-wp_genesis 1233 $result = $wpdb->get_results($sql,ARRAY_A);
mp-wp_genesis 1234
mp-wp_genesis 1235 return $result ? $result : array();
mp-wp_genesis 1236 }
mp-wp_genesis 1237
mp-wp_genesis 1238 /**
mp-wp_genesis 1239 * Retrieve a single post, based on post ID.
mp-wp_genesis 1240 *
mp-wp_genesis 1241 * Has categories in 'post_category' property or key. Has tags in 'tags_input'
mp-wp_genesis 1242 * property or key.
mp-wp_genesis 1243 *
mp-wp_genesis 1244 * @since 1.0.0
mp-wp_genesis 1245 *
mp-wp_genesis 1246 * @param int $postid Post ID.
mp-wp_genesis 1247 * @param string $mode How to return result, either OBJECT, ARRAY_N, or ARRAY_A.
mp-wp_genesis 1248 * @return object|array Post object or array holding post contents and information
mp-wp_genesis 1249 */
mp-wp_genesis 1250 function wp_get_single_post($postid = 0, $mode = OBJECT) {
mp-wp_genesis 1251 $postid = (int) $postid;
mp-wp_genesis 1252
mp-wp_genesis 1253 $post = get_post($postid, $mode);
mp-wp_genesis 1254
mp-wp_genesis 1255 // Set categories and tags
mp-wp_genesis 1256 if($mode == OBJECT) {
mp-wp_genesis 1257 $post->post_category = wp_get_post_categories($postid);
mp-wp_genesis 1258 $post->tags_input = wp_get_post_tags($postid, array('fields' => 'names'));
mp-wp_genesis 1259 }
mp-wp_genesis 1260 else {
mp-wp_genesis 1261 $post['post_category'] = wp_get_post_categories($postid);
mp-wp_genesis 1262 $post['tags_input'] = wp_get_post_tags($postid, array('fields' => 'names'));
mp-wp_genesis 1263 }
mp-wp_genesis 1264
mp-wp_genesis 1265 return $post;
mp-wp_genesis 1266 }
mp-wp_genesis 1267
mp-wp_genesis 1268 /**
mp-wp_genesis 1269 * Insert a post.
mp-wp_genesis 1270 *
mp-wp_genesis 1271 * If the $postarr parameter has 'ID' set to a value, then post will be updated.
mp-wp_genesis 1272 *
mp-wp_genesis 1273 * You can set the post date manually, but setting the values for 'post_date'
mp-wp_genesis 1274 * and 'post_date_gmt' keys. You can close the comments or open the comments by
mp-wp_genesis 1275 * setting the value for 'comment_status' key.
mp-wp_genesis 1276 *
mp-wp_genesis 1277 * The defaults for the parameter $postarr are:
mp-wp_genesis 1278 * 'post_status' - Default is 'draft'.
mp-wp_genesis 1279 * 'post_type' - Default is 'post'.
mp-wp_genesis 1280 * 'post_author' - Default is current user ID. The ID of the user, who added
mp-wp_genesis 1281 * the post.
mp-wp_genesis 1282 * 'ping_status' - Default is the value in default ping status option.
mp-wp_genesis 1283 * Whether the attachment can accept pings.
mp-wp_genesis 1284 * 'post_parent' - Default is 0. Set this for the post it belongs to, if
mp-wp_genesis 1285 * any.
mp-wp_genesis 1286 * 'menu_order' - Default is 0. The order it is displayed.
mp-wp_genesis 1287 * 'to_ping' - Whether to ping.
mp-wp_genesis 1288 * 'pinged' - Default is empty string.
mp-wp_genesis 1289 * 'post_password' - Default is empty string. The password to access the
mp-wp_genesis 1290 * attachment.
mp-wp_genesis 1291 * 'guid' - Global Unique ID for referencing the attachment.
mp-wp_genesis 1292 * 'post_content_filtered' - Post content filtered.
mp-wp_genesis 1293 * 'post_excerpt' - Post excerpt.
mp-wp_genesis 1294 *
mp-wp_genesis 1295 * @since 1.0.0
mp-wp_genesis 1296 * @uses $wpdb
mp-wp_genesis 1297 * @uses $wp_rewrite
mp-wp_genesis 1298 * @uses $user_ID
mp-wp_genesis 1299 *
mp-wp_genesis 1300 * @param array $postarr Optional. Override defaults.
mp-wp_genesis 1301 * @param bool $wp_error Optional. Allow return of WP_Error on failure.
mp-wp_genesis 1302 * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success.
mp-wp_genesis 1303 */
mp-wp_genesis 1304 function wp_insert_post($postarr = array(), $wp_error = false) {
mp-wp_genesis 1305 global $wpdb, $wp_rewrite, $user_ID;
mp-wp_genesis 1306
mp-wp_genesis 1307 $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
mp-wp_genesis 1308 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
mp-wp_genesis 1309 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '',
mp-wp_genesis 1310 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0);
mp-wp_genesis 1311
mp-wp_genesis 1312 $postarr = wp_parse_args($postarr, $defaults);
mp-wp_genesis 1313 $postarr = sanitize_post($postarr, 'db');
mp-wp_genesis 1314
mp-wp_genesis 1315 // export array as variables
mp-wp_genesis 1316 extract($postarr, EXTR_SKIP);
mp-wp_genesis 1317
mp-wp_genesis 1318 // Are we updating or creating?
mp-wp_genesis 1319 $update = false;
mp-wp_genesis 1320 if ( !empty($ID) ) {
mp-wp_genesis 1321 $update = true;
mp-wp_genesis 1322 $previous_status = get_post_field('post_status', $ID);
mp-wp_genesis 1323 } else {
mp-wp_genesis 1324 $previous_status = 'new';
mp-wp_genesis 1325 }
mp-wp_genesis 1326
mp-wp_genesis 1327 if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) ) {
mp-wp_genesis 1328 if ( $wp_error )
mp-wp_genesis 1329 return new WP_Error('empty_content', __('Content, title, and excerpt are empty.'));
mp-wp_genesis 1330 else
mp-wp_genesis 1331 return 0;
mp-wp_genesis 1332 }
mp-wp_genesis 1333
mp-wp_genesis 1334 // Make sure we set a valid category
mp-wp_genesis 1335 if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
mp-wp_genesis 1336 $post_category = array(get_option('default_category'));
mp-wp_genesis 1337 }
mp-wp_genesis 1338
mp-wp_genesis 1339 //Set the default tag list
mp-wp_genesis 1340 if ( !isset($tags_input) )
mp-wp_genesis 1341 $tags_input = array();
mp-wp_genesis 1342
mp-wp_genesis 1343 if ( empty($post_author) )
mp-wp_genesis 1344 $post_author = $user_ID;
mp-wp_genesis 1345
mp-wp_genesis 1346 if ( empty($post_status) )
mp-wp_genesis 1347 $post_status = 'draft';
mp-wp_genesis 1348
mp-wp_genesis 1349 if ( empty($post_type) )
mp-wp_genesis 1350 $post_type = 'post';
mp-wp_genesis 1351
mp-wp_genesis 1352 $post_ID = 0;
mp-wp_genesis 1353
mp-wp_genesis 1354 // Get the post ID and GUID
mp-wp_genesis 1355 if ( $update ) {
mp-wp_genesis 1356 $post_ID = (int) $ID;
mp-wp_genesis 1357 $guid = get_post_field( 'guid', $post_ID );
mp-wp_genesis 1358 }
mp-wp_genesis 1359
mp-wp_genesis 1360 // Don't allow contributors to set to set the post slug for pending review posts
mp-wp_genesis 1361 if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) )
mp-wp_genesis 1362 $post_name = '';
mp-wp_genesis 1363
mp-wp_genesis 1364 // Create a valid post name. Drafts and pending posts are allowed to have an empty
mp-wp_genesis 1365 // post name.
mp-wp_genesis 1366 if ( empty($post_name) ) {
mp-wp_genesis 1367 if ( !in_array( $post_status, array( 'draft', 'pending' ) ) )
mp-wp_genesis 1368 $post_name = sanitize_title($post_title);
mp-wp_genesis 1369 } else {
mp-wp_genesis 1370 $post_name = sanitize_title($post_name);
mp-wp_genesis 1371 }
mp-wp_genesis 1372
mp-wp_genesis 1373 // If the post date is empty (due to having been new or a draft) and status is not 'draft' or 'pending', set date to now
mp-wp_genesis 1374 if ( empty($post_date) || '0000-00-00 00:00:00' == $post_date )
mp-wp_genesis 1375 $post_date = current_time('mysql');
mp-wp_genesis 1376
mp-wp_genesis 1377 if ( empty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) {
mp-wp_genesis 1378 if ( !in_array( $post_status, array( 'draft', 'pending' ) ) )
mp-wp_genesis 1379 $post_date_gmt = get_gmt_from_date($post_date);
mp-wp_genesis 1380 else
mp-wp_genesis 1381 $post_date_gmt = '0000-00-00 00:00:00';
mp-wp_genesis 1382 }
mp-wp_genesis 1383
mp-wp_genesis 1384 if ( $update || '0000-00-00 00:00:00' == $post_date ) {
mp-wp_genesis 1385 $post_modified = current_time( 'mysql' );
mp-wp_genesis 1386 $post_modified_gmt = current_time( 'mysql', 1 );
mp-wp_genesis 1387 } else {
mp-wp_genesis 1388 $post_modified = $post_date;
mp-wp_genesis 1389 $post_modified_gmt = $post_date_gmt;
mp-wp_genesis 1390 }
mp-wp_genesis 1391
mp-wp_genesis 1392 if ( 'publish' == $post_status ) {
mp-wp_genesis 1393 $now = gmdate('Y-m-d H:i:59');
mp-wp_genesis 1394 if ( mysql2date('U', $post_date_gmt) > mysql2date('U', $now) )
mp-wp_genesis 1395 $post_status = 'future';
mp-wp_genesis 1396 }
mp-wp_genesis 1397
mp-wp_genesis 1398 if ( empty($comment_status) ) {
mp-wp_genesis 1399 if ( $update )
mp-wp_genesis 1400 $comment_status = 'closed';
mp-wp_genesis 1401 else
mp-wp_genesis 1402 $comment_status = get_option('default_comment_status');
mp-wp_genesis 1403 }
mp-wp_genesis 1404 if ( empty($ping_status) )
mp-wp_genesis 1405 $ping_status = get_option('default_ping_status');
mp-wp_genesis 1406
mp-wp_genesis 1407 if ( isset($to_ping) )
mp-wp_genesis 1408 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
mp-wp_genesis 1409 else
mp-wp_genesis 1410 $to_ping = '';
mp-wp_genesis 1411
mp-wp_genesis 1412 if ( ! isset($pinged) )
mp-wp_genesis 1413 $pinged = '';
mp-wp_genesis 1414
mp-wp_genesis 1415 if ( isset($post_parent) )
mp-wp_genesis 1416 $post_parent = (int) $post_parent;
mp-wp_genesis 1417 else
mp-wp_genesis 1418 $post_parent = 0;
mp-wp_genesis 1419
mp-wp_genesis 1420 if ( !empty($post_ID) ) {
mp-wp_genesis 1421 if ( $post_parent == $post_ID ) {
mp-wp_genesis 1422 // Post can't be its own parent
mp-wp_genesis 1423 $post_parent = 0;
mp-wp_genesis 1424 } elseif ( !empty($post_parent) ) {
mp-wp_genesis 1425 $parent_post = get_post($post_parent);
mp-wp_genesis 1426 // Check for circular dependency
mp-wp_genesis 1427 if ( $parent_post->post_parent == $post_ID )
mp-wp_genesis 1428 $post_parent = 0;
mp-wp_genesis 1429 }
mp-wp_genesis 1430 }
mp-wp_genesis 1431
mp-wp_genesis 1432 if ( isset($menu_order) )
mp-wp_genesis 1433 $menu_order = (int) $menu_order;
mp-wp_genesis 1434 else
mp-wp_genesis 1435 $menu_order = 0;
mp-wp_genesis 1436
mp-wp_genesis 1437 if ( !isset($post_password) || 'private' == $post_status )
mp-wp_genesis 1438 $post_password = '';
mp-wp_genesis 1439
mp-wp_genesis 1440 if ( !in_array( $post_status, array( 'draft', 'pending' ) ) ) {
mp-wp_genesis 1441 $post_name_check = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1", $post_name, $post_type, $post_ID, $post_parent));
mp-wp_genesis 1442
mp-wp_genesis 1443 if ($post_name_check || in_array($post_name, $wp_rewrite->feeds) ) {
mp-wp_genesis 1444 $suffix = 2;
mp-wp_genesis 1445 do {
mp-wp_genesis 1446 $alt_post_name = substr($post_name, 0, 200-(strlen($suffix)+1)). "-$suffix";
mp-wp_genesis 1447 $post_name_check = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1", $alt_post_name, $post_type, $post_ID, $post_parent));
mp-wp_genesis 1448 $suffix++;
mp-wp_genesis 1449 } while ($post_name_check);
mp-wp_genesis 1450 $post_name = $alt_post_name;
mp-wp_genesis 1451 }
mp-wp_genesis 1452 }
mp-wp_genesis 1453
mp-wp_genesis 1454 // expected_slashed (everything!)
mp-wp_genesis 1455 $data = compact( array( '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', 'guid' ) );
mp-wp_genesis 1456 $data = apply_filters('wp_insert_post_data', $data, $postarr);
mp-wp_genesis 1457 $data = stripslashes_deep( $data );
mp-wp_genesis 1458 $where = array( 'ID' => $post_ID );
mp-wp_genesis 1459
mp-wp_genesis 1460 if ($update) {
mp-wp_genesis 1461 do_action( 'pre_post_update', $post_ID );
mp-wp_genesis 1462 if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
mp-wp_genesis 1463 if ( $wp_error )
mp-wp_genesis 1464 return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error);
mp-wp_genesis 1465 else
mp-wp_genesis 1466 return 0;
mp-wp_genesis 1467 }
mp-wp_genesis 1468 } else {
mp-wp_genesis 1469 if ( isset($post_mime_type) )
mp-wp_genesis 1470 $data['post_mime_type'] = stripslashes( $post_mime_type ); // This isn't in the update
mp-wp_genesis 1471 // If there is a suggested ID, use it if not already present
mp-wp_genesis 1472 if ( !empty($import_id) ) {
mp-wp_genesis 1473 $import_id = (int) $import_id;
mp-wp_genesis 1474 if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {
mp-wp_genesis 1475 $data['ID'] = $import_id;
mp-wp_genesis 1476 }
mp-wp_genesis 1477 }
mp-wp_genesis 1478 if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
mp-wp_genesis 1479 if ( $wp_error )
mp-wp_genesis 1480 return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error);
mp-wp_genesis 1481 else
mp-wp_genesis 1482 return 0;
mp-wp_genesis 1483 }
mp-wp_genesis 1484 $post_ID = (int) $wpdb->insert_id;
mp-wp_genesis 1485
mp-wp_genesis 1486 // use the newly generated $post_ID
mp-wp_genesis 1487 $where = array( 'ID' => $post_ID );
mp-wp_genesis 1488 }
mp-wp_genesis 1489
mp-wp_genesis 1490 if ( empty($post_name) && !in_array( $post_status, array( 'draft', 'pending' ) ) ) {
mp-wp_genesis 1491 $post_name = sanitize_title($post_title, $post_ID);
mp-wp_genesis 1492 $wpdb->update( $wpdb->posts, compact( 'post_name' ), $where );
mp-wp_genesis 1493 }
mp-wp_genesis 1494
mp-wp_genesis 1495 wp_set_post_categories( $post_ID, $post_category );
mp-wp_genesis 1496 wp_set_post_tags( $post_ID, $tags_input );
mp-wp_genesis 1497
mp-wp_genesis 1498 $current_guid = get_post_field( 'guid', $post_ID );
mp-wp_genesis 1499
mp-wp_genesis 1500 if ( 'page' == $post_type )
mp-wp_genesis 1501 clean_page_cache($post_ID);
mp-wp_genesis 1502 else
mp-wp_genesis 1503 clean_post_cache($post_ID);
mp-wp_genesis 1504
mp-wp_genesis 1505 // Set GUID
mp-wp_genesis 1506 if ( !$update && '' == $current_guid )
mp-wp_genesis 1507 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
mp-wp_genesis 1508
mp-wp_genesis 1509 $post = get_post($post_ID);
mp-wp_genesis 1510
mp-wp_genesis 1511 if ( !empty($page_template) && 'page' == $post_type ) {
mp-wp_genesis 1512 $post->page_template = $page_template;
mp-wp_genesis 1513 $page_templates = get_page_templates();
mp-wp_genesis 1514 if ( 'default' != $page_template && !in_array($page_template, $page_templates) ) {
mp-wp_genesis 1515 if ( $wp_error )
mp-wp_genesis 1516 return new WP_Error('invalid_page_template', __('The page template is invalid.'));
mp-wp_genesis 1517 else
mp-wp_genesis 1518 return 0;
mp-wp_genesis 1519 }
mp-wp_genesis 1520 update_post_meta($post_ID, '_wp_page_template', $page_template);
mp-wp_genesis 1521 }
mp-wp_genesis 1522
mp-wp_genesis 1523 wp_transition_post_status($post_status, $previous_status, $post);
mp-wp_genesis 1524
mp-wp_genesis 1525 if ( $update)
mp-wp_genesis 1526 do_action('edit_post', $post_ID, $post);
mp-wp_genesis 1527
mp-wp_genesis 1528 do_action('save_post', $post_ID, $post);
mp-wp_genesis 1529 do_action('wp_insert_post', $post_ID, $post);
mp-wp_genesis 1530
mp-wp_genesis 1531 return $post_ID;
mp-wp_genesis 1532 }
mp-wp_genesis 1533
mp-wp_genesis 1534 /**
mp-wp_genesis 1535 * Update a post with new post data.
mp-wp_genesis 1536 *
mp-wp_genesis 1537 * The date does not have to be set for drafts. You can set the date and it will
mp-wp_genesis 1538 * not be overridden.
mp-wp_genesis 1539 *
mp-wp_genesis 1540 * @since 1.0.0
mp-wp_genesis 1541 *
mp-wp_genesis 1542 * @param array|object $postarr Post data.
mp-wp_genesis 1543 * @return int 0 on failure, Post ID on success.
mp-wp_genesis 1544 */
mp-wp_genesis 1545 function wp_update_post($postarr = array()) {
mp-wp_genesis 1546 if ( is_object($postarr) )
mp-wp_genesis 1547 $postarr = get_object_vars($postarr);
mp-wp_genesis 1548
mp-wp_genesis 1549 // First, get all of the original fields
mp-wp_genesis 1550 $post = wp_get_single_post($postarr['ID'], ARRAY_A);
mp-wp_genesis 1551
mp-wp_genesis 1552 // Escape data pulled from DB.
mp-wp_genesis 1553 $post = add_magic_quotes($post);
mp-wp_genesis 1554
mp-wp_genesis 1555 // Passed post category list overwrites existing category list if not empty.
mp-wp_genesis 1556 if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
mp-wp_genesis 1557 && 0 != count($postarr['post_category']) )
mp-wp_genesis 1558 $post_cats = $postarr['post_category'];
mp-wp_genesis 1559 else
mp-wp_genesis 1560 $post_cats = $post['post_category'];
mp-wp_genesis 1561
mp-wp_genesis 1562 // Drafts shouldn't be assigned a date unless explicitly done so by the user
mp-wp_genesis 1563 if ( in_array($post['post_status'], array('draft', 'pending')) && empty($postarr['edit_date']) &&
mp-wp_genesis 1564 ('0000-00-00 00:00:00' == $post['post_date_gmt']) )
mp-wp_genesis 1565 $clear_date = true;
mp-wp_genesis 1566 else
mp-wp_genesis 1567 $clear_date = false;
mp-wp_genesis 1568
mp-wp_genesis 1569 // Merge old and new fields with new fields overwriting old ones.
mp-wp_genesis 1570 $postarr = array_merge($post, $postarr);
mp-wp_genesis 1571 $postarr['post_category'] = $post_cats;
mp-wp_genesis 1572 if ( $clear_date ) {
mp-wp_genesis 1573 $postarr['post_date'] = current_time('mysql');
mp-wp_genesis 1574 $postarr['post_date_gmt'] = '';
mp-wp_genesis 1575 }
mp-wp_genesis 1576
mp-wp_genesis 1577 if ($postarr['post_type'] == 'attachment')
mp-wp_genesis 1578 return wp_insert_attachment($postarr);
mp-wp_genesis 1579
mp-wp_genesis 1580 return wp_insert_post($postarr);
mp-wp_genesis 1581 }
mp-wp_genesis 1582
mp-wp_genesis 1583 /**
mp-wp_genesis 1584 * Publish a post by transitioning the post status.
mp-wp_genesis 1585 *
mp-wp_genesis 1586 * @since 2.1.0
mp-wp_genesis 1587 * @uses $wpdb
mp-wp_genesis 1588 * @uses do_action() Calls 'edit_post', 'save_post', and 'wp_insert_post' on post_id and post data.
mp-wp_genesis 1589 *
mp-wp_genesis 1590 * @param int $post_id Post ID.
mp-wp_genesis 1591 * @return null
mp-wp_genesis 1592 */
mp-wp_genesis 1593 function wp_publish_post($post_id) {
mp-wp_genesis 1594 global $wpdb;
mp-wp_genesis 1595
mp-wp_genesis 1596 $post = get_post($post_id);
mp-wp_genesis 1597
mp-wp_genesis 1598 if ( empty($post) )
mp-wp_genesis 1599 return;
mp-wp_genesis 1600
mp-wp_genesis 1601 if ( 'publish' == $post->post_status )
mp-wp_genesis 1602 return;
mp-wp_genesis 1603
mp-wp_genesis 1604 $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post_id ) );
mp-wp_genesis 1605
mp-wp_genesis 1606 $old_status = $post->post_status;
mp-wp_genesis 1607 $post->post_status = 'publish';
mp-wp_genesis 1608 wp_transition_post_status('publish', $old_status, $post);
mp-wp_genesis 1609
mp-wp_genesis 1610 // Update counts for the post's terms.
mp-wp_genesis 1611 foreach ( (array) get_object_taxonomies('post') as $taxonomy ) {
mp-wp_genesis 1612 $tt_ids = wp_get_object_terms($post_id, $taxonomy, 'fields=tt_ids');
mp-wp_genesis 1613 wp_update_term_count($tt_ids, $taxonomy);
mp-wp_genesis 1614 }
mp-wp_genesis 1615
mp-wp_genesis 1616 do_action('edit_post', $post_id, $post);
mp-wp_genesis 1617 do_action('save_post', $post_id, $post);
mp-wp_genesis 1618 do_action('wp_insert_post', $post_id, $post);
mp-wp_genesis 1619 }
mp-wp_genesis 1620
mp-wp_genesis 1621 /**
mp-wp_genesis 1622 * Publish future post and make sure post ID has future post status.
mp-wp_genesis 1623 *
mp-wp_genesis 1624 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
mp-wp_genesis 1625 * from publishing drafts, etc.
mp-wp_genesis 1626 *
mp-wp_genesis 1627 * @since 2.5.0
mp-wp_genesis 1628 *
mp-wp_genesis 1629 * @param int $post_id Post ID.
mp-wp_genesis 1630 * @return null Nothing is returned. Which can mean that no action is required or post was published.
mp-wp_genesis 1631 */
mp-wp_genesis 1632 function check_and_publish_future_post($post_id) {
mp-wp_genesis 1633
mp-wp_genesis 1634 $post = get_post($post_id);
mp-wp_genesis 1635
mp-wp_genesis 1636 if ( empty($post) )
mp-wp_genesis 1637 return;
mp-wp_genesis 1638
mp-wp_genesis 1639 if ( 'future' != $post->post_status )
mp-wp_genesis 1640 return;
mp-wp_genesis 1641
mp-wp_genesis 1642 $time = strtotime( $post->post_date_gmt . ' GMT' );
mp-wp_genesis 1643
mp-wp_genesis 1644 if ( $time > time() ) { // Uh oh, someone jumped the gun!
mp-wp_genesis 1645 wp_clear_scheduled_hook( 'publish_future_post', $post_id ); // clear anything else in the system
mp-wp_genesis 1646 wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) );
mp-wp_genesis 1647 return;
mp-wp_genesis 1648 }
mp-wp_genesis 1649
mp-wp_genesis 1650 return wp_publish_post($post_id);
mp-wp_genesis 1651 }
mp-wp_genesis 1652
mp-wp_genesis 1653 /**
mp-wp_genesis 1654 * Adds tags to a post.
mp-wp_genesis 1655 *
mp-wp_genesis 1656 * @uses wp_set_post_tags() Same first two parameters, but the last parameter is always set to true.
mp-wp_genesis 1657 *
mp-wp_genesis 1658 * @package WordPress
mp-wp_genesis 1659 * @subpackage Post
mp-wp_genesis 1660 * @since 2.3.0
mp-wp_genesis 1661 *
mp-wp_genesis 1662 * @param int $post_id Post ID
mp-wp_genesis 1663 * @param string $tags The tags to set for the post, separated by commas.
mp-wp_genesis 1664 * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise
mp-wp_genesis 1665 */
mp-wp_genesis 1666 function wp_add_post_tags($post_id = 0, $tags = '') {
mp-wp_genesis 1667 return wp_set_post_tags($post_id, $tags, true);
mp-wp_genesis 1668 }
mp-wp_genesis 1669
mp-wp_genesis 1670
mp-wp_genesis 1671 /**
mp-wp_genesis 1672 * Set the tags for a post.
mp-wp_genesis 1673 *
mp-wp_genesis 1674 * @since 2.3.0
mp-wp_genesis 1675 * @uses wp_set_object_terms() Sets the tags for the post.
mp-wp_genesis 1676 *
mp-wp_genesis 1677 * @param int $post_id Post ID.
mp-wp_genesis 1678 * @param string $tags The tags to set for the post, separated by commas.
mp-wp_genesis 1679 * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags.
mp-wp_genesis 1680 * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise
mp-wp_genesis 1681 */
mp-wp_genesis 1682 function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
mp-wp_genesis 1683
mp-wp_genesis 1684 $post_id = (int) $post_id;
mp-wp_genesis 1685
mp-wp_genesis 1686 if ( !$post_id )
mp-wp_genesis 1687 return false;
mp-wp_genesis 1688
mp-wp_genesis 1689 if ( empty($tags) )
mp-wp_genesis 1690 $tags = array();
mp-wp_genesis 1691 $tags = (is_array($tags)) ? $tags : explode( ',', trim($tags, " \n\t\r\0\x0B,") );
mp-wp_genesis 1692 wp_set_object_terms($post_id, $tags, 'post_tag', $append);
mp-wp_genesis 1693 }
mp-wp_genesis 1694
mp-wp_genesis 1695 /**
mp-wp_genesis 1696 * Set categories for a post.
mp-wp_genesis 1697 *
mp-wp_genesis 1698 * If the post categories parameter is not set, then the default category is
mp-wp_genesis 1699 * going used.
mp-wp_genesis 1700 *
mp-wp_genesis 1701 * @since 2.1.0
mp-wp_genesis 1702 *
mp-wp_genesis 1703 * @param int $post_ID Post ID.
mp-wp_genesis 1704 * @param array $post_categories Optional. List of categories.
mp-wp_genesis 1705 * @return bool|mixed
mp-wp_genesis 1706 */
mp-wp_genesis 1707 function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
mp-wp_genesis 1708 $post_ID = (int) $post_ID;
mp-wp_genesis 1709 // If $post_categories isn't already an array, make it one:
mp-wp_genesis 1710 if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories))
mp-wp_genesis 1711 $post_categories = array(get_option('default_category'));
mp-wp_genesis 1712 else if ( 1 == count($post_categories) && '' == $post_categories[0] )
mp-wp_genesis 1713 return true;
mp-wp_genesis 1714
mp-wp_genesis 1715 $post_categories = array_map('intval', $post_categories);
mp-wp_genesis 1716 $post_categories = array_unique($post_categories);
mp-wp_genesis 1717
mp-wp_genesis 1718 return wp_set_object_terms($post_ID, $post_categories, 'category');
mp-wp_genesis 1719 }
mp-wp_genesis 1720
mp-wp_genesis 1721 /**
mp-wp_genesis 1722 * Transition the post status of a post.
mp-wp_genesis 1723 *
mp-wp_genesis 1724 * Calls hooks to transition post status. If the new post status is not the same
mp-wp_genesis 1725 * as the previous post status, then two hooks will be ran, the first is
mp-wp_genesis 1726 * 'transition_post_status' with new status, old status, and post data. The
mp-wp_genesis 1727 * next action called is 'OLDSTATUS_to_NEWSTATUS' the NEWSTATUS is the
mp-wp_genesis 1728 * $new_status parameter and the OLDSTATUS is $old_status parameter; it has the
mp-wp_genesis 1729 * post data.
mp-wp_genesis 1730 *
mp-wp_genesis 1731 * The final action will run whether or not the post statuses are the same. The
mp-wp_genesis 1732 * action is named 'NEWSTATUS_POSTTYPE', NEWSTATUS is from the $new_status
mp-wp_genesis 1733 * parameter and POSTTYPE is post_type post data.
mp-wp_genesis 1734 *
mp-wp_genesis 1735 * @since 2.3.0
mp-wp_genesis 1736 *
mp-wp_genesis 1737 * @param string $new_status Transition to this post status.
mp-wp_genesis 1738 * @param string $old_status Previous post status.
mp-wp_genesis 1739 * @param object $post Post data.
mp-wp_genesis 1740 */
mp-wp_genesis 1741 function wp_transition_post_status($new_status, $old_status, $post) {
mp-wp_genesis 1742 if ( $new_status != $old_status ) {
mp-wp_genesis 1743 do_action('transition_post_status', $new_status, $old_status, $post);
mp-wp_genesis 1744 do_action("${old_status}_to_$new_status", $post);
mp-wp_genesis 1745 }
mp-wp_genesis 1746 do_action("${new_status}_$post->post_type", $post->ID, $post);
mp-wp_genesis 1747 }
mp-wp_genesis 1748
mp-wp_genesis 1749 //
mp-wp_genesis 1750 // Trackback and ping functions
mp-wp_genesis 1751 //
mp-wp_genesis 1752
mp-wp_genesis 1753 /**
mp-wp_genesis 1754 * Add a URL to those already pung.
mp-wp_genesis 1755 *
mp-wp_genesis 1756 * @since 1.5.0
mp-wp_genesis 1757 * @uses $wpdb
mp-wp_genesis 1758 *
mp-wp_genesis 1759 * @param int $post_id Post ID.
mp-wp_genesis 1760 * @param string $uri Ping URI.
mp-wp_genesis 1761 * @return int How many rows were updated.
mp-wp_genesis 1762 */
mp-wp_genesis 1763 function add_ping($post_id, $uri) {
mp-wp_genesis 1764 global $wpdb;
mp-wp_genesis 1765 $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
mp-wp_genesis 1766 $pung = trim($pung);
mp-wp_genesis 1767 $pung = preg_split('/\s/', $pung);
mp-wp_genesis 1768 $pung[] = $uri;
mp-wp_genesis 1769 $new = implode("\n", $pung);
mp-wp_genesis 1770 $new = apply_filters('add_ping', $new);
mp-wp_genesis 1771 // expected_slashed ($new)
mp-wp_genesis 1772 $new = stripslashes($new);
mp-wp_genesis 1773 return $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post_id ) );
mp-wp_genesis 1774 }
mp-wp_genesis 1775
mp-wp_genesis 1776 /**
mp-wp_genesis 1777 * Retrieve enclosures already enclosed for a post.
mp-wp_genesis 1778 *
mp-wp_genesis 1779 * @since 1.5.0
mp-wp_genesis 1780 * @uses $wpdb
mp-wp_genesis 1781 *
mp-wp_genesis 1782 * @param int $post_id Post ID.
mp-wp_genesis 1783 * @return array List of enclosures
mp-wp_genesis 1784 */
mp-wp_genesis 1785 function get_enclosed($post_id) {
mp-wp_genesis 1786 $custom_fields = get_post_custom( $post_id );
mp-wp_genesis 1787 $pung = array();
mp-wp_genesis 1788 if ( !is_array( $custom_fields ) )
mp-wp_genesis 1789 return $pung;
mp-wp_genesis 1790
mp-wp_genesis 1791 foreach ( $custom_fields as $key => $val ) {
mp-wp_genesis 1792 if ( 'enclosure' != $key || !is_array( $val ) )
mp-wp_genesis 1793 continue;
mp-wp_genesis 1794 foreach( $val as $enc ) {
mp-wp_genesis 1795 $enclosure = split( "\n", $enc );
mp-wp_genesis 1796 $pung[] = trim( $enclosure[ 0 ] );
mp-wp_genesis 1797 }
mp-wp_genesis 1798 }
mp-wp_genesis 1799 $pung = apply_filters('get_enclosed', $pung);
mp-wp_genesis 1800 return $pung;
mp-wp_genesis 1801 }
mp-wp_genesis 1802
mp-wp_genesis 1803 /**
mp-wp_genesis 1804 * Retrieve URLs already pinged for a post.
mp-wp_genesis 1805 *
mp-wp_genesis 1806 * @since 1.5.0
mp-wp_genesis 1807 * @uses $wpdb
mp-wp_genesis 1808 *
mp-wp_genesis 1809 * @param int $post_id Post ID.
mp-wp_genesis 1810 * @return array
mp-wp_genesis 1811 */
mp-wp_genesis 1812 function get_pung($post_id) {
mp-wp_genesis 1813 global $wpdb;
mp-wp_genesis 1814 $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
mp-wp_genesis 1815 $pung = trim($pung);
mp-wp_genesis 1816 $pung = preg_split('/\s/', $pung);
mp-wp_genesis 1817 $pung = apply_filters('get_pung', $pung);
mp-wp_genesis 1818 return $pung;
mp-wp_genesis 1819 }
mp-wp_genesis 1820
mp-wp_genesis 1821 /**
mp-wp_genesis 1822 * Retrieve URLs that need to be pinged.
mp-wp_genesis 1823 *
mp-wp_genesis 1824 * @since 1.5.0
mp-wp_genesis 1825 * @uses $wpdb
mp-wp_genesis 1826 *
mp-wp_genesis 1827 * @param int $post_id Post ID
mp-wp_genesis 1828 * @return array
mp-wp_genesis 1829 */
mp-wp_genesis 1830 function get_to_ping($post_id) {
mp-wp_genesis 1831 global $wpdb;
mp-wp_genesis 1832 $to_ping = $wpdb->get_var( $wpdb->prepare( "SELECT to_ping FROM $wpdb->posts WHERE ID = %d", $post_id ));
mp-wp_genesis 1833 $to_ping = trim($to_ping);
mp-wp_genesis 1834 $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
mp-wp_genesis 1835 $to_ping = apply_filters('get_to_ping', $to_ping);
mp-wp_genesis 1836 return $to_ping;
mp-wp_genesis 1837 }
mp-wp_genesis 1838
mp-wp_genesis 1839 /**
mp-wp_genesis 1840 * Do trackbacks for a list of URLs.
mp-wp_genesis 1841 *
mp-wp_genesis 1842 * @since 1.0.0
mp-wp_genesis 1843 *
mp-wp_genesis 1844 * @param string $tb_list Comma separated list of URLs
mp-wp_genesis 1845 * @param int $post_id Post ID
mp-wp_genesis 1846 */
mp-wp_genesis 1847 function trackback_url_list($tb_list, $post_id) {
mp-wp_genesis 1848 if ( ! empty( $tb_list ) ) {
mp-wp_genesis 1849 // get post data
mp-wp_genesis 1850 $postdata = wp_get_single_post($post_id, ARRAY_A);
mp-wp_genesis 1851
mp-wp_genesis 1852 // import postdata as variables
mp-wp_genesis 1853 extract($postdata, EXTR_SKIP);
mp-wp_genesis 1854
mp-wp_genesis 1855 // form an excerpt
mp-wp_genesis 1856 $excerpt = strip_tags($post_excerpt ? $post_excerpt : $post_content);
mp-wp_genesis 1857
mp-wp_genesis 1858 if (strlen($excerpt) > 255) {
mp-wp_genesis 1859 $excerpt = substr($excerpt,0,252) . '...';
mp-wp_genesis 1860 }
mp-wp_genesis 1861
mp-wp_genesis 1862 $trackback_urls = explode(',', $tb_list);
mp-wp_genesis 1863 foreach( (array) $trackback_urls as $tb_url) {
mp-wp_genesis 1864 $tb_url = trim($tb_url);
mp-wp_genesis 1865 trackback($tb_url, stripslashes($post_title), $excerpt, $post_id);
mp-wp_genesis 1866 }
mp-wp_genesis 1867 }
mp-wp_genesis 1868 }
mp-wp_genesis 1869
mp-wp_genesis 1870 //
mp-wp_genesis 1871 // Page functions
mp-wp_genesis 1872 //
mp-wp_genesis 1873
mp-wp_genesis 1874 /**
mp-wp_genesis 1875 * Get a list of page IDs.
mp-wp_genesis 1876 *
mp-wp_genesis 1877 * @since 2.0.0
mp-wp_genesis 1878 * @uses $wpdb
mp-wp_genesis 1879 *
mp-wp_genesis 1880 * @return array List of page IDs.
mp-wp_genesis 1881 */
mp-wp_genesis 1882 function get_all_page_ids() {
mp-wp_genesis 1883 global $wpdb;
mp-wp_genesis 1884
mp-wp_genesis 1885 if ( ! $page_ids = wp_cache_get('all_page_ids', 'posts') ) {
mp-wp_genesis 1886 $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'");
mp-wp_genesis 1887 wp_cache_add('all_page_ids', $page_ids, 'posts');
mp-wp_genesis 1888 }
mp-wp_genesis 1889
mp-wp_genesis 1890 return $page_ids;
mp-wp_genesis 1891 }
mp-wp_genesis 1892
mp-wp_genesis 1893 /**
mp-wp_genesis 1894 * Retrieves page data given a page ID or page object.
mp-wp_genesis 1895 *
mp-wp_genesis 1896 * @since 1.5.1
mp-wp_genesis 1897 *
mp-wp_genesis 1898 * @param mixed $page Page object or page ID. Passed by reference.
mp-wp_genesis 1899 * @param string $output What to output. OBJECT, ARRAY_A, or ARRAY_N.
mp-wp_genesis 1900 * @param string $filter How the return value should be filtered.
mp-wp_genesis 1901 * @return mixed Page data.
mp-wp_genesis 1902 */
mp-wp_genesis 1903 function &get_page(&$page, $output = OBJECT, $filter = 'raw') {
mp-wp_genesis 1904 if ( empty($page) ) {
mp-wp_genesis 1905 if ( isset( $GLOBALS['page'] ) && isset( $GLOBALS['page']->ID ) ) {
mp-wp_genesis 1906 return get_post($GLOBALS['page'], $output, $filter);
mp-wp_genesis 1907 } else {
mp-wp_genesis 1908 $page = null;
mp-wp_genesis 1909 return $page;
mp-wp_genesis 1910 }
mp-wp_genesis 1911 }
mp-wp_genesis 1912
mp-wp_genesis 1913 $the_page = get_post($page, $output, $filter);
mp-wp_genesis 1914 return $the_page;
mp-wp_genesis 1915 }
mp-wp_genesis 1916
mp-wp_genesis 1917 /**
mp-wp_genesis 1918 * Retrieves a page given its path.
mp-wp_genesis 1919 *
mp-wp_genesis 1920 * @since 2.1.0
mp-wp_genesis 1921 * @uses $wpdb
mp-wp_genesis 1922 *
mp-wp_genesis 1923 * @param string $page_path Page path
mp-wp_genesis 1924 * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
mp-wp_genesis 1925 * @return mixed Null when complete.
mp-wp_genesis 1926 */
mp-wp_genesis 1927 function get_page_by_path($page_path, $output = OBJECT) {
mp-wp_genesis 1928 global $wpdb;
mp-wp_genesis 1929 $page_path = rawurlencode(urldecode($page_path));
mp-wp_genesis 1930 $page_path = str_replace('%2F', '/', $page_path);
mp-wp_genesis 1931 $page_path = str_replace('%20', ' ', $page_path);
mp-wp_genesis 1932 $page_paths = '/' . trim($page_path, '/');
mp-wp_genesis 1933 $leaf_path = sanitize_title(basename($page_paths));
mp-wp_genesis 1934 $page_paths = explode('/', $page_paths);
mp-wp_genesis 1935 $full_path = '';
mp-wp_genesis 1936 foreach( (array) $page_paths as $pathdir)
mp-wp_genesis 1937 $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
mp-wp_genesis 1938
mp-wp_genesis 1939 $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = 'page' OR post_type = 'attachment')", $leaf_path ));
mp-wp_genesis 1940
mp-wp_genesis 1941 if ( empty($pages) )
mp-wp_genesis 1942 return null;
mp-wp_genesis 1943
mp-wp_genesis 1944 foreach ($pages as $page) {
mp-wp_genesis 1945 $path = '/' . $leaf_path;
mp-wp_genesis 1946 $curpage = $page;
mp-wp_genesis 1947 while ($curpage->post_parent != 0) {
mp-wp_genesis 1948 $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type='page'", $curpage->post_parent ));
mp-wp_genesis 1949 $path = '/' . $curpage->post_name . $path;
mp-wp_genesis 1950 }
mp-wp_genesis 1951
mp-wp_genesis 1952 if ( $path == $full_path )
mp-wp_genesis 1953 return get_page($page->ID, $output);
mp-wp_genesis 1954 }
mp-wp_genesis 1955
mp-wp_genesis 1956 return null;
mp-wp_genesis 1957 }
mp-wp_genesis 1958
mp-wp_genesis 1959 /**
mp-wp_genesis 1960 * Retrieve a page given its title.
mp-wp_genesis 1961 *
mp-wp_genesis 1962 * @since 2.1.0
mp-wp_genesis 1963 * @uses $wpdb
mp-wp_genesis 1964 *
mp-wp_genesis 1965 * @param string $page_title Page title
mp-wp_genesis 1966 * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
mp-wp_genesis 1967 * @return mixed
mp-wp_genesis 1968 */
mp-wp_genesis 1969 function get_page_by_title($page_title, $output = OBJECT) {
mp-wp_genesis 1970 global $wpdb;
mp-wp_genesis 1971 $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='page'", $page_title ));
mp-wp_genesis 1972 if ( $page )
mp-wp_genesis 1973 return get_page($page, $output);
mp-wp_genesis 1974
mp-wp_genesis 1975 return null;
mp-wp_genesis 1976 }
mp-wp_genesis 1977
mp-wp_genesis 1978 /**
mp-wp_genesis 1979 * Retrieve child pages from list of pages matching page ID.
mp-wp_genesis 1980 *
mp-wp_genesis 1981 * Matches against the pages parameter against the page ID. Also matches all
mp-wp_genesis 1982 * children for the same to retrieve all children of a page. Does not make any
mp-wp_genesis 1983 * SQL queries to get the children.
mp-wp_genesis 1984 *
mp-wp_genesis 1985 * @since 1.5.1
mp-wp_genesis 1986 *
mp-wp_genesis 1987 * @param int $page_id Page ID.
mp-wp_genesis 1988 * @param array $pages List of pages' objects.
mp-wp_genesis 1989 * @return array
mp-wp_genesis 1990 */
mp-wp_genesis 1991 function &get_page_children($page_id, $pages) {
mp-wp_genesis 1992 $page_list = array();
mp-wp_genesis 1993 foreach ( (array) $pages as $page ) {
mp-wp_genesis 1994 if ( $page->post_parent == $page_id ) {
mp-wp_genesis 1995 $page_list[] = $page;
mp-wp_genesis 1996 if ( $children = get_page_children($page->ID, $pages) )
mp-wp_genesis 1997 $page_list = array_merge($page_list, $children);
mp-wp_genesis 1998 }
mp-wp_genesis 1999 }
mp-wp_genesis 2000 return $page_list;
mp-wp_genesis 2001 }
mp-wp_genesis 2002
mp-wp_genesis 2003 /**
mp-wp_genesis 2004 * Order the pages with children under parents in a flat list.
mp-wp_genesis 2005 *
mp-wp_genesis 2006 * Fetches the pages returned as a FLAT list, but arranged in order of their
mp-wp_genesis 2007 * hierarchy, i.e., child parents immediately follow their parents.
mp-wp_genesis 2008 *
mp-wp_genesis 2009 * @since 2.0.0
mp-wp_genesis 2010 *
mp-wp_genesis 2011 * @param array $posts Posts array.
mp-wp_genesis 2012 * @param int $parent Parent page ID.
mp-wp_genesis 2013 * @return array
mp-wp_genesis 2014 */
mp-wp_genesis 2015 function get_page_hierarchy($posts, $parent = 0) {
mp-wp_genesis 2016 $result = array ( );
mp-wp_genesis 2017 if ($posts) { foreach ( (array) $posts as $post) {
mp-wp_genesis 2018 if ($post->post_parent == $parent) {
mp-wp_genesis 2019 $result[$post->ID] = $post->post_name;
mp-wp_genesis 2020 $children = get_page_hierarchy($posts, $post->ID);
mp-wp_genesis 2021 $result += $children; //append $children to $result
mp-wp_genesis 2022 }
mp-wp_genesis 2023 } }
mp-wp_genesis 2024 return $result;
mp-wp_genesis 2025 }
mp-wp_genesis 2026
mp-wp_genesis 2027 /**
mp-wp_genesis 2028 * Builds URI for a page.
mp-wp_genesis 2029 *
mp-wp_genesis 2030 * Sub pages will be in the "directory" under the parent page post name.
mp-wp_genesis 2031 *
mp-wp_genesis 2032 * @since 1.5.0
mp-wp_genesis 2033 *
mp-wp_genesis 2034 * @param int $page_id Page ID.
mp-wp_genesis 2035 * @return string Page URI.
mp-wp_genesis 2036 */
mp-wp_genesis 2037 function get_page_uri($page_id) {
mp-wp_genesis 2038 $page = get_page($page_id);
mp-wp_genesis 2039 $uri = $page->post_name;
mp-wp_genesis 2040
mp-wp_genesis 2041 // A page cannot be it's own parent.
mp-wp_genesis 2042 if ( $page->post_parent == $page->ID )
mp-wp_genesis 2043 return $uri;
mp-wp_genesis 2044
mp-wp_genesis 2045 while ($page->post_parent != 0) {
mp-wp_genesis 2046 $page = get_page($page->post_parent);
mp-wp_genesis 2047 $uri = $page->post_name . "/" . $uri;
mp-wp_genesis 2048 }
mp-wp_genesis 2049
mp-wp_genesis 2050 return $uri;
mp-wp_genesis 2051 }
mp-wp_genesis 2052
mp-wp_genesis 2053 /**
mp-wp_genesis 2054 * Retrieve a list of pages.
mp-wp_genesis 2055 *
mp-wp_genesis 2056 * The defaults that can be overridden are the following: 'child_of',
mp-wp_genesis 2057 * 'sort_order', 'sort_column', 'post_title', 'hierarchical', 'exclude',
mp-wp_genesis 2058 * 'include', 'meta_key', 'meta_value', and 'authors'.
mp-wp_genesis 2059 *
mp-wp_genesis 2060 * @since 1.5.0
mp-wp_genesis 2061 * @uses $wpdb
mp-wp_genesis 2062 *
mp-wp_genesis 2063 * @param mixed $args Optional. Array or string of options that overrides defaults.
mp-wp_genesis 2064 * @return array List of pages matching defaults or $args
mp-wp_genesis 2065 */
mp-wp_genesis 2066 function &get_pages($args = '') {
mp-wp_genesis 2067 global $wpdb;
mp-wp_genesis 2068
mp-wp_genesis 2069 $defaults = array(
mp-wp_genesis 2070 'child_of' => 0, 'sort_order' => 'ASC',
mp-wp_genesis 2071 'sort_column' => 'post_title', 'hierarchical' => 1,
mp-wp_genesis 2072 'exclude' => '', 'include' => '',
mp-wp_genesis 2073 'meta_key' => '', 'meta_value' => '',
mp-wp_genesis 2074 'authors' => '', 'parent' => -1, 'exclude_tree' => ''
mp-wp_genesis 2075 );
mp-wp_genesis 2076
mp-wp_genesis 2077 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 2078 extract( $r, EXTR_SKIP );
mp-wp_genesis 2079
mp-wp_genesis 2080 $key = md5( serialize( compact(array_keys($defaults)) ) );
mp-wp_genesis 2081 if ( $cache = wp_cache_get( 'get_pages', 'posts' ) ) {
mp-wp_genesis 2082 if ( isset( $cache[ $key ] ) ) {
mp-wp_genesis 2083 $pages = apply_filters('get_pages', $cache[ $key ], $r );
mp-wp_genesis 2084 return $pages;
mp-wp_genesis 2085 }
mp-wp_genesis 2086 }
mp-wp_genesis 2087
mp-wp_genesis 2088 $inclusions = '';
mp-wp_genesis 2089 if ( !empty($include) ) {
mp-wp_genesis 2090 $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include
mp-wp_genesis 2091 $parent = -1;
mp-wp_genesis 2092 $exclude = '';
mp-wp_genesis 2093 $meta_key = '';
mp-wp_genesis 2094 $meta_value = '';
mp-wp_genesis 2095 $hierarchical = false;
mp-wp_genesis 2096 $incpages = preg_split('/[\s,]+/',$include);
mp-wp_genesis 2097 if ( count($incpages) ) {
mp-wp_genesis 2098 foreach ( $incpages as $incpage ) {
mp-wp_genesis 2099 if (empty($inclusions))
mp-wp_genesis 2100 $inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpage);
mp-wp_genesis 2101 else
mp-wp_genesis 2102 $inclusions .= $wpdb->prepare(' OR ID = %d ', $incpage);
mp-wp_genesis 2103 }
mp-wp_genesis 2104 }
mp-wp_genesis 2105 }
mp-wp_genesis 2106 if (!empty($inclusions))
mp-wp_genesis 2107 $inclusions .= ')';
mp-wp_genesis 2108
mp-wp_genesis 2109 $exclusions = '';
mp-wp_genesis 2110 if ( !empty($exclude) ) {
mp-wp_genesis 2111 $expages = preg_split('/[\s,]+/',$exclude);
mp-wp_genesis 2112 if ( count($expages) ) {
mp-wp_genesis 2113 foreach ( $expages as $expage ) {
mp-wp_genesis 2114 if (empty($exclusions))
mp-wp_genesis 2115 $exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expage);
mp-wp_genesis 2116 else
mp-wp_genesis 2117 $exclusions .= $wpdb->prepare(' AND ID <> %d ', $expage);
mp-wp_genesis 2118 }
mp-wp_genesis 2119 }
mp-wp_genesis 2120 }
mp-wp_genesis 2121 if (!empty($exclusions))
mp-wp_genesis 2122 $exclusions .= ')';
mp-wp_genesis 2123
mp-wp_genesis 2124 $author_query = '';
mp-wp_genesis 2125 if (!empty($authors)) {
mp-wp_genesis 2126 $post_authors = preg_split('/[\s,]+/',$authors);
mp-wp_genesis 2127
mp-wp_genesis 2128 if ( count($post_authors) ) {
mp-wp_genesis 2129 foreach ( $post_authors as $post_author ) {
mp-wp_genesis 2130 //Do we have an author id or an author login?
mp-wp_genesis 2131 if ( 0 == intval($post_author) ) {
mp-wp_genesis 2132 $post_author = get_userdatabylogin($post_author);
mp-wp_genesis 2133 if ( empty($post_author) )
mp-wp_genesis 2134 continue;
mp-wp_genesis 2135 if ( empty($post_author->ID) )
mp-wp_genesis 2136 continue;
mp-wp_genesis 2137 $post_author = $post_author->ID;
mp-wp_genesis 2138 }
mp-wp_genesis 2139
mp-wp_genesis 2140 if ( '' == $author_query )
mp-wp_genesis 2141 $author_query = $wpdb->prepare(' post_author = %d ', $post_author);
mp-wp_genesis 2142 else
mp-wp_genesis 2143 $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author);
mp-wp_genesis 2144 }
mp-wp_genesis 2145 if ( '' != $author_query )
mp-wp_genesis 2146 $author_query = " AND ($author_query)";
mp-wp_genesis 2147 }
mp-wp_genesis 2148 }
mp-wp_genesis 2149
mp-wp_genesis 2150 $join = '';
mp-wp_genesis 2151 $where = "$exclusions $inclusions ";
mp-wp_genesis 2152 if ( ! empty( $meta_key ) || ! empty( $meta_value ) ) {
mp-wp_genesis 2153 $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )";
mp-wp_genesis 2154
mp-wp_genesis 2155 // meta_key and meta_value might be slashed
mp-wp_genesis 2156 $meta_key = stripslashes($meta_key);
mp-wp_genesis 2157 $meta_value = stripslashes($meta_value);
mp-wp_genesis 2158 if ( ! empty( $meta_key ) )
mp-wp_genesis 2159 $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key);
mp-wp_genesis 2160 if ( ! empty( $meta_value ) )
mp-wp_genesis 2161 $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value);
mp-wp_genesis 2162
mp-wp_genesis 2163 }
mp-wp_genesis 2164
mp-wp_genesis 2165 if ( $parent >= 0 )
mp-wp_genesis 2166 $where .= $wpdb->prepare(' AND post_parent = %d ', $parent);
mp-wp_genesis 2167
mp-wp_genesis 2168 $query = "SELECT * FROM $wpdb->posts $join WHERE (post_type = 'page' AND post_status = 'publish') $where ";
mp-wp_genesis 2169 $query .= $author_query;
mp-wp_genesis 2170 $query .= " ORDER BY " . $sort_column . " " . $sort_order ;
mp-wp_genesis 2171
mp-wp_genesis 2172 $pages = $wpdb->get_results($query);
mp-wp_genesis 2173
mp-wp_genesis 2174 if ( empty($pages) ) {
mp-wp_genesis 2175 $pages = apply_filters('get_pages', array(), $r);
mp-wp_genesis 2176 return $pages;
mp-wp_genesis 2177 }
mp-wp_genesis 2178
mp-wp_genesis 2179 // Update cache.
mp-wp_genesis 2180 update_page_cache($pages);
mp-wp_genesis 2181
mp-wp_genesis 2182 if ( $child_of || $hierarchical )
mp-wp_genesis 2183 $pages = & get_page_children($child_of, $pages);
mp-wp_genesis 2184
mp-wp_genesis 2185 if ( !empty($exclude_tree) ) {
mp-wp_genesis 2186 $exclude = array();
mp-wp_genesis 2187
mp-wp_genesis 2188 $exclude = (int) $exclude_tree;
mp-wp_genesis 2189 $children = get_page_children($exclude, $pages);
mp-wp_genesis 2190 $excludes = array();
mp-wp_genesis 2191 foreach ( $children as $child )
mp-wp_genesis 2192 $excludes[] = $child->ID;
mp-wp_genesis 2193 $excludes[] = $exclude;
mp-wp_genesis 2194 $total = count($pages);
mp-wp_genesis 2195 for ( $i = 0; $i < $total; $i++ ) {
mp-wp_genesis 2196 if ( in_array($pages[$i]->ID, $excludes) )
mp-wp_genesis 2197 unset($pages[$i]);
mp-wp_genesis 2198 }
mp-wp_genesis 2199 }
mp-wp_genesis 2200
mp-wp_genesis 2201 $cache[ $key ] = $pages;
mp-wp_genesis 2202 wp_cache_set( 'get_pages', $cache, 'posts' );
mp-wp_genesis 2203
mp-wp_genesis 2204 $pages = apply_filters('get_pages', $pages, $r);
mp-wp_genesis 2205
mp-wp_genesis 2206 return $pages;
mp-wp_genesis 2207 }
mp-wp_genesis 2208
mp-wp_genesis 2209 //
mp-wp_genesis 2210 // Attachment functions
mp-wp_genesis 2211 //
mp-wp_genesis 2212
mp-wp_genesis 2213 /**
mp-wp_genesis 2214 * Check if the attachment URI is local one and is really an attachment.
mp-wp_genesis 2215 *
mp-wp_genesis 2216 * @since 2.0.0
mp-wp_genesis 2217 *
mp-wp_genesis 2218 * @param string $url URL to check
mp-wp_genesis 2219 * @return bool True on success, false on failure.
mp-wp_genesis 2220 */
mp-wp_genesis 2221 function is_local_attachment($url) {
mp-wp_genesis 2222 if (strpos($url, get_bloginfo('url')) === false)
mp-wp_genesis 2223 return false;
mp-wp_genesis 2224 if (strpos($url, get_bloginfo('url') . '/?attachment_id=') !== false)
mp-wp_genesis 2225 return true;
mp-wp_genesis 2226 if ( $id = url_to_postid($url) ) {
mp-wp_genesis 2227 $post = & get_post($id);
mp-wp_genesis 2228 if ( 'attachment' == $post->post_type )
mp-wp_genesis 2229 return true;
mp-wp_genesis 2230 }
mp-wp_genesis 2231 return false;
mp-wp_genesis 2232 }
mp-wp_genesis 2233
mp-wp_genesis 2234 /**
mp-wp_genesis 2235 * Insert an attachment.
mp-wp_genesis 2236 *
mp-wp_genesis 2237 * If you set the 'ID' in the $object parameter, it will mean that you are
mp-wp_genesis 2238 * updating and attempt to update the attachment. You can also set the
mp-wp_genesis 2239 * attachment name or title by setting the key 'post_name' or 'post_title'.
mp-wp_genesis 2240 *
mp-wp_genesis 2241 * You can set the dates for the attachment manually by setting the 'post_date'
mp-wp_genesis 2242 * and 'post_date_gmt' keys' values.
mp-wp_genesis 2243 *
mp-wp_genesis 2244 * By default, the comments will use the default settings for whether the
mp-wp_genesis 2245 * comments are allowed. You can close them manually or keep them open by
mp-wp_genesis 2246 * setting the value for the 'comment_status' key.
mp-wp_genesis 2247 *
mp-wp_genesis 2248 * The $object parameter can have the following:
mp-wp_genesis 2249 * 'post_status' - Default is 'draft'. Can not be override, set the same as
mp-wp_genesis 2250 * parent post.
mp-wp_genesis 2251 * 'post_type' - Default is 'post', will be set to attachment. Can not
mp-wp_genesis 2252 * override.
mp-wp_genesis 2253 * 'post_author' - Default is current user ID. The ID of the user, who added
mp-wp_genesis 2254 * the attachment.
mp-wp_genesis 2255 * 'ping_status' - Default is the value in default ping status option.
mp-wp_genesis 2256 * Whether the attachment can accept pings.
mp-wp_genesis 2257 * 'post_parent' - Default is 0. Can use $parent parameter or set this for
mp-wp_genesis 2258 * the post it belongs to, if any.
mp-wp_genesis 2259 * 'menu_order' - Default is 0. The order it is displayed.
mp-wp_genesis 2260 * 'to_ping' - Whether to ping.
mp-wp_genesis 2261 * 'pinged' - Default is empty string.
mp-wp_genesis 2262 * 'post_password' - Default is empty string. The password to access the
mp-wp_genesis 2263 * attachment.
mp-wp_genesis 2264 * 'guid' - Global Unique ID for referencing the attachment.
mp-wp_genesis 2265 * 'post_content_filtered' - Attachment post content filtered.
mp-wp_genesis 2266 * 'post_excerpt' - Attachment excerpt.
mp-wp_genesis 2267 *
mp-wp_genesis 2268 * @since 2.0.0
mp-wp_genesis 2269 * @uses $wpdb
mp-wp_genesis 2270 * @uses $user_ID
mp-wp_genesis 2271 *
mp-wp_genesis 2272 * @param string|array $object Arguments to override defaults.
mp-wp_genesis 2273 * @param string $file Optional filename.
mp-wp_genesis 2274 * @param int $post_parent Parent post ID.
mp-wp_genesis 2275 * @return int Attachment ID.
mp-wp_genesis 2276 */
mp-wp_genesis 2277 function wp_insert_attachment($object, $file = false, $parent = 0) {
mp-wp_genesis 2278 global $wpdb, $user_ID;
mp-wp_genesis 2279
mp-wp_genesis 2280 $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
mp-wp_genesis 2281 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
mp-wp_genesis 2282 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '',
mp-wp_genesis 2283 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0);
mp-wp_genesis 2284
mp-wp_genesis 2285 $object = wp_parse_args($object, $defaults);
mp-wp_genesis 2286 if ( !empty($parent) )
mp-wp_genesis 2287 $object['post_parent'] = $parent;
mp-wp_genesis 2288
mp-wp_genesis 2289 $object = sanitize_post($object, 'db');
mp-wp_genesis 2290
mp-wp_genesis 2291 // export array as variables
mp-wp_genesis 2292 extract($object, EXTR_SKIP);
mp-wp_genesis 2293
mp-wp_genesis 2294 // Make sure we set a valid category
mp-wp_genesis 2295 if ( !isset($post_category) || 0 == count($post_category) || !is_array($post_category)) {
mp-wp_genesis 2296 $post_category = array(get_option('default_category'));
mp-wp_genesis 2297 }
mp-wp_genesis 2298
mp-wp_genesis 2299 if ( empty($post_author) )
mp-wp_genesis 2300 $post_author = $user_ID;
mp-wp_genesis 2301
mp-wp_genesis 2302 $post_type = 'attachment';
mp-wp_genesis 2303 $post_status = 'inherit';
mp-wp_genesis 2304
mp-wp_genesis 2305 // Are we updating or creating?
mp-wp_genesis 2306 if ( !empty($ID) ) {
mp-wp_genesis 2307 $update = true;
mp-wp_genesis 2308 $post_ID = (int) $ID;
mp-wp_genesis 2309 } else {
mp-wp_genesis 2310 $update = false;
mp-wp_genesis 2311 $post_ID = 0;
mp-wp_genesis 2312 }
mp-wp_genesis 2313
mp-wp_genesis 2314 // Create a valid post name.
mp-wp_genesis 2315 if ( empty($post_name) )
mp-wp_genesis 2316 $post_name = sanitize_title($post_title);
mp-wp_genesis 2317 else
mp-wp_genesis 2318 $post_name = sanitize_title($post_name);
mp-wp_genesis 2319
mp-wp_genesis 2320 // expected_slashed ($post_name)
mp-wp_genesis 2321 $post_name_check = $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_status = 'inherit' AND ID != %d LIMIT 1", $post_name, $post_ID));
mp-wp_genesis 2322
mp-wp_genesis 2323 if ($post_name_check) {
mp-wp_genesis 2324 $suffix = 2;
mp-wp_genesis 2325 while ($post_name_check) {
mp-wp_genesis 2326 $alt_post_name = $post_name . "-$suffix";
mp-wp_genesis 2327 // expected_slashed ($alt_post_name, $post_name)
mp-wp_genesis 2328 $post_name_check = $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_status = 'inherit' AND ID != %d AND post_parent = %d LIMIT 1", $alt_post_name, $post_ID, $post_parent));
mp-wp_genesis 2329 $suffix++;
mp-wp_genesis 2330 }
mp-wp_genesis 2331 $post_name = $alt_post_name;
mp-wp_genesis 2332 }
mp-wp_genesis 2333
mp-wp_genesis 2334 if ( empty($post_date) )
mp-wp_genesis 2335 $post_date = current_time('mysql');
mp-wp_genesis 2336 if ( empty($post_date_gmt) )
mp-wp_genesis 2337 $post_date_gmt = current_time('mysql', 1);
mp-wp_genesis 2338
mp-wp_genesis 2339 if ( empty($post_modified) )
mp-wp_genesis 2340 $post_modified = $post_date;
mp-wp_genesis 2341 if ( empty($post_modified_gmt) )
mp-wp_genesis 2342 $post_modified_gmt = $post_date_gmt;
mp-wp_genesis 2343
mp-wp_genesis 2344 if ( empty($comment_status) ) {
mp-wp_genesis 2345 if ( $update )
mp-wp_genesis 2346 $comment_status = 'closed';
mp-wp_genesis 2347 else
mp-wp_genesis 2348 $comment_status = get_option('default_comment_status');
mp-wp_genesis 2349 }
mp-wp_genesis 2350 if ( empty($ping_status) )
mp-wp_genesis 2351 $ping_status = get_option('default_ping_status');
mp-wp_genesis 2352
mp-wp_genesis 2353 if ( isset($to_ping) )
mp-wp_genesis 2354 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
mp-wp_genesis 2355 else
mp-wp_genesis 2356 $to_ping = '';
mp-wp_genesis 2357
mp-wp_genesis 2358 if ( isset($post_parent) )
mp-wp_genesis 2359 $post_parent = (int) $post_parent;
mp-wp_genesis 2360 else
mp-wp_genesis 2361 $post_parent = 0;
mp-wp_genesis 2362
mp-wp_genesis 2363 if ( isset($menu_order) )
mp-wp_genesis 2364 $menu_order = (int) $menu_order;
mp-wp_genesis 2365 else
mp-wp_genesis 2366 $menu_order = 0;
mp-wp_genesis 2367
mp-wp_genesis 2368 if ( !isset($post_password) )
mp-wp_genesis 2369 $post_password = '';
mp-wp_genesis 2370
mp-wp_genesis 2371 if ( ! isset($pinged) )
mp-wp_genesis 2372 $pinged = '';
mp-wp_genesis 2373
mp-wp_genesis 2374 // expected_slashed (everything!)
mp-wp_genesis 2375 $data = compact( array( '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' ) );
mp-wp_genesis 2376 $data = stripslashes_deep( $data );
mp-wp_genesis 2377
mp-wp_genesis 2378 if ( $update ) {
mp-wp_genesis 2379 $wpdb->update( $wpdb->posts, $data, array( 'ID' => $post_ID ) );
mp-wp_genesis 2380 } else {
mp-wp_genesis 2381 // If there is a suggested ID, use it if not already present
mp-wp_genesis 2382 if ( !empty($import_id) ) {
mp-wp_genesis 2383 $import_id = (int) $import_id;
mp-wp_genesis 2384 if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {
mp-wp_genesis 2385 $data['ID'] = $import_id;
mp-wp_genesis 2386 }
mp-wp_genesis 2387 }
mp-wp_genesis 2388
mp-wp_genesis 2389 $wpdb->insert( $wpdb->posts, $data );
mp-wp_genesis 2390 $post_ID = (int) $wpdb->insert_id;
mp-wp_genesis 2391 }
mp-wp_genesis 2392
mp-wp_genesis 2393 if ( empty($post_name) ) {
mp-wp_genesis 2394 $post_name = sanitize_title($post_title, $post_ID);
mp-wp_genesis 2395 $wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) );
mp-wp_genesis 2396 }
mp-wp_genesis 2397
mp-wp_genesis 2398 wp_set_post_categories($post_ID, $post_category);
mp-wp_genesis 2399
mp-wp_genesis 2400 if ( $file )
mp-wp_genesis 2401 update_attached_file( $post_ID, $file );
mp-wp_genesis 2402
mp-wp_genesis 2403 clean_post_cache($post_ID);
mp-wp_genesis 2404
mp-wp_genesis 2405 if ( $update) {
mp-wp_genesis 2406 do_action('edit_attachment', $post_ID);
mp-wp_genesis 2407 } else {
mp-wp_genesis 2408 do_action('add_attachment', $post_ID);
mp-wp_genesis 2409 }
mp-wp_genesis 2410
mp-wp_genesis 2411 return $post_ID;
mp-wp_genesis 2412 }
mp-wp_genesis 2413
mp-wp_genesis 2414 /**
mp-wp_genesis 2415 * Delete an attachment.
mp-wp_genesis 2416 *
mp-wp_genesis 2417 * Will remove the file also, when the attachment is removed. Removes all post
mp-wp_genesis 2418 * meta fields, taxonomy, comments, etc associated with the attachment (except
mp-wp_genesis 2419 * the main post).
mp-wp_genesis 2420 *
mp-wp_genesis 2421 * @since 2.0.0
mp-wp_genesis 2422 * @uses $wpdb
mp-wp_genesis 2423 * @uses do_action() Calls 'delete_attachment' hook on Attachment ID.
mp-wp_genesis 2424 *
mp-wp_genesis 2425 * @param int $postid Attachment ID.
mp-wp_genesis 2426 * @return mixed False on failure. Post data on success.
mp-wp_genesis 2427 */
mp-wp_genesis 2428 function wp_delete_attachment($postid) {
mp-wp_genesis 2429 global $wpdb;
mp-wp_genesis 2430
mp-wp_genesis 2431 if ( !$post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) )
mp-wp_genesis 2432 return $post;
mp-wp_genesis 2433
mp-wp_genesis 2434 if ( 'attachment' != $post->post_type )
mp-wp_genesis 2435 return false;
mp-wp_genesis 2436
mp-wp_genesis 2437 $meta = wp_get_attachment_metadata( $postid );
mp-wp_genesis 2438 $file = get_attached_file( $postid );
mp-wp_genesis 2439
mp-wp_genesis 2440 /** @todo Delete for pluggable post taxonomies too */
mp-wp_genesis 2441 wp_delete_object_term_relationships($postid, array('category', 'post_tag'));
mp-wp_genesis 2442
mp-wp_genesis 2443 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid ));
mp-wp_genesis 2444
mp-wp_genesis 2445 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ));
mp-wp_genesis 2446
mp-wp_genesis 2447 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d ", $postid ));
mp-wp_genesis 2448
mp-wp_genesis 2449 $uploadPath = wp_upload_dir();
mp-wp_genesis 2450
mp-wp_genesis 2451 if ( ! empty($meta['thumb']) ) {
mp-wp_genesis 2452 // Don't delete the thumb if another attachment uses it
mp-wp_genesis 2453 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", '%'.$meta['thumb'].'%', $postid)) ) {
mp-wp_genesis 2454 $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
mp-wp_genesis 2455 $thumbfile = apply_filters('wp_delete_file', $thumbfile);
mp-wp_genesis 2456 @ unlink( path_join($uploadPath['basedir'], $thumbfile) );
mp-wp_genesis 2457 }
mp-wp_genesis 2458 }
mp-wp_genesis 2459
mp-wp_genesis 2460 // remove intermediate images if there are any
mp-wp_genesis 2461 $sizes = apply_filters('intermediate_image_sizes', array('thumbnail', 'medium', 'large'));
mp-wp_genesis 2462 foreach ( $sizes as $size ) {
mp-wp_genesis 2463 if ( $intermediate = image_get_intermediate_size($postid, $size) ) {
mp-wp_genesis 2464 $intermediate_file = apply_filters('wp_delete_file', $intermediate['path']);
mp-wp_genesis 2465 @ unlink( path_join($uploadPath['basedir'], $intermediate_file) );
mp-wp_genesis 2466 }
mp-wp_genesis 2467 }
mp-wp_genesis 2468
mp-wp_genesis 2469 $file = apply_filters('wp_delete_file', $file);
mp-wp_genesis 2470
mp-wp_genesis 2471 if ( ! empty($file) )
mp-wp_genesis 2472 @ unlink($file);
mp-wp_genesis 2473
mp-wp_genesis 2474 clean_post_cache($postid);
mp-wp_genesis 2475
mp-wp_genesis 2476 do_action('delete_attachment', $postid);
mp-wp_genesis 2477
mp-wp_genesis 2478 return $post;
mp-wp_genesis 2479 }
mp-wp_genesis 2480
mp-wp_genesis 2481 /**
mp-wp_genesis 2482 * Retrieve attachment meta field for attachment ID.
mp-wp_genesis 2483 *
mp-wp_genesis 2484 * @since 2.1.0
mp-wp_genesis 2485 *
mp-wp_genesis 2486 * @param int $post_id Attachment ID
mp-wp_genesis 2487 * @param bool $unfiltered Optional, default is false. If true, filters are not run.
mp-wp_genesis 2488 * @return string|bool Attachment meta field. False on failure.
mp-wp_genesis 2489 */
mp-wp_genesis 2490 function wp_get_attachment_metadata( $post_id, $unfiltered = false ) {
mp-wp_genesis 2491 $post_id = (int) $post_id;
mp-wp_genesis 2492 if ( !$post =& get_post( $post_id ) )
mp-wp_genesis 2493 return false;
mp-wp_genesis 2494
mp-wp_genesis 2495 $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
mp-wp_genesis 2496 if ( $unfiltered )
mp-wp_genesis 2497 return $data;
mp-wp_genesis 2498 return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID );
mp-wp_genesis 2499 }
mp-wp_genesis 2500
mp-wp_genesis 2501 /**
mp-wp_genesis 2502 * Update metadata for an attachment.
mp-wp_genesis 2503 *
mp-wp_genesis 2504 * @since 2.1.0
mp-wp_genesis 2505 *
mp-wp_genesis 2506 * @param int $post_id Attachment ID.
mp-wp_genesis 2507 * @param array $data Attachment data.
mp-wp_genesis 2508 * @return int
mp-wp_genesis 2509 */
mp-wp_genesis 2510 function wp_update_attachment_metadata( $post_id, $data ) {
mp-wp_genesis 2511 $post_id = (int) $post_id;
mp-wp_genesis 2512 if ( !$post =& get_post( $post_id ) )
mp-wp_genesis 2513 return false;
mp-wp_genesis 2514
mp-wp_genesis 2515 $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID );
mp-wp_genesis 2516
mp-wp_genesis 2517 return update_post_meta( $post->ID, '_wp_attachment_metadata', $data);
mp-wp_genesis 2518 }
mp-wp_genesis 2519
mp-wp_genesis 2520 /**
mp-wp_genesis 2521 * Retrieve the URL for an attachment.
mp-wp_genesis 2522 *
mp-wp_genesis 2523 * @since 2.1.0
mp-wp_genesis 2524 *
mp-wp_genesis 2525 * @param int $post_id Attachment ID.
mp-wp_genesis 2526 * @return string
mp-wp_genesis 2527 */
mp-wp_genesis 2528 function wp_get_attachment_url( $post_id = 0 ) {
mp-wp_genesis 2529 $post_id = (int) $post_id;
mp-wp_genesis 2530 if ( !$post =& get_post( $post_id ) )
mp-wp_genesis 2531 return false;
mp-wp_genesis 2532
mp-wp_genesis 2533 $url = '';
mp-wp_genesis 2534 if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) { //Get attached file
mp-wp_genesis 2535 if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { //Get upload directory
mp-wp_genesis 2536 if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location
mp-wp_genesis 2537 $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location
mp-wp_genesis 2538 }
mp-wp_genesis 2539 }
mp-wp_genesis 2540
mp-wp_genesis 2541 if ( empty($url) ) //If any of the above options failed, Fallback on the GUID as used pre-2.7, not recomended to rely upon this.
mp-wp_genesis 2542 $url = get_the_guid( $post->ID );
mp-wp_genesis 2543
mp-wp_genesis 2544 if ( 'attachment' != $post->post_type || empty($url) )
mp-wp_genesis 2545 return false;
mp-wp_genesis 2546
mp-wp_genesis 2547 return apply_filters( 'wp_get_attachment_url', $url, $post->ID );
mp-wp_genesis 2548 }
mp-wp_genesis 2549
mp-wp_genesis 2550 /**
mp-wp_genesis 2551 * Retrieve thumbnail for an attachment.
mp-wp_genesis 2552 *
mp-wp_genesis 2553 * @since 2.1.0
mp-wp_genesis 2554 *
mp-wp_genesis 2555 * @param int $post_id Attachment ID.
mp-wp_genesis 2556 * @return mixed False on failure. Thumbnail file path on success.
mp-wp_genesis 2557 */
mp-wp_genesis 2558 function wp_get_attachment_thumb_file( $post_id = 0 ) {
mp-wp_genesis 2559 $post_id = (int) $post_id;
mp-wp_genesis 2560 if ( !$post =& get_post( $post_id ) )
mp-wp_genesis 2561 return false;
mp-wp_genesis 2562 if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) )
mp-wp_genesis 2563 return false;
mp-wp_genesis 2564
mp-wp_genesis 2565 $file = get_attached_file( $post->ID );
mp-wp_genesis 2566
mp-wp_genesis 2567 if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) )
mp-wp_genesis 2568 return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );
mp-wp_genesis 2569 return false;
mp-wp_genesis 2570 }
mp-wp_genesis 2571
mp-wp_genesis 2572 /**
mp-wp_genesis 2573 * Retrieve URL for an attachment thumbnail.
mp-wp_genesis 2574 *
mp-wp_genesis 2575 * @since 2.1.0
mp-wp_genesis 2576 *
mp-wp_genesis 2577 * @param int $post_id Attachment ID
mp-wp_genesis 2578 * @return string|bool False on failure. Thumbnail URL on success.
mp-wp_genesis 2579 */
mp-wp_genesis 2580 function wp_get_attachment_thumb_url( $post_id = 0 ) {
mp-wp_genesis 2581 $post_id = (int) $post_id;
mp-wp_genesis 2582 if ( !$post =& get_post( $post_id ) )
mp-wp_genesis 2583 return false;
mp-wp_genesis 2584 if ( !$url = wp_get_attachment_url( $post->ID ) )
mp-wp_genesis 2585 return false;
mp-wp_genesis 2586
mp-wp_genesis 2587 $sized = image_downsize( $post_id, 'thumbnail' );
mp-wp_genesis 2588 if ( $sized )
mp-wp_genesis 2589 return $sized[0];
mp-wp_genesis 2590
mp-wp_genesis 2591 if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) )
mp-wp_genesis 2592 return false;
mp-wp_genesis 2593
mp-wp_genesis 2594 $url = str_replace(basename($url), basename($thumb), $url);
mp-wp_genesis 2595
mp-wp_genesis 2596 return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID );
mp-wp_genesis 2597 }
mp-wp_genesis 2598
mp-wp_genesis 2599 /**
mp-wp_genesis 2600 * Check if the attachment is an image.
mp-wp_genesis 2601 *
mp-wp_genesis 2602 * @since 2.1.0
mp-wp_genesis 2603 *
mp-wp_genesis 2604 * @param int $post_id Attachment ID
mp-wp_genesis 2605 * @return bool
mp-wp_genesis 2606 */
mp-wp_genesis 2607 function wp_attachment_is_image( $post_id = 0 ) {
mp-wp_genesis 2608 $post_id = (int) $post_id;
mp-wp_genesis 2609 if ( !$post =& get_post( $post_id ) )
mp-wp_genesis 2610 return false;
mp-wp_genesis 2611
mp-wp_genesis 2612 if ( !$file = get_attached_file( $post->ID ) )
mp-wp_genesis 2613 return false;
mp-wp_genesis 2614
mp-wp_genesis 2615 $ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false;
mp-wp_genesis 2616
mp-wp_genesis 2617 $image_exts = array('jpg', 'jpeg', 'gif', 'png');
mp-wp_genesis 2618
mp-wp_genesis 2619 if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) )
mp-wp_genesis 2620 return true;
mp-wp_genesis 2621 return false;
mp-wp_genesis 2622 }
mp-wp_genesis 2623
mp-wp_genesis 2624 /**
mp-wp_genesis 2625 * Retrieve the icon for a MIME type.
mp-wp_genesis 2626 *
mp-wp_genesis 2627 * @since 2.1.0
mp-wp_genesis 2628 *
mp-wp_genesis 2629 * @param string $mime MIME type
mp-wp_genesis 2630 * @return string|bool
mp-wp_genesis 2631 */
mp-wp_genesis 2632 function wp_mime_type_icon( $mime = 0 ) {
mp-wp_genesis 2633 if ( !is_numeric($mime) )
mp-wp_genesis 2634 $icon = wp_cache_get("mime_type_icon_$mime");
mp-wp_genesis 2635 if ( empty($icon) ) {
mp-wp_genesis 2636 $post_id = 0;
mp-wp_genesis 2637 $post_mimes = array();
mp-wp_genesis 2638 if ( is_numeric($mime) ) {
mp-wp_genesis 2639 $mime = (int) $mime;
mp-wp_genesis 2640 if ( $post =& get_post( $mime ) ) {
mp-wp_genesis 2641 $post_id = (int) $post->ID;
mp-wp_genesis 2642 $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $post->guid);
mp-wp_genesis 2643 if ( !empty($ext) ) {
mp-wp_genesis 2644 $post_mimes[] = $ext;
mp-wp_genesis 2645 if ( $ext_type = wp_ext2type( $ext ) )
mp-wp_genesis 2646 $post_mimes[] = $ext_type;
mp-wp_genesis 2647 }
mp-wp_genesis 2648 $mime = $post->post_mime_type;
mp-wp_genesis 2649 } else {
mp-wp_genesis 2650 $mime = 0;
mp-wp_genesis 2651 }
mp-wp_genesis 2652 } else {
mp-wp_genesis 2653 $post_mimes[] = $mime;
mp-wp_genesis 2654 }
mp-wp_genesis 2655
mp-wp_genesis 2656 $icon_files = wp_cache_get('icon_files');
mp-wp_genesis 2657
mp-wp_genesis 2658 if ( !is_array($icon_files) ) {
mp-wp_genesis 2659 $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' );
mp-wp_genesis 2660 $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url('images/crystal') );
mp-wp_genesis 2661 $dirs = apply_filters( 'icon_dirs', array($icon_dir => $icon_dir_uri) );
mp-wp_genesis 2662 $icon_files = array();
mp-wp_genesis 2663 while ( $dirs ) {
mp-wp_genesis 2664 $dir = array_shift($keys = array_keys($dirs));
mp-wp_genesis 2665 $uri = array_shift($dirs);
mp-wp_genesis 2666 if ( $dh = opendir($dir) ) {
mp-wp_genesis 2667 while ( false !== $file = readdir($dh) ) {
mp-wp_genesis 2668 $file = basename($file);
mp-wp_genesis 2669 if ( substr($file, 0, 1) == '.' )
mp-wp_genesis 2670 continue;
mp-wp_genesis 2671 if ( !in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg') ) ) {
mp-wp_genesis 2672 if ( is_dir("$dir/$file") )
mp-wp_genesis 2673 $dirs["$dir/$file"] = "$uri/$file";
mp-wp_genesis 2674 continue;
mp-wp_genesis 2675 }
mp-wp_genesis 2676 $icon_files["$dir/$file"] = "$uri/$file";
mp-wp_genesis 2677 }
mp-wp_genesis 2678 closedir($dh);
mp-wp_genesis 2679 }
mp-wp_genesis 2680 }
mp-wp_genesis 2681 wp_cache_set('icon_files', $icon_files, 600);
mp-wp_genesis 2682 }
mp-wp_genesis 2683
mp-wp_genesis 2684 // Icon basename - extension = MIME wildcard
mp-wp_genesis 2685 foreach ( $icon_files as $file => $uri )
mp-wp_genesis 2686 $types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file];
mp-wp_genesis 2687
mp-wp_genesis 2688 if ( ! empty($mime) ) {
mp-wp_genesis 2689 $post_mimes[] = substr($mime, 0, strpos($mime, '/'));
mp-wp_genesis 2690 $post_mimes[] = substr($mime, strpos($mime, '/') + 1);
mp-wp_genesis 2691 $post_mimes[] = str_replace('/', '_', $mime);
mp-wp_genesis 2692 }
mp-wp_genesis 2693
mp-wp_genesis 2694 $matches = wp_match_mime_types(array_keys($types), $post_mimes);
mp-wp_genesis 2695 $matches['default'] = array('default');
mp-wp_genesis 2696
mp-wp_genesis 2697 foreach ( $matches as $match => $wilds ) {
mp-wp_genesis 2698 if ( isset($types[$wilds[0]])) {
mp-wp_genesis 2699 $icon = $types[$wilds[0]];
mp-wp_genesis 2700 if ( !is_numeric($mime) )
mp-wp_genesis 2701 wp_cache_set("mime_type_icon_$mime", $icon);
mp-wp_genesis 2702 break;
mp-wp_genesis 2703 }
mp-wp_genesis 2704 }
mp-wp_genesis 2705 }
mp-wp_genesis 2706
mp-wp_genesis 2707 return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); // Last arg is 0 if function pass mime type.
mp-wp_genesis 2708 }
mp-wp_genesis 2709
mp-wp_genesis 2710 /**
mp-wp_genesis 2711 * Checked for changed slugs for published posts and save old slug.
mp-wp_genesis 2712 *
mp-wp_genesis 2713 * The function is used along with form POST data. It checks for the wp-old-slug
mp-wp_genesis 2714 * POST field. Will only be concerned with published posts and the slug actually
mp-wp_genesis 2715 * changing.
mp-wp_genesis 2716 *
mp-wp_genesis 2717 * If the slug was changed and not already part of the old slugs then it will be
mp-wp_genesis 2718 * added to the post meta field ('_wp_old_slug') for storing old slugs for that
mp-wp_genesis 2719 * post.
mp-wp_genesis 2720 *
mp-wp_genesis 2721 * The most logically usage of this function is redirecting changed posts, so
mp-wp_genesis 2722 * that those that linked to an changed post will be redirected to the new post.
mp-wp_genesis 2723 *
mp-wp_genesis 2724 * @since 2.1.0
mp-wp_genesis 2725 *
mp-wp_genesis 2726 * @param int $post_id Post ID.
mp-wp_genesis 2727 * @return int Same as $post_id
mp-wp_genesis 2728 */
mp-wp_genesis 2729 function wp_check_for_changed_slugs($post_id) {
mp-wp_genesis 2730 if ( !isset($_POST['wp-old-slug']) || !strlen($_POST['wp-old-slug']) )
mp-wp_genesis 2731 return $post_id;
mp-wp_genesis 2732
mp-wp_genesis 2733 $post = &get_post($post_id);
mp-wp_genesis 2734
mp-wp_genesis 2735 // we're only concerned with published posts
mp-wp_genesis 2736 if ( $post->post_status != 'publish' || $post->post_type != 'post' )
mp-wp_genesis 2737 return $post_id;
mp-wp_genesis 2738
mp-wp_genesis 2739 // only bother if the slug has changed
mp-wp_genesis 2740 if ( $post->post_name == $_POST['wp-old-slug'] )
mp-wp_genesis 2741 return $post_id;
mp-wp_genesis 2742
mp-wp_genesis 2743 $old_slugs = (array) get_post_meta($post_id, '_wp_old_slug');
mp-wp_genesis 2744
mp-wp_genesis 2745 // if we haven't added this old slug before, add it now
mp-wp_genesis 2746 if ( !count($old_slugs) || !in_array($_POST['wp-old-slug'], $old_slugs) )
mp-wp_genesis 2747 add_post_meta($post_id, '_wp_old_slug', $_POST['wp-old-slug']);
mp-wp_genesis 2748
mp-wp_genesis 2749 // if the new slug was used previously, delete it from the list
mp-wp_genesis 2750 if ( in_array($post->post_name, $old_slugs) )
mp-wp_genesis 2751 delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
mp-wp_genesis 2752
mp-wp_genesis 2753 return $post_id;
mp-wp_genesis 2754 }
mp-wp_genesis 2755
mp-wp_genesis 2756 /**
mp-wp_genesis 2757 * Retrieve the private post SQL based on capability.
mp-wp_genesis 2758 *
mp-wp_genesis 2759 * This function provides a standardized way to appropriately select on the
mp-wp_genesis 2760 * post_status of posts/pages. The function will return a piece of SQL code that
mp-wp_genesis 2761 * can be added to a WHERE clause; this SQL is constructed to allow all
mp-wp_genesis 2762 * published posts, and all private posts to which the user has access.
mp-wp_genesis 2763 *
mp-wp_genesis 2764 * It also allows plugins that define their own post type to control the cap by
mp-wp_genesis 2765 * using the hook 'pub_priv_sql_capability'. The plugin is expected to return
mp-wp_genesis 2766 * the capability the user must have to read the private post type.
mp-wp_genesis 2767 *
mp-wp_genesis 2768 * @since 2.2.0
mp-wp_genesis 2769 *
mp-wp_genesis 2770 * @uses $user_ID
mp-wp_genesis 2771 * @uses apply_filters() Call 'pub_priv_sql_capability' filter for plugins with different post types.
mp-wp_genesis 2772 *
mp-wp_genesis 2773 * @param string $post_type currently only supports 'post' or 'page'.
mp-wp_genesis 2774 * @return string SQL code that can be added to a where clause.
mp-wp_genesis 2775 */
mp-wp_genesis 2776 function get_private_posts_cap_sql($post_type) {
mp-wp_genesis 2777 global $user_ID;
mp-wp_genesis 2778 $cap = '';
mp-wp_genesis 2779
mp-wp_genesis 2780 // Private posts
mp-wp_genesis 2781 if ($post_type == 'post') {
mp-wp_genesis 2782 $cap = 'read_private_posts';
mp-wp_genesis 2783 // Private pages
mp-wp_genesis 2784 } elseif ($post_type == 'page') {
mp-wp_genesis 2785 $cap = 'read_private_pages';
mp-wp_genesis 2786 // Dunno what it is, maybe plugins have their own post type?
mp-wp_genesis 2787 } else {
mp-wp_genesis 2788 $cap = apply_filters('pub_priv_sql_capability', $cap);
mp-wp_genesis 2789
mp-wp_genesis 2790 if (empty($cap)) {
mp-wp_genesis 2791 // We don't know what it is, filters don't change anything,
mp-wp_genesis 2792 // so set the SQL up to return nothing.
mp-wp_genesis 2793 return '1 = 0';
mp-wp_genesis 2794 }
mp-wp_genesis 2795 }
mp-wp_genesis 2796
mp-wp_genesis 2797 $sql = '(post_status = \'publish\'';
mp-wp_genesis 2798
mp-wp_genesis 2799 if (current_user_can($cap)) {
mp-wp_genesis 2800 // Does the user have the capability to view private posts? Guess so.
mp-wp_genesis 2801 $sql .= ' OR post_status = \'private\'';
mp-wp_genesis 2802 } elseif (is_user_logged_in()) {
mp-wp_genesis 2803 // Users can view their own private posts.
mp-wp_genesis 2804 $sql .= ' OR post_status = \'private\' AND post_author = \'' . $user_ID . '\'';
mp-wp_genesis 2805 }
mp-wp_genesis 2806
mp-wp_genesis 2807 $sql .= ')';
mp-wp_genesis 2808
mp-wp_genesis 2809 return $sql;
mp-wp_genesis 2810 }
mp-wp_genesis 2811
mp-wp_genesis 2812 /**
mp-wp_genesis 2813 * Retrieve the date the the last post was published.
mp-wp_genesis 2814 *
mp-wp_genesis 2815 * The server timezone is the default and is the difference between GMT and
mp-wp_genesis 2816 * server time. The 'blog' value is the date when the last post was posted. The
mp-wp_genesis 2817 * 'gmt' is when the last post was posted in GMT formatted date.
mp-wp_genesis 2818 *
mp-wp_genesis 2819 * @since 0.71
mp-wp_genesis 2820 *
mp-wp_genesis 2821 * @uses $wpdb
mp-wp_genesis 2822 * @uses $blog_id
mp-wp_genesis 2823 * @uses apply_filters() Calls 'get_lastpostdate' filter
mp-wp_genesis 2824 *
mp-wp_genesis 2825 * @global mixed $cache_lastpostdate Stores the last post date
mp-wp_genesis 2826 * @global mixed $pagenow The current page being viewed
mp-wp_genesis 2827 *
mp-wp_genesis 2828 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
mp-wp_genesis 2829 * @return string The date of the last post.
mp-wp_genesis 2830 */
mp-wp_genesis 2831 function get_lastpostdate($timezone = 'server') {
mp-wp_genesis 2832 global $cache_lastpostdate, $wpdb, $blog_id;
mp-wp_genesis 2833 $add_seconds_server = date('Z');
mp-wp_genesis 2834 if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
mp-wp_genesis 2835 switch(strtolower($timezone)) {
mp-wp_genesis 2836 case 'gmt':
mp-wp_genesis 2837 $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
mp-wp_genesis 2838 break;
mp-wp_genesis 2839 case 'blog':
mp-wp_genesis 2840 $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
mp-wp_genesis 2841 break;
mp-wp_genesis 2842 case 'server':
mp-wp_genesis 2843 $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
mp-wp_genesis 2844 break;
mp-wp_genesis 2845 }
mp-wp_genesis 2846 $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
mp-wp_genesis 2847 } else {
mp-wp_genesis 2848 $lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
mp-wp_genesis 2849 }
mp-wp_genesis 2850 return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone );
mp-wp_genesis 2851 }
mp-wp_genesis 2852
mp-wp_genesis 2853 /**
mp-wp_genesis 2854 * Retrieve last post modified date depending on timezone.
mp-wp_genesis 2855 *
mp-wp_genesis 2856 * The server timezone is the default and is the difference between GMT and
mp-wp_genesis 2857 * server time. The 'blog' value is just when the last post was modified. The
mp-wp_genesis 2858 * 'gmt' is when the last post was modified in GMT time.
mp-wp_genesis 2859 *
mp-wp_genesis 2860 * @since 1.2.0
mp-wp_genesis 2861 * @uses $wpdb
mp-wp_genesis 2862 * @uses $blog_id
mp-wp_genesis 2863 * @uses apply_filters() Calls 'get_lastpostmodified' filter
mp-wp_genesis 2864 *
mp-wp_genesis 2865 * @global mixed $cache_lastpostmodified Stores the date the last post was modified
mp-wp_genesis 2866 *
mp-wp_genesis 2867 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
mp-wp_genesis 2868 * @return string The date the post was last modified.
mp-wp_genesis 2869 */
mp-wp_genesis 2870 function get_lastpostmodified($timezone = 'server') {
mp-wp_genesis 2871 global $cache_lastpostmodified, $wpdb, $blog_id;
mp-wp_genesis 2872 $add_seconds_server = date('Z');
mp-wp_genesis 2873 if ( !isset($cache_lastpostmodified[$blog_id][$timezone]) ) {
mp-wp_genesis 2874 switch(strtolower($timezone)) {
mp-wp_genesis 2875 case 'gmt':
mp-wp_genesis 2876 $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
mp-wp_genesis 2877 break;
mp-wp_genesis 2878 case 'blog':
mp-wp_genesis 2879 $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
mp-wp_genesis 2880 break;
mp-wp_genesis 2881 case 'server':
mp-wp_genesis 2882 $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
mp-wp_genesis 2883 break;
mp-wp_genesis 2884 }
mp-wp_genesis 2885 $lastpostdate = get_lastpostdate($timezone);
mp-wp_genesis 2886 if ( $lastpostdate > $lastpostmodified ) {
mp-wp_genesis 2887 $lastpostmodified = $lastpostdate;
mp-wp_genesis 2888 }
mp-wp_genesis 2889 $cache_lastpostmodified[$blog_id][$timezone] = $lastpostmodified;
mp-wp_genesis 2890 } else {
mp-wp_genesis 2891 $lastpostmodified = $cache_lastpostmodified[$blog_id][$timezone];
mp-wp_genesis 2892 }
mp-wp_genesis 2893 return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
mp-wp_genesis 2894 }
mp-wp_genesis 2895
mp-wp_genesis 2896 /**
mp-wp_genesis 2897 * Updates posts in cache.
mp-wp_genesis 2898 *
mp-wp_genesis 2899 * @usedby update_page_cache() Aliased by this function.
mp-wp_genesis 2900 *
mp-wp_genesis 2901 * @package WordPress
mp-wp_genesis 2902 * @subpackage Cache
mp-wp_genesis 2903 * @since 1.5.1
mp-wp_genesis 2904 *
mp-wp_genesis 2905 * @param array $posts Array of post objects
mp-wp_genesis 2906 */
mp-wp_genesis 2907 function update_post_cache(&$posts) {
mp-wp_genesis 2908 if ( !$posts )
mp-wp_genesis 2909 return;
mp-wp_genesis 2910
mp-wp_genesis 2911 foreach ( $posts as $post )
mp-wp_genesis 2912 wp_cache_add($post->ID, $post, 'posts');
mp-wp_genesis 2913 }
mp-wp_genesis 2914
mp-wp_genesis 2915 /**
mp-wp_genesis 2916 * Will clean the post in the cache.
mp-wp_genesis 2917 *
mp-wp_genesis 2918 * Cleaning means delete from the cache of the post. Will call to clean the term
mp-wp_genesis 2919 * object cache associated with the post ID.
mp-wp_genesis 2920 *
mp-wp_genesis 2921 * @package WordPress
mp-wp_genesis 2922 * @subpackage Cache
mp-wp_genesis 2923 * @since 2.0.0
mp-wp_genesis 2924 *
mp-wp_genesis 2925 * @uses do_action() Will call the 'clean_post_cache' hook action.
mp-wp_genesis 2926 *
mp-wp_genesis 2927 * @param int $id The Post ID in the cache to clean
mp-wp_genesis 2928 */
mp-wp_genesis 2929 function clean_post_cache($id) {
mp-wp_genesis 2930 global $_wp_suspend_cache_invalidation, $wpdb;
mp-wp_genesis 2931
mp-wp_genesis 2932 if ( !empty($_wp_suspend_cache_invalidation) )
mp-wp_genesis 2933 return;
mp-wp_genesis 2934
mp-wp_genesis 2935 $id = (int) $id;
mp-wp_genesis 2936
mp-wp_genesis 2937 wp_cache_delete($id, 'posts');
mp-wp_genesis 2938 wp_cache_delete($id, 'post_meta');
mp-wp_genesis 2939
mp-wp_genesis 2940 clean_object_term_cache($id, 'post');
mp-wp_genesis 2941
mp-wp_genesis 2942 wp_cache_delete( 'wp_get_archives', 'general' );
mp-wp_genesis 2943
mp-wp_genesis 2944 do_action('clean_post_cache', $id);
mp-wp_genesis 2945
mp-wp_genesis 2946 if ( $children = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d", $id) ) ) {
mp-wp_genesis 2947 foreach( $children as $cid )
mp-wp_genesis 2948 clean_post_cache( $cid );
mp-wp_genesis 2949 }
mp-wp_genesis 2950 }
mp-wp_genesis 2951
mp-wp_genesis 2952 /**
mp-wp_genesis 2953 * Alias of update_post_cache().
mp-wp_genesis 2954 *
mp-wp_genesis 2955 * @see update_post_cache() Posts and pages are the same, alias is intentional
mp-wp_genesis 2956 *
mp-wp_genesis 2957 * @package WordPress
mp-wp_genesis 2958 * @subpackage Cache
mp-wp_genesis 2959 * @since 1.5.1
mp-wp_genesis 2960 *
mp-wp_genesis 2961 * @param array $pages list of page objects
mp-wp_genesis 2962 */
mp-wp_genesis 2963 function update_page_cache(&$pages) {
mp-wp_genesis 2964 update_post_cache($pages);
mp-wp_genesis 2965 }
mp-wp_genesis 2966
mp-wp_genesis 2967 /**
mp-wp_genesis 2968 * Will clean the page in the cache.
mp-wp_genesis 2969 *
mp-wp_genesis 2970 * Clean (read: delete) page from cache that matches $id. Will also clean cache
mp-wp_genesis 2971 * associated with 'all_page_ids' and 'get_pages'.
mp-wp_genesis 2972 *
mp-wp_genesis 2973 * @package WordPress
mp-wp_genesis 2974 * @subpackage Cache
mp-wp_genesis 2975 * @since 2.0.0
mp-wp_genesis 2976 *
mp-wp_genesis 2977 * @uses do_action() Will call the 'clean_page_cache' hook action.
mp-wp_genesis 2978 *
mp-wp_genesis 2979 * @param int $id Page ID to clean
mp-wp_genesis 2980 */
mp-wp_genesis 2981 function clean_page_cache($id) {
mp-wp_genesis 2982 clean_post_cache($id);
mp-wp_genesis 2983
mp-wp_genesis 2984 wp_cache_delete( 'all_page_ids', 'posts' );
mp-wp_genesis 2985 wp_cache_delete( 'get_pages', 'posts' );
mp-wp_genesis 2986
mp-wp_genesis 2987 do_action('clean_page_cache', $id);
mp-wp_genesis 2988 }
mp-wp_genesis 2989
mp-wp_genesis 2990 /**
mp-wp_genesis 2991 * Call major cache updating functions for list of Post objects.
mp-wp_genesis 2992 *
mp-wp_genesis 2993 * @package WordPress
mp-wp_genesis 2994 * @subpackage Cache
mp-wp_genesis 2995 * @since 1.5.0
mp-wp_genesis 2996 *
mp-wp_genesis 2997 * @uses $wpdb
mp-wp_genesis 2998 * @uses update_post_cache()
mp-wp_genesis 2999 * @uses update_object_term_cache()
mp-wp_genesis 3000 * @uses update_postmeta_cache()
mp-wp_genesis 3001 *
mp-wp_genesis 3002 * @param array $posts Array of Post objects
mp-wp_genesis 3003 */
mp-wp_genesis 3004 function update_post_caches(&$posts) {
mp-wp_genesis 3005 // No point in doing all this work if we didn't match any posts.
mp-wp_genesis 3006 if ( !$posts )
mp-wp_genesis 3007 return;
mp-wp_genesis 3008
mp-wp_genesis 3009 update_post_cache($posts);
mp-wp_genesis 3010
mp-wp_genesis 3011 $post_ids = array();
mp-wp_genesis 3012
mp-wp_genesis 3013 for ($i = 0; $i < count($posts); $i++)
mp-wp_genesis 3014 $post_ids[] = $posts[$i]->ID;
mp-wp_genesis 3015
mp-wp_genesis 3016 update_object_term_cache($post_ids, 'post');
mp-wp_genesis 3017
mp-wp_genesis 3018 update_postmeta_cache($post_ids);
mp-wp_genesis 3019 }
mp-wp_genesis 3020
mp-wp_genesis 3021 /**
mp-wp_genesis 3022 * Updates metadata cache for list of post IDs.
mp-wp_genesis 3023 *
mp-wp_genesis 3024 * Performs SQL query to retrieve the metadata for the post IDs and updates the
mp-wp_genesis 3025 * metadata cache for the posts. Therefore, the functions, which call this
mp-wp_genesis 3026 * function, do not need to perform SQL queries on their own.
mp-wp_genesis 3027 *
mp-wp_genesis 3028 * @package WordPress
mp-wp_genesis 3029 * @subpackage Cache
mp-wp_genesis 3030 * @since 2.1.0
mp-wp_genesis 3031 *
mp-wp_genesis 3032 * @uses $wpdb
mp-wp_genesis 3033 *
mp-wp_genesis 3034 * @param array $post_ids List of post IDs.
mp-wp_genesis 3035 * @return bool|array Returns false if there is nothing to update or an array of metadata.
mp-wp_genesis 3036 */
mp-wp_genesis 3037 function update_postmeta_cache($post_ids) {
mp-wp_genesis 3038 global $wpdb;
mp-wp_genesis 3039
mp-wp_genesis 3040 if ( empty( $post_ids ) )
mp-wp_genesis 3041 return false;
mp-wp_genesis 3042
mp-wp_genesis 3043 if ( !is_array($post_ids) ) {
mp-wp_genesis 3044 $post_ids = preg_replace('|[^0-9,]|', '', $post_ids);
mp-wp_genesis 3045 $post_ids = explode(',', $post_ids);
mp-wp_genesis 3046 }
mp-wp_genesis 3047
mp-wp_genesis 3048 $post_ids = array_map('intval', $post_ids);
mp-wp_genesis 3049
mp-wp_genesis 3050 $ids = array();
mp-wp_genesis 3051 foreach ( (array) $post_ids as $id ) {
mp-wp_genesis 3052 if ( false === wp_cache_get($id, 'post_meta') )
mp-wp_genesis 3053 $ids[] = $id;
mp-wp_genesis 3054 }
mp-wp_genesis 3055
mp-wp_genesis 3056 if ( empty( $ids ) )
mp-wp_genesis 3057 return false;
mp-wp_genesis 3058
mp-wp_genesis 3059 // Get post-meta info
mp-wp_genesis 3060 $id_list = join(',', $ids);
mp-wp_genesis 3061 $cache = array();
mp-wp_genesis 3062 if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id IN ($id_list)", ARRAY_A) ) {
mp-wp_genesis 3063 foreach ( (array) $meta_list as $metarow) {
mp-wp_genesis 3064 $mpid = (int) $metarow['post_id'];
mp-wp_genesis 3065 $mkey = $metarow['meta_key'];
mp-wp_genesis 3066 $mval = $metarow['meta_value'];
mp-wp_genesis 3067
mp-wp_genesis 3068 // Force subkeys to be array type:
mp-wp_genesis 3069 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
mp-wp_genesis 3070 $cache[$mpid] = array();
mp-wp_genesis 3071 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
mp-wp_genesis 3072 $cache[$mpid][$mkey] = array();
mp-wp_genesis 3073
mp-wp_genesis 3074 // Add a value to the current pid/key:
mp-wp_genesis 3075 $cache[$mpid][$mkey][] = $mval;
mp-wp_genesis 3076 }
mp-wp_genesis 3077 }
mp-wp_genesis 3078
mp-wp_genesis 3079 foreach ( (array) $ids as $id ) {
mp-wp_genesis 3080 if ( ! isset($cache[$id]) )
mp-wp_genesis 3081 $cache[$id] = array();
mp-wp_genesis 3082 }
mp-wp_genesis 3083
mp-wp_genesis 3084 foreach ( (array) array_keys($cache) as $post)
mp-wp_genesis 3085 wp_cache_set($post, $cache[$post], 'post_meta');
mp-wp_genesis 3086
mp-wp_genesis 3087 return $cache;
mp-wp_genesis 3088 }
mp-wp_genesis 3089
mp-wp_genesis 3090 //
mp-wp_genesis 3091 // Hooks
mp-wp_genesis 3092 //
mp-wp_genesis 3093
mp-wp_genesis 3094 /**
mp-wp_genesis 3095 * Hook for managing future post transitions to published.
mp-wp_genesis 3096 *
mp-wp_genesis 3097 * @since 2.3.0
mp-wp_genesis 3098 * @access private
mp-wp_genesis 3099 * @uses $wpdb
mp-wp_genesis 3100 *
mp-wp_genesis 3101 * @param string $new_status New post status
mp-wp_genesis 3102 * @param string $old_status Previous post status
mp-wp_genesis 3103 * @param object $post Object type containing the post information
mp-wp_genesis 3104 */
mp-wp_genesis 3105 function _transition_post_status($new_status, $old_status, $post) {
mp-wp_genesis 3106 global $wpdb;
mp-wp_genesis 3107
mp-wp_genesis 3108 if ( $old_status != 'publish' && $new_status == 'publish' ) {
mp-wp_genesis 3109 // Reset GUID if transitioning to publish and it is empty
mp-wp_genesis 3110 if ( '' == get_the_guid($post->ID) )
mp-wp_genesis 3111 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) );
mp-wp_genesis 3112 do_action('private_to_published', $post->ID); // Deprecated, use private_to_publish
mp-wp_genesis 3113 }
mp-wp_genesis 3114
mp-wp_genesis 3115 // Always clears the hook in case the post status bounced from future to draft.
mp-wp_genesis 3116 wp_clear_scheduled_hook('publish_future_post', $post->ID);
mp-wp_genesis 3117 }
mp-wp_genesis 3118
mp-wp_genesis 3119 /**
mp-wp_genesis 3120 * Hook used to schedule publication for a post marked for the future.
mp-wp_genesis 3121 *
mp-wp_genesis 3122 * The $post properties used and must exist are 'ID' and 'post_date_gmt'.
mp-wp_genesis 3123 *
mp-wp_genesis 3124 * @since 2.3.0
mp-wp_genesis 3125 *
mp-wp_genesis 3126 * @param int $deprecated Not Used. Can be set to null.
mp-wp_genesis 3127 * @param object $post Object type containing the post information
mp-wp_genesis 3128 */
mp-wp_genesis 3129 function _future_post_hook($deprecated = '', $post) {
mp-wp_genesis 3130 wp_clear_scheduled_hook( 'publish_future_post', $post->ID );
mp-wp_genesis 3131 wp_schedule_single_event(strtotime($post->post_date_gmt. ' GMT'), 'publish_future_post', array($post->ID));
mp-wp_genesis 3132 }
mp-wp_genesis 3133
mp-wp_genesis 3134 /**
mp-wp_genesis 3135 * Hook to schedule pings and enclosures when a post is published.
mp-wp_genesis 3136 *
mp-wp_genesis 3137 * @since 2.3.0
mp-wp_genesis 3138 * @uses $wpdb
mp-wp_genesis 3139 * @uses XMLRPC_REQUEST
mp-wp_genesis 3140 * @uses APP_REQUEST
mp-wp_genesis 3141 * @uses do_action Calls 'xmlprc_publish_post' action if XMLRPC_REQUEST is defined. Calls 'app_publish_post'
mp-wp_genesis 3142 * action if APP_REQUEST is defined.
mp-wp_genesis 3143 *
mp-wp_genesis 3144 * @param int $post_id The ID in the database table of the post being published
mp-wp_genesis 3145 */
mp-wp_genesis 3146 function _publish_post_hook($post_id) {
mp-wp_genesis 3147 global $wpdb;
mp-wp_genesis 3148
mp-wp_genesis 3149 if ( defined('XMLRPC_REQUEST') )
mp-wp_genesis 3150 do_action('xmlrpc_publish_post', $post_id);
mp-wp_genesis 3151 if ( defined('APP_REQUEST') )
mp-wp_genesis 3152 do_action('app_publish_post', $post_id);
mp-wp_genesis 3153
mp-wp_genesis 3154 if ( defined('WP_IMPORTING') )
mp-wp_genesis 3155 return;
mp-wp_genesis 3156
mp-wp_genesis 3157 $data = array( 'post_id' => $post_id, 'meta_value' => '1' );
mp-wp_genesis 3158 if ( get_option('default_pingback_flag') )
mp-wp_genesis 3159 $wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_pingme' ) );
mp-wp_genesis 3160 $wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_encloseme' ) );
mp-wp_genesis 3161 wp_schedule_single_event(time(), 'do_pings');
mp-wp_genesis 3162 }
mp-wp_genesis 3163
mp-wp_genesis 3164 /**
mp-wp_genesis 3165 * Hook used to prevent page/post cache and rewrite rules from staying dirty.
mp-wp_genesis 3166 *
mp-wp_genesis 3167 * Does two things. If the post is a page and has a template then it will
mp-wp_genesis 3168 * update/add that template to the meta. For both pages and posts, it will clean
mp-wp_genesis 3169 * the post cache to make sure that the cache updates to the changes done
mp-wp_genesis 3170 * recently. For pages, the rewrite rules of WordPress are flushed to allow for
mp-wp_genesis 3171 * any changes.
mp-wp_genesis 3172 *
mp-wp_genesis 3173 * The $post parameter, only uses 'post_type' property and 'page_template'
mp-wp_genesis 3174 * property.
mp-wp_genesis 3175 *
mp-wp_genesis 3176 * @since 2.3.0
mp-wp_genesis 3177 * @uses $wp_rewrite Flushes Rewrite Rules.
mp-wp_genesis 3178 *
mp-wp_genesis 3179 * @param int $post_id The ID in the database table for the $post
mp-wp_genesis 3180 * @param object $post Object type containing the post information
mp-wp_genesis 3181 */
mp-wp_genesis 3182 function _save_post_hook($post_id, $post) {
mp-wp_genesis 3183 if ( $post->post_type == 'page' ) {
mp-wp_genesis 3184 clean_page_cache($post_id);
mp-wp_genesis 3185 // Avoid flushing rules for every post during import.
mp-wp_genesis 3186 if ( !defined('WP_IMPORTING') ) {
mp-wp_genesis 3187 global $wp_rewrite;
mp-wp_genesis 3188 $wp_rewrite->flush_rules();
mp-wp_genesis 3189 }
mp-wp_genesis 3190 } else {
mp-wp_genesis 3191 clean_post_cache($post_id);
mp-wp_genesis 3192 }
mp-wp_genesis 3193 }
mp-wp_genesis 3194
mp-wp_genesis 3195 /**
mp-wp_genesis 3196 * Retrieve post ancestors and append to post ancestors property.
mp-wp_genesis 3197 *
mp-wp_genesis 3198 * Will only retrieve ancestors once, if property is already set, then nothing
mp-wp_genesis 3199 * will be done. If there is not a parent post, or post ID and post parent ID
mp-wp_genesis 3200 * are the same then nothing will be done.
mp-wp_genesis 3201 *
mp-wp_genesis 3202 * The parameter is passed by reference, so nothing needs to be returned. The
mp-wp_genesis 3203 * property will be updated and can be referenced after the function is
mp-wp_genesis 3204 * complete. The post parent will be an ancestor and the parent of the post
mp-wp_genesis 3205 * parent will be an ancestor. There will only be two ancestors at the most.
mp-wp_genesis 3206 *
mp-wp_genesis 3207 * @access private
mp-wp_genesis 3208 * @since unknown
mp-wp_genesis 3209 * @uses $wpdb
mp-wp_genesis 3210 *
mp-wp_genesis 3211 * @param object $_post Post data.
mp-wp_genesis 3212 * @return null When nothing needs to be done.
mp-wp_genesis 3213 */
mp-wp_genesis 3214 function _get_post_ancestors(&$_post) {
mp-wp_genesis 3215 global $wpdb;
mp-wp_genesis 3216
mp-wp_genesis 3217 if ( isset($_post->ancestors) )
mp-wp_genesis 3218 return;
mp-wp_genesis 3219
mp-wp_genesis 3220 $_post->ancestors = array();
mp-wp_genesis 3221
mp-wp_genesis 3222 if ( empty($_post->post_parent) || $_post->ID == $_post->post_parent )
mp-wp_genesis 3223 return;
mp-wp_genesis 3224
mp-wp_genesis 3225 $id = $_post->ancestors[] = $_post->post_parent;
mp-wp_genesis 3226 while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) {
mp-wp_genesis 3227 if ( $id == $ancestor )
mp-wp_genesis 3228 break;
mp-wp_genesis 3229 $id = $_post->ancestors[] = $ancestor;
mp-wp_genesis 3230 }
mp-wp_genesis 3231 }
mp-wp_genesis 3232
mp-wp_genesis 3233 /**
mp-wp_genesis 3234 * Determines which fields of posts are to be saved in revisions.
mp-wp_genesis 3235 *
mp-wp_genesis 3236 * Does two things. If passed a post *array*, it will return a post array ready
mp-wp_genesis 3237 * to be insterted into the posts table as a post revision. Otherwise, returns
mp-wp_genesis 3238 * an array whose keys are the post fields to be saved for post revisions.
mp-wp_genesis 3239 *
mp-wp_genesis 3240 * @package WordPress
mp-wp_genesis 3241 * @subpackage Post_Revisions
mp-wp_genesis 3242 * @since 2.6.0
mp-wp_genesis 3243 * @access private
mp-wp_genesis 3244 *
mp-wp_genesis 3245 * @param array $post Optional a post array to be processed for insertion as a post revision.
mp-wp_genesis 3246 * @param bool $autosave optional Is the revision an autosave?
mp-wp_genesis 3247 * @return array Post array ready to be inserted as a post revision or array of fields that can be versioned.
mp-wp_genesis 3248 */
mp-wp_genesis 3249 function _wp_post_revision_fields( $post = null, $autosave = false ) {
mp-wp_genesis 3250 static $fields = false;
mp-wp_genesis 3251
mp-wp_genesis 3252 if ( !$fields ) {
mp-wp_genesis 3253 // Allow these to be versioned
mp-wp_genesis 3254 $fields = array(
mp-wp_genesis 3255 'post_title' => __( 'Title' ),
mp-wp_genesis 3256 'post_content' => __( 'Content' ),
mp-wp_genesis 3257 'post_excerpt' => __( 'Excerpt' ),
mp-wp_genesis 3258 );
mp-wp_genesis 3259
mp-wp_genesis 3260 // Runs only once
mp-wp_genesis 3261 $fields = apply_filters( '_wp_post_revision_fields', $fields );
mp-wp_genesis 3262
mp-wp_genesis 3263 // WP uses these internally either in versioning or elsewhere - they cannot be versioned
mp-wp_genesis 3264 foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect )
mp-wp_genesis 3265 unset( $fields[$protect] );
mp-wp_genesis 3266 }
mp-wp_genesis 3267
mp-wp_genesis 3268 if ( !is_array($post) )
mp-wp_genesis 3269 return $fields;
mp-wp_genesis 3270
mp-wp_genesis 3271 $return = array();
mp-wp_genesis 3272 foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
mp-wp_genesis 3273 $return[$field] = $post[$field];
mp-wp_genesis 3274
mp-wp_genesis 3275 $return['post_parent'] = $post['ID'];
mp-wp_genesis 3276 $return['post_status'] = 'inherit';
mp-wp_genesis 3277 $return['post_type'] = 'revision';
mp-wp_genesis 3278 $return['post_name'] = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";
mp-wp_genesis 3279 $return['post_date'] = isset($post['post_modified']) ? $post['post_modified'] : '';
mp-wp_genesis 3280 $return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
mp-wp_genesis 3281
mp-wp_genesis 3282 return $return;
mp-wp_genesis 3283 }
mp-wp_genesis 3284
mp-wp_genesis 3285 /**
mp-wp_genesis 3286 * Saves an already existing post as a post revision.
mp-wp_genesis 3287 *
mp-wp_genesis 3288 * Typically used immediately prior to post updates.
mp-wp_genesis 3289 *
mp-wp_genesis 3290 * @package WordPress
mp-wp_genesis 3291 * @subpackage Post_Revisions
mp-wp_genesis 3292 * @since 2.6.0
mp-wp_genesis 3293 *
mp-wp_genesis 3294 * @uses _wp_put_post_revision()
mp-wp_genesis 3295 *
mp-wp_genesis 3296 * @param int $post_id The ID of the post to save as a revision.
mp-wp_genesis 3297 * @return mixed Null or 0 if error, new revision ID, if success.
mp-wp_genesis 3298 */
mp-wp_genesis 3299 function wp_save_post_revision( $post_id ) {
mp-wp_genesis 3300 // We do autosaves manually with wp_create_post_autosave()
mp-wp_genesis 3301 if ( @constant( 'DOING_AUTOSAVE' ) )
mp-wp_genesis 3302 return;
mp-wp_genesis 3303
mp-wp_genesis 3304 // WP_POST_REVISIONS = 0, false
mp-wp_genesis 3305 if ( !constant('WP_POST_REVISIONS') )
mp-wp_genesis 3306 return;
mp-wp_genesis 3307
mp-wp_genesis 3308 if ( !$post = get_post( $post_id, ARRAY_A ) )
mp-wp_genesis 3309 return;
mp-wp_genesis 3310
mp-wp_genesis 3311 if ( !in_array( $post['post_type'], array( 'post', 'page' ) ) )
mp-wp_genesis 3312 return;
mp-wp_genesis 3313
mp-wp_genesis 3314 $return = _wp_put_post_revision( $post );
mp-wp_genesis 3315
mp-wp_genesis 3316 // WP_POST_REVISIONS = true (default), -1
mp-wp_genesis 3317 if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 )
mp-wp_genesis 3318 return $return;
mp-wp_genesis 3319
mp-wp_genesis 3320 // all revisions and (possibly) one autosave
mp-wp_genesis 3321 $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
mp-wp_genesis 3322
mp-wp_genesis 3323 // WP_POST_REVISIONS = (int) (# of autasaves to save)
mp-wp_genesis 3324 $delete = count($revisions) - WP_POST_REVISIONS;
mp-wp_genesis 3325
mp-wp_genesis 3326 if ( $delete < 1 )
mp-wp_genesis 3327 return $return;
mp-wp_genesis 3328
mp-wp_genesis 3329 $revisions = array_slice( $revisions, 0, $delete );
mp-wp_genesis 3330
mp-wp_genesis 3331 for ( $i = 0; isset($revisions[$i]); $i++ ) {
mp-wp_genesis 3332 if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) )
mp-wp_genesis 3333 continue;
mp-wp_genesis 3334 wp_delete_post_revision( $revisions[$i]->ID );
mp-wp_genesis 3335 }
mp-wp_genesis 3336
mp-wp_genesis 3337 return $return;
mp-wp_genesis 3338 }
mp-wp_genesis 3339
mp-wp_genesis 3340 /**
mp-wp_genesis 3341 * Retrieve the autosaved data of the specified post.
mp-wp_genesis 3342 *
mp-wp_genesis 3343 * Returns a post object containing the information that was autosaved for the
mp-wp_genesis 3344 * specified post.
mp-wp_genesis 3345 *
mp-wp_genesis 3346 * @package WordPress
mp-wp_genesis 3347 * @subpackage Post_Revisions
mp-wp_genesis 3348 * @since 2.6.0
mp-wp_genesis 3349 *
mp-wp_genesis 3350 * @param int $post_id The post ID.
mp-wp_genesis 3351 * @return object|bool The autosaved data or false on failure or when no autosave exists.
mp-wp_genesis 3352 */
mp-wp_genesis 3353 function wp_get_post_autosave( $post_id ) {
mp-wp_genesis 3354
mp-wp_genesis 3355 if ( !$post = get_post( $post_id ) )
mp-wp_genesis 3356 return false;
mp-wp_genesis 3357
mp-wp_genesis 3358 $q = array(
mp-wp_genesis 3359 'name' => "{$post->ID}-autosave",
mp-wp_genesis 3360 'post_parent' => $post->ID,
mp-wp_genesis 3361 'post_type' => 'revision',
mp-wp_genesis 3362 'post_status' => 'inherit'
mp-wp_genesis 3363 );
mp-wp_genesis 3364
mp-wp_genesis 3365 // Use WP_Query so that the result gets cached
mp-wp_genesis 3366 $autosave_query = new WP_Query;
mp-wp_genesis 3367
mp-wp_genesis 3368 add_action( 'parse_query', '_wp_get_post_autosave_hack' );
mp-wp_genesis 3369 $autosave = $autosave_query->query( $q );
mp-wp_genesis 3370 remove_action( 'parse_query', '_wp_get_post_autosave_hack' );
mp-wp_genesis 3371
mp-wp_genesis 3372 if ( $autosave && is_array($autosave) && is_object($autosave[0]) )
mp-wp_genesis 3373 return $autosave[0];
mp-wp_genesis 3374
mp-wp_genesis 3375 return false;
mp-wp_genesis 3376 }
mp-wp_genesis 3377
mp-wp_genesis 3378 /**
mp-wp_genesis 3379 * Internally used to hack WP_Query into submission.
mp-wp_genesis 3380 *
mp-wp_genesis 3381 * @package WordPress
mp-wp_genesis 3382 * @subpackage Post_Revisions
mp-wp_genesis 3383 * @since 2.6.0
mp-wp_genesis 3384 *
mp-wp_genesis 3385 * @param object $query WP_Query object
mp-wp_genesis 3386 */
mp-wp_genesis 3387 function _wp_get_post_autosave_hack( $query ) {
mp-wp_genesis 3388 $query->is_single = false;
mp-wp_genesis 3389 }
mp-wp_genesis 3390
mp-wp_genesis 3391 /**
mp-wp_genesis 3392 * Determines if the specified post is a revision.
mp-wp_genesis 3393 *
mp-wp_genesis 3394 * @package WordPress
mp-wp_genesis 3395 * @subpackage Post_Revisions
mp-wp_genesis 3396 * @since 2.6.0
mp-wp_genesis 3397 *
mp-wp_genesis 3398 * @param int|object $post Post ID or post object.
mp-wp_genesis 3399 * @return bool|int False if not a revision, ID of revision's parent otherwise.
mp-wp_genesis 3400 */
mp-wp_genesis 3401 function wp_is_post_revision( $post ) {
mp-wp_genesis 3402 if ( !$post = wp_get_post_revision( $post ) )
mp-wp_genesis 3403 return false;
mp-wp_genesis 3404 return (int) $post->post_parent;
mp-wp_genesis 3405 }
mp-wp_genesis 3406
mp-wp_genesis 3407 /**
mp-wp_genesis 3408 * Determines if the specified post is an autosave.
mp-wp_genesis 3409 *
mp-wp_genesis 3410 * @package WordPress
mp-wp_genesis 3411 * @subpackage Post_Revisions
mp-wp_genesis 3412 * @since 2.6.0
mp-wp_genesis 3413 *
mp-wp_genesis 3414 * @param int|object $post Post ID or post object.
mp-wp_genesis 3415 * @return bool|int False if not a revision, ID of autosave's parent otherwise
mp-wp_genesis 3416 */
mp-wp_genesis 3417 function wp_is_post_autosave( $post ) {
mp-wp_genesis 3418 if ( !$post = wp_get_post_revision( $post ) )
mp-wp_genesis 3419 return false;
mp-wp_genesis 3420 if ( "{$post->post_parent}-autosave" !== $post->post_name )
mp-wp_genesis 3421 return false;
mp-wp_genesis 3422 return (int) $post->post_parent;
mp-wp_genesis 3423 }
mp-wp_genesis 3424
mp-wp_genesis 3425 /**
mp-wp_genesis 3426 * Inserts post data into the posts table as a post revision.
mp-wp_genesis 3427 *
mp-wp_genesis 3428 * @package WordPress
mp-wp_genesis 3429 * @subpackage Post_Revisions
mp-wp_genesis 3430 * @since 2.6.0
mp-wp_genesis 3431 *
mp-wp_genesis 3432 * @uses wp_insert_post()
mp-wp_genesis 3433 *
mp-wp_genesis 3434 * @param int|object|array $post Post ID, post object OR post array.
mp-wp_genesis 3435 * @param bool $autosave Optional. Is the revision an autosave?
mp-wp_genesis 3436 * @return mixed Null or 0 if error, new revision ID if success.
mp-wp_genesis 3437 */
mp-wp_genesis 3438 function _wp_put_post_revision( $post = null, $autosave = false ) {
mp-wp_genesis 3439 if ( is_object($post) )
mp-wp_genesis 3440 $post = get_object_vars( $post );
mp-wp_genesis 3441 elseif ( !is_array($post) )
mp-wp_genesis 3442 $post = get_post($post, ARRAY_A);
mp-wp_genesis 3443 if ( !$post || empty($post['ID']) )
mp-wp_genesis 3444 return;
mp-wp_genesis 3445
mp-wp_genesis 3446 if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
mp-wp_genesis 3447 return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
mp-wp_genesis 3448
mp-wp_genesis 3449 $post = _wp_post_revision_fields( $post, $autosave );
mp-wp_genesis 3450
mp-wp_genesis 3451 $revision_id = wp_insert_post( $post );
mp-wp_genesis 3452 if ( is_wp_error($revision_id) )
mp-wp_genesis 3453 return $revision_id;
mp-wp_genesis 3454
mp-wp_genesis 3455 if ( $revision_id )
mp-wp_genesis 3456 do_action( '_wp_put_post_revision', $revision_id );
mp-wp_genesis 3457 return $revision_id;
mp-wp_genesis 3458 }
mp-wp_genesis 3459
mp-wp_genesis 3460 /**
mp-wp_genesis 3461 * Gets a post revision.
mp-wp_genesis 3462 *
mp-wp_genesis 3463 * @package WordPress
mp-wp_genesis 3464 * @subpackage Post_Revisions
mp-wp_genesis 3465 * @since 2.6.0
mp-wp_genesis 3466 *
mp-wp_genesis 3467 * @uses get_post()
mp-wp_genesis 3468 *
mp-wp_genesis 3469 * @param int|object $post Post ID or post object
mp-wp_genesis 3470 * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
mp-wp_genesis 3471 * @param string $filter Optional sanitation filter. @see sanitize_post()
mp-wp_genesis 3472 * @return mixed Null if error or post object if success
mp-wp_genesis 3473 */
mp-wp_genesis 3474 function &wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
mp-wp_genesis 3475 $null = null;
mp-wp_genesis 3476 if ( !$revision = get_post( $post, OBJECT, $filter ) )
mp-wp_genesis 3477 return $revision;
mp-wp_genesis 3478 if ( 'revision' !== $revision->post_type )
mp-wp_genesis 3479 return $null;
mp-wp_genesis 3480
mp-wp_genesis 3481 if ( $output == OBJECT ) {
mp-wp_genesis 3482 return $revision;
mp-wp_genesis 3483 } elseif ( $output == ARRAY_A ) {
mp-wp_genesis 3484 $_revision = get_object_vars($revision);
mp-wp_genesis 3485 return $_revision;
mp-wp_genesis 3486 } elseif ( $output == ARRAY_N ) {
mp-wp_genesis 3487 $_revision = array_values(get_object_vars($revision));
mp-wp_genesis 3488 return $_revision;
mp-wp_genesis 3489 }
mp-wp_genesis 3490
mp-wp_genesis 3491 return $revision;
mp-wp_genesis 3492 }
mp-wp_genesis 3493
mp-wp_genesis 3494 /**
mp-wp_genesis 3495 * Restores a post to the specified revision.
mp-wp_genesis 3496 *
mp-wp_genesis 3497 * Can restore a past using all fields of the post revision, or only selected
mp-wp_genesis 3498 * fields.
mp-wp_genesis 3499 *
mp-wp_genesis 3500 * @package WordPress
mp-wp_genesis 3501 * @subpackage Post_Revisions
mp-wp_genesis 3502 * @since 2.6.0
mp-wp_genesis 3503 *
mp-wp_genesis 3504 * @uses wp_get_post_revision()
mp-wp_genesis 3505 * @uses wp_update_post()
mp-wp_genesis 3506 *
mp-wp_genesis 3507 * @param int|object $revision_id Revision ID or revision object.
mp-wp_genesis 3508 * @param array $fields Optional. What fields to restore from. Defaults to all.
mp-wp_genesis 3509 * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
mp-wp_genesis 3510 */
mp-wp_genesis 3511 function wp_restore_post_revision( $revision_id, $fields = null ) {
mp-wp_genesis 3512 if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
mp-wp_genesis 3513 return $revision;
mp-wp_genesis 3514
mp-wp_genesis 3515 if ( !is_array( $fields ) )
mp-wp_genesis 3516 $fields = array_keys( _wp_post_revision_fields() );
mp-wp_genesis 3517
mp-wp_genesis 3518 $update = array();
mp-wp_genesis 3519 foreach( array_intersect( array_keys( $revision ), $fields ) as $field )
mp-wp_genesis 3520 $update[$field] = $revision[$field];
mp-wp_genesis 3521
mp-wp_genesis 3522 if ( !$update )
mp-wp_genesis 3523 return false;
mp-wp_genesis 3524
mp-wp_genesis 3525 $update['ID'] = $revision['post_parent'];
mp-wp_genesis 3526
mp-wp_genesis 3527 $post_id = wp_update_post( $update );
mp-wp_genesis 3528 if ( is_wp_error( $post_id ) )
mp-wp_genesis 3529 return $post_id;
mp-wp_genesis 3530
mp-wp_genesis 3531 if ( $post_id )
mp-wp_genesis 3532 do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
mp-wp_genesis 3533
mp-wp_genesis 3534 return $post_id;
mp-wp_genesis 3535 }
mp-wp_genesis 3536
mp-wp_genesis 3537 /**
mp-wp_genesis 3538 * Deletes a revision.
mp-wp_genesis 3539 *
mp-wp_genesis 3540 * Deletes the row from the posts table corresponding to the specified revision.
mp-wp_genesis 3541 *
mp-wp_genesis 3542 * @package WordPress
mp-wp_genesis 3543 * @subpackage Post_Revisions
mp-wp_genesis 3544 * @since 2.6.0
mp-wp_genesis 3545 *
mp-wp_genesis 3546 * @uses wp_get_post_revision()
mp-wp_genesis 3547 * @uses wp_delete_post()
mp-wp_genesis 3548 *
mp-wp_genesis 3549 * @param int|object $revision_id Revision ID or revision object.
mp-wp_genesis 3550 * @param array $fields Optional. What fields to restore from. Defaults to all.
mp-wp_genesis 3551 * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
mp-wp_genesis 3552 */
mp-wp_genesis 3553 function wp_delete_post_revision( $revision_id ) {
mp-wp_genesis 3554 if ( !$revision = wp_get_post_revision( $revision_id ) )
mp-wp_genesis 3555 return $revision;
mp-wp_genesis 3556
mp-wp_genesis 3557 $delete = wp_delete_post( $revision->ID );
mp-wp_genesis 3558 if ( is_wp_error( $delete ) )
mp-wp_genesis 3559 return $delete;
mp-wp_genesis 3560
mp-wp_genesis 3561 if ( $delete )
mp-wp_genesis 3562 do_action( 'wp_delete_post_revision', $revision->ID, $revision );
mp-wp_genesis 3563
mp-wp_genesis 3564 return $delete;
mp-wp_genesis 3565 }
mp-wp_genesis 3566
mp-wp_genesis 3567 /**
mp-wp_genesis 3568 * Returns all revisions of specified post.
mp-wp_genesis 3569 *
mp-wp_genesis 3570 * @package WordPress
mp-wp_genesis 3571 * @subpackage Post_Revisions
mp-wp_genesis 3572 * @since 2.6.0
mp-wp_genesis 3573 *
mp-wp_genesis 3574 * @uses get_children()
mp-wp_genesis 3575 *
mp-wp_genesis 3576 * @param int|object $post_id Post ID or post object
mp-wp_genesis 3577 * @return array empty if no revisions
mp-wp_genesis 3578 */
mp-wp_genesis 3579 function wp_get_post_revisions( $post_id = 0, $args = null ) {
mp-wp_genesis 3580 if ( !constant('WP_POST_REVISIONS') )
mp-wp_genesis 3581 return array();
mp-wp_genesis 3582 if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) )
mp-wp_genesis 3583 return array();
mp-wp_genesis 3584
mp-wp_genesis 3585 $defaults = array( 'order' => 'DESC', 'orderby' => 'date' );
mp-wp_genesis 3586 $args = wp_parse_args( $args, $defaults );
mp-wp_genesis 3587 $args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) );
mp-wp_genesis 3588
mp-wp_genesis 3589 if ( !$revisions = get_children( $args ) )
mp-wp_genesis 3590 return array();
mp-wp_genesis 3591 return $revisions;
mp-wp_genesis 3592 }
mp-wp_genesis 3593
mp-wp_genesis 3594 function _set_preview($post) {
mp-wp_genesis 3595
mp-wp_genesis 3596 if ( ! is_object($post) )
mp-wp_genesis 3597 return $post;
mp-wp_genesis 3598
mp-wp_genesis 3599 $preview = wp_get_post_autosave($post->ID);
mp-wp_genesis 3600
mp-wp_genesis 3601 if ( ! is_object($preview) )
mp-wp_genesis 3602 return $post;
mp-wp_genesis 3603
mp-wp_genesis 3604 $preview = sanitize_post($preview);
mp-wp_genesis 3605
mp-wp_genesis 3606 $post->post_content = $preview->post_content;
mp-wp_genesis 3607 $post->post_title = $preview->post_title;
mp-wp_genesis 3608 $post->post_excerpt = $preview->post_excerpt;
mp-wp_genesis 3609
mp-wp_genesis 3610 return $post;
mp-wp_genesis 3611 }
mp-wp_genesis 3612
mp-wp_genesis 3613 function _show_post_preview() {
mp-wp_genesis 3614
mp-wp_genesis 3615 if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
mp-wp_genesis 3616 $id = (int) $_GET['preview_id'];
mp-wp_genesis 3617
mp-wp_genesis 3618 if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
mp-wp_genesis 3619 wp_die( __('You do not have permission to preview drafts.') );
mp-wp_genesis 3620
mp-wp_genesis 3621 add_filter('the_preview', '_set_preview');
mp-wp_genesis 3622 }
mp-wp_genesis 3623 }