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_remove-tiny... 844 $value = format_to_edit($value);
mp-wp_genesis 845 } else {
mp-wp_genesis 846 $value = attribute_escape($value);
mp-wp_genesis 847 }
mp-wp_genesis 848 } else if ( 'db' == $context ) {
mp-wp_genesis 849 if ( $prefixed ) {
mp-wp_genesis 850 $value = apply_filters("pre_$field", $value);
mp-wp_genesis 851 $value = apply_filters("${field_no_prefix}_save_pre", $value);
mp-wp_genesis 852 } else {
mp-wp_genesis 853 $value = apply_filters("pre_post_$field", $value);
mp-wp_genesis 854 $value = apply_filters("${field}_pre", $value);
mp-wp_genesis 855 }
mp-wp_genesis 856 } else {
mp-wp_genesis 857 // Use display filters by default.
mp-wp_genesis 858 if ( $prefixed )
mp-wp_genesis 859 $value = apply_filters($field, $value, $post_id, $context);
mp-wp_genesis 860 else
mp-wp_genesis 861 $value = apply_filters("post_$field", $value, $post_id, $context);
mp-wp_genesis 862 }
mp-wp_genesis 863
mp-wp_genesis 864 if ( 'attribute' == $context )
mp-wp_genesis 865 $value = attribute_escape($value);
mp-wp_genesis 866 else if ( 'js' == $context )
mp-wp_genesis 867 $value = js_escape($value);
mp-wp_genesis 868
mp-wp_genesis 869 return $value;
mp-wp_genesis 870 }
mp-wp_genesis 871
mp-wp_genesis 872 /**
mp-wp_genesis 873 * Make a post sticky.
mp-wp_genesis 874 *
mp-wp_genesis 875 * Sticky posts should be displayed at the top of the front page.
mp-wp_genesis 876 *
mp-wp_genesis 877 * @since 2.7.0
mp-wp_genesis 878 *
mp-wp_genesis 879 * @param int $post_id Post ID.
mp-wp_genesis 880 */
mp-wp_genesis 881 function stick_post($post_id) {
mp-wp_genesis 882 $stickies = get_option('sticky_posts');
mp-wp_genesis 883
mp-wp_genesis 884 if ( !is_array($stickies) )
mp-wp_genesis 885 $stickies = array($post_id);
mp-wp_genesis 886
mp-wp_genesis 887 if ( ! in_array($post_id, $stickies) )
mp-wp_genesis 888 $stickies[] = $post_id;
mp-wp_genesis 889
mp-wp_genesis 890 update_option('sticky_posts', $stickies);
mp-wp_genesis 891 }
mp-wp_genesis 892
mp-wp_genesis 893 /**
mp-wp_genesis 894 * Unstick a post.
mp-wp_genesis 895 *
mp-wp_genesis 896 * Sticky posts should be displayed at the top of the front page.
mp-wp_genesis 897 *
mp-wp_genesis 898 * @since 2.7.0
mp-wp_genesis 899 *
mp-wp_genesis 900 * @param int $post_id Post ID.
mp-wp_genesis 901 */
mp-wp_genesis 902 function unstick_post($post_id) {
mp-wp_genesis 903 $stickies = get_option('sticky_posts');
mp-wp_genesis 904
mp-wp_genesis 905 if ( !is_array($stickies) )
mp-wp_genesis 906 return;
mp-wp_genesis 907
mp-wp_genesis 908 if ( ! in_array($post_id, $stickies) )
mp-wp_genesis 909 return;
mp-wp_genesis 910
mp-wp_genesis 911 $offset = array_search($post_id, $stickies);
mp-wp_genesis 912 if ( false === $offset )
mp-wp_genesis 913 return;
mp-wp_genesis 914
mp-wp_genesis 915 array_splice($stickies, $offset, 1);
mp-wp_genesis 916
mp-wp_genesis 917 update_option('sticky_posts', $stickies);
mp-wp_genesis 918 }
mp-wp_genesis 919
mp-wp_genesis 920 /**
mp-wp_genesis 921 * Count number of posts of a post type and is user has permissions to view.
mp-wp_genesis 922 *
mp-wp_genesis 923 * This function provides an efficient method of finding the amount of post's
mp-wp_genesis 924 * type a blog has. Another method is to count the amount of items in
mp-wp_genesis 925 * get_posts(), but that method has a lot of overhead with doing so. Therefore,
mp-wp_genesis 926 * when developing for 2.5+, use this function instead.
mp-wp_genesis 927 *
mp-wp_genesis 928 * The $perm parameter checks for 'readable' value and if the user can read
mp-wp_genesis 929 * private posts, it will display that for the user that is signed in.
mp-wp_genesis 930 *
mp-wp_genesis 931 * @since 2.5.0
mp-wp_genesis 932 * @link http://codex.wordpress.org/Template_Tags/wp_count_posts
mp-wp_genesis 933 *
mp-wp_genesis 934 * @param string $type Optional. Post type to retrieve count
mp-wp_genesis 935 * @param string $perm Optional. 'readable' or empty.
mp-wp_genesis 936 * @return object Number of posts for each status
mp-wp_genesis 937 */
mp-wp_genesis 938 function wp_count_posts( $type = 'post', $perm = '' ) {
mp-wp_genesis 939 global $wpdb;
mp-wp_genesis 940
mp-wp_genesis 941 $user = wp_get_current_user();
mp-wp_genesis 942
mp-wp_genesis 943 $cache_key = $type;
mp-wp_genesis 944
mp-wp_genesis 945 $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
mp-wp_genesis 946 if ( 'readable' == $perm && is_user_logged_in() ) {
mp-wp_genesis 947 if ( !current_user_can("read_private_{$type}s") ) {
mp-wp_genesis 948 $cache_key .= '_' . $perm . '_' . $user->ID;
mp-wp_genesis 949 $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
mp-wp_genesis 950 }
mp-wp_genesis 951 }
mp-wp_genesis 952 $query .= ' GROUP BY post_status';
mp-wp_genesis 953
mp-wp_genesis 954 $count = wp_cache_get($cache_key, 'counts');
mp-wp_genesis 955 if ( false !== $count )
mp-wp_genesis 956 return $count;
mp-wp_genesis 957
mp-wp_genesis 958 $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
mp-wp_genesis 959
mp-wp_genesis 960 $stats = array( );
mp-wp_genesis 961 foreach( (array) $count as $row_num => $row ) {
mp-wp_genesis 962 $stats[$row['post_status']] = $row['num_posts'];
mp-wp_genesis 963 }
mp-wp_genesis 964
mp-wp_genesis 965 $stats = (object) $stats;
mp-wp_genesis 966 wp_cache_set($cache_key, $stats, 'counts');
mp-wp_genesis 967
mp-wp_genesis 968 return $stats;
mp-wp_genesis 969 }
mp-wp_genesis 970
mp-wp_genesis 971
mp-wp_genesis 972 /**
mp-wp_genesis 973 * Count number of attachments for the mime type(s).
mp-wp_genesis 974 *
mp-wp_genesis 975 * If you set the optional mime_type parameter, then an array will still be
mp-wp_genesis 976 * returned, but will only have the item you are looking for. It does not give
mp-wp_genesis 977 * you the number of attachments that are children of a post. You can get that
mp-wp_genesis 978 * by counting the number of children that post has.
mp-wp_genesis 979 *
mp-wp_genesis 980 * @since 2.5.0
mp-wp_genesis 981 *
mp-wp_genesis 982 * @param string|array $mime_type Optional. Array or comma-separated list of MIME patterns.
mp-wp_genesis 983 * @return array Number of posts for each mime type.
mp-wp_genesis 984 */
mp-wp_genesis 985 function wp_count_attachments( $mime_type = '' ) {
mp-wp_genesis 986 global $wpdb;
mp-wp_genesis 987
mp-wp_genesis 988 $and = wp_post_mime_type_where( $mime_type );
mp-wp_genesis 989 $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 990
mp-wp_genesis 991 $stats = array( );
mp-wp_genesis 992 foreach( (array) $count as $row ) {
mp-wp_genesis 993 $stats[$row['post_mime_type']] = $row['num_posts'];
mp-wp_genesis 994 }
mp-wp_genesis 995
mp-wp_genesis 996 return (object) $stats;
mp-wp_genesis 997 }
mp-wp_genesis 998
mp-wp_genesis 999 /**
mp-wp_genesis 1000 * Check a MIME-Type against a list.
mp-wp_genesis 1001 *
mp-wp_genesis 1002 * If the wildcard_mime_types parameter is a string, it must be comma separated
mp-wp_genesis 1003 * list. If the real_mime_types is a string, it is also comma separated to
mp-wp_genesis 1004 * create the list.
mp-wp_genesis 1005 *
mp-wp_genesis 1006 * @since 2.5.0
mp-wp_genesis 1007 *
mp-wp_genesis 1008 * @param string|array $wildcard_mime_types e.g. audio/mpeg or image (same as image/*) or flash (same as *flash*)
mp-wp_genesis 1009 * @param string|array $real_mime_types post_mime_type values
mp-wp_genesis 1010 * @return array array(wildcard=>array(real types))
mp-wp_genesis 1011 */
mp-wp_genesis 1012 function wp_match_mime_types($wildcard_mime_types, $real_mime_types) {
mp-wp_genesis 1013 $matches = array();
mp-wp_genesis 1014 if ( is_string($wildcard_mime_types) )
mp-wp_genesis 1015 $wildcard_mime_types = array_map('trim', explode(',', $wildcard_mime_types));
mp-wp_genesis 1016 if ( is_string($real_mime_types) )
mp-wp_genesis 1017 $real_mime_types = array_map('trim', explode(',', $real_mime_types));
mp-wp_genesis 1018 $wild = '[-._a-z0-9]*';
mp-wp_genesis 1019 foreach ( (array) $wildcard_mime_types as $type ) {
mp-wp_genesis 1020 $type = str_replace('*', $wild, $type);
mp-wp_genesis 1021 $patternses[1][$type] = "^$type$";
mp-wp_genesis 1022 if ( false === strpos($type, '/') ) {
mp-wp_genesis 1023 $patternses[2][$type] = "^$type/";
mp-wp_genesis 1024 $patternses[3][$type] = $type;
mp-wp_genesis 1025 }
mp-wp_genesis 1026 }
mp-wp_genesis 1027 asort($patternses);
mp-wp_genesis 1028 foreach ( $patternses as $patterns )
mp-wp_genesis 1029 foreach ( $patterns as $type => $pattern )
mp-wp_genesis 1030 foreach ( (array) $real_mime_types as $real )
mp-wp_genesis 1031 if ( preg_match("#$pattern#", $real) && ( empty($matches[$type]) || false === array_search($real, $matches[$type]) ) )
mp-wp_genesis 1032 $matches[$type][] = $real;
mp-wp_genesis 1033 return $matches;
mp-wp_genesis 1034 }
mp-wp_genesis 1035
mp-wp_genesis 1036 /**
mp-wp_genesis 1037 * Convert MIME types into SQL.
mp-wp_genesis 1038 *
mp-wp_genesis 1039 * @since 2.5.0
mp-wp_genesis 1040 *
mp-wp_genesis 1041 * @param string|array $mime_types List of mime types or comma separated string of mime types.
mp-wp_genesis 1042 * @return string The SQL AND clause for mime searching.
mp-wp_genesis 1043 */
mp-wp_genesis 1044 function wp_post_mime_type_where($post_mime_types) {
mp-wp_genesis 1045 $where = '';
mp-wp_genesis 1046 $wildcards = array('', '%', '%/%');
mp-wp_genesis 1047 if ( is_string($post_mime_types) )
mp-wp_genesis 1048 $post_mime_types = array_map('trim', explode(',', $post_mime_types));
mp-wp_genesis 1049 foreach ( (array) $post_mime_types as $mime_type ) {
mp-wp_genesis 1050 $mime_type = preg_replace('/\s/', '', $mime_type);
mp-wp_genesis 1051 $slashpos = strpos($mime_type, '/');
mp-wp_genesis 1052 if ( false !== $slashpos ) {
mp-wp_genesis 1053 $mime_group = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, 0, $slashpos));
mp-wp_genesis 1054 $mime_subgroup = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, $slashpos + 1));
mp-wp_genesis 1055 if ( empty($mime_subgroup) )
mp-wp_genesis 1056 $mime_subgroup = '*';
mp-wp_genesis 1057 else
mp-wp_genesis 1058 $mime_subgroup = str_replace('/', '', $mime_subgroup);
mp-wp_genesis 1059 $mime_pattern = "$mime_group/$mime_subgroup";
mp-wp_genesis 1060 } else {
mp-wp_genesis 1061 $mime_pattern = preg_replace('/[^-*.a-zA-Z0-9]/', '', $mime_type);
mp-wp_genesis 1062 if ( false === strpos($mime_pattern, '*') )
mp-wp_genesis 1063 $mime_pattern .= '/*';
mp-wp_genesis 1064 }
mp-wp_genesis 1065
mp-wp_genesis 1066 $mime_pattern = preg_replace('/\*+/', '%', $mime_pattern);
mp-wp_genesis 1067
mp-wp_genesis 1068 if ( in_array( $mime_type, $wildcards ) )
mp-wp_genesis 1069 return '';
mp-wp_genesis 1070
mp-wp_genesis 1071 if ( false !== strpos($mime_pattern, '%') )
mp-wp_genesis 1072 $wheres[] = "post_mime_type LIKE '$mime_pattern'";
mp-wp_genesis 1073 else
mp-wp_genesis 1074 $wheres[] = "post_mime_type = '$mime_pattern'";
mp-wp_genesis 1075 }
mp-wp_genesis 1076 if ( !empty($wheres) )
mp-wp_genesis 1077 $where = ' AND (' . join(' OR ', $wheres) . ') ';
mp-wp_genesis 1078 return $where;
mp-wp_genesis 1079 }
mp-wp_genesis 1080
mp-wp_genesis 1081 /**
mp-wp_genesis 1082 * Removes a post, attachment, or page.
mp-wp_genesis 1083 *
mp-wp_genesis 1084 * When the post and page goes, everything that is tied to it is deleted also.
mp-wp_genesis 1085 * This includes comments, post meta fields, and terms associated with the post.
mp-wp_genesis 1086 *
mp-wp_genesis 1087 * @since 1.0.0
mp-wp_genesis 1088 * @uses do_action() Calls 'deleted_post' hook on post ID.
mp-wp_genesis 1089 *
mp-wp_genesis 1090 * @param int $postid Post ID.
mp-wp_genesis 1091 * @return mixed
mp-wp_genesis 1092 */
mp-wp_genesis 1093 function wp_delete_post($postid = 0) {
mp-wp_genesis 1094 global $wpdb, $wp_rewrite;
mp-wp_genesis 1095
mp-wp_genesis 1096 if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) )
mp-wp_genesis 1097 return $post;
mp-wp_genesis 1098
mp-wp_genesis 1099 if ( 'attachment' == $post->post_type )
mp-wp_genesis 1100 return wp_delete_attachment($postid);
mp-wp_genesis 1101
mp-wp_genesis 1102 do_action('delete_post', $postid);
mp-wp_genesis 1103
mp-wp_genesis 1104 /** @todo delete for pluggable post taxonomies too */
mp-wp_genesis 1105 wp_delete_object_term_relationships($postid, array('category', 'post_tag'));
mp-wp_genesis 1106
mp-wp_genesis 1107 $parent_data = array( 'post_parent' => $post->post_parent );
mp-wp_genesis 1108 $parent_where = array( 'post_parent' => $postid );
mp-wp_genesis 1109
mp-wp_genesis 1110 if ( 'page' == $post->post_type) {
mp-wp_genesis 1111 // if the page is defined in option page_on_front or post_for_posts,
mp-wp_genesis 1112 // adjust the corresponding options
mp-wp_genesis 1113 if ( get_option('page_on_front') == $postid ) {
mp-wp_genesis 1114 update_option('show_on_front', 'posts');
mp-wp_genesis 1115 delete_option('page_on_front');
mp-wp_genesis 1116 }
mp-wp_genesis 1117 if ( get_option('page_for_posts') == $postid ) {
mp-wp_genesis 1118 delete_option('page_for_posts');
mp-wp_genesis 1119 }
mp-wp_genesis 1120
mp-wp_genesis 1121 // Point children of this page to its parent, also clean the cache of affected children
mp-wp_genesis 1122 $children_query = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type='page'", $postid);
mp-wp_genesis 1123 $children = $wpdb->get_results($children_query);
mp-wp_genesis 1124
mp-wp_genesis 1125 $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'page' ) );
mp-wp_genesis 1126 }
mp-wp_genesis 1127
mp-wp_genesis 1128 // Do raw query. wp_get_post_revisions() is filtered
mp-wp_genesis 1129 $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) );
mp-wp_genesis 1130 // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up.
mp-wp_genesis 1131 foreach ( $revision_ids as $revision_id )
mp-wp_genesis 1132 wp_delete_post_revision( $revision_id );
mp-wp_genesis 1133
mp-wp_genesis 1134 // Point all attachments to this post up one level
mp-wp_genesis 1135 $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );
mp-wp_genesis 1136
mp-wp_genesis 1137 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid ));
mp-wp_genesis 1138
mp-wp_genesis 1139 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ));
mp-wp_genesis 1140
mp-wp_genesis 1141 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d", $postid ));
mp-wp_genesis 1142
mp-wp_genesis 1143 if ( 'page' == $post->post_type ) {
mp-wp_genesis 1144 clean_page_cache($postid);
mp-wp_genesis 1145
mp-wp_genesis 1146 foreach ( (array) $children as $child )
mp-wp_genesis 1147 clean_page_cache($child->ID);
mp-wp_genesis 1148
mp-wp_genesis 1149 $wp_rewrite->flush_rules();
mp-wp_genesis 1150 } else {
mp-wp_genesis 1151 clean_post_cache($postid);
mp-wp_genesis 1152 }
mp-wp_genesis 1153
mp-wp_genesis 1154 do_action('deleted_post', $postid);
mp-wp_genesis 1155
mp-wp_genesis 1156 return $post;
mp-wp_genesis 1157 }
mp-wp_genesis 1158
mp-wp_genesis 1159 /**
mp-wp_genesis 1160 * Retrieve the list of categories for a post.
mp-wp_genesis 1161 *
mp-wp_genesis 1162 * Compatibility layer for themes and plugins. Also an easy layer of abstraction
mp-wp_genesis 1163 * away from the complexity of the taxonomy layer.
mp-wp_genesis 1164 *
mp-wp_genesis 1165 * @since 2.1.0
mp-wp_genesis 1166 *
mp-wp_genesis 1167 * @uses wp_get_object_terms() Retrieves the categories. Args details can be found here.
mp-wp_genesis 1168 *
mp-wp_genesis 1169 * @param int $post_id Optional. The Post ID.
mp-wp_genesis 1170 * @param array $args Optional. Overwrite the defaults.
mp-wp_genesis 1171 * @return array
mp-wp_genesis 1172 */
mp-wp_genesis 1173 function wp_get_post_categories( $post_id = 0, $args = array() ) {
mp-wp_genesis 1174 $post_id = (int) $post_id;
mp-wp_genesis 1175
mp-wp_genesis 1176 $defaults = array('fields' => 'ids');
mp-wp_genesis 1177 $args = wp_parse_args( $args, $defaults );
mp-wp_genesis 1178
mp-wp_genesis 1179 $cats = wp_get_object_terms($post_id, 'category', $args);
mp-wp_genesis 1180 return $cats;
mp-wp_genesis 1181 }
mp-wp_genesis 1182
mp-wp_genesis 1183 /**
mp-wp_genesis 1184 * Retrieve the tags for a post.
mp-wp_genesis 1185 *
mp-wp_genesis 1186 * There is only one default for this function, called 'fields' and by default
mp-wp_genesis 1187 * is set to 'all'. There are other defaults that can be override in
mp-wp_genesis 1188 * {@link wp_get_object_terms()}.
mp-wp_genesis 1189 *
mp-wp_genesis 1190 * @package WordPress
mp-wp_genesis 1191 * @subpackage Post
mp-wp_genesis 1192 * @since 2.3.0
mp-wp_genesis 1193 *
mp-wp_genesis 1194 * @uses wp_get_object_terms() Gets the tags for returning. Args can be found here
mp-wp_genesis 1195 *
mp-wp_genesis 1196 * @param int $post_id Optional. The Post ID
mp-wp_genesis 1197 * @param array $args Optional. Overwrite the defaults
mp-wp_genesis 1198 * @return array List of post tags.
mp-wp_genesis 1199 */
mp-wp_genesis 1200 function wp_get_post_tags( $post_id = 0, $args = array() ) {
mp-wp_genesis 1201 $post_id = (int) $post_id;
mp-wp_genesis 1202
mp-wp_genesis 1203 $defaults = array('fields' => 'all');
mp-wp_genesis 1204 $args = wp_parse_args( $args, $defaults );
mp-wp_genesis 1205
mp-wp_genesis 1206 $tags = wp_get_object_terms($post_id, 'post_tag', $args);
mp-wp_genesis 1207
mp-wp_genesis 1208 return $tags;
mp-wp_genesis 1209 }
mp-wp_genesis 1210
mp-wp_genesis 1211 /**
mp-wp_genesis 1212 * Retrieve number of recent posts.
mp-wp_genesis 1213 *
mp-wp_genesis 1214 * @since 1.0.0
mp-wp_genesis 1215 * @uses $wpdb
mp-wp_genesis 1216 *
mp-wp_genesis 1217 * @param int $num Optional, default is 10. Number of posts to get.
mp-wp_genesis 1218 * @return array List of posts.
mp-wp_genesis 1219 */
mp-wp_genesis 1220 function wp_get_recent_posts($num = 10) {
mp-wp_genesis 1221 global $wpdb;
mp-wp_genesis 1222
mp-wp_genesis 1223 // Set the limit clause, if we got a limit
mp-wp_genesis 1224 $num = (int) $num;
mp-wp_genesis 1225 if ($num) {
mp-wp_genesis 1226 $limit = "LIMIT $num";
mp-wp_genesis 1227 }
mp-wp_genesis 1228
mp-wp_genesis 1229 $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC $limit";
mp-wp_genesis 1230 $result = $wpdb->get_results($sql,ARRAY_A);
mp-wp_genesis 1231
mp-wp_genesis 1232 return $result ? $result : array();
mp-wp_genesis 1233 }
mp-wp_genesis 1234
mp-wp_genesis 1235 /**
mp-wp_genesis 1236 * Retrieve a single post, based on post ID.
mp-wp_genesis 1237 *
mp-wp_genesis 1238 * Has categories in 'post_category' property or key. Has tags in 'tags_input'
mp-wp_genesis 1239 * property or key.
mp-wp_genesis 1240 *
mp-wp_genesis 1241 * @since 1.0.0
mp-wp_genesis 1242 *
mp-wp_genesis 1243 * @param int $postid Post ID.
mp-wp_genesis 1244 * @param string $mode How to return result, either OBJECT, ARRAY_N, or ARRAY_A.
mp-wp_genesis 1245 * @return object|array Post object or array holding post contents and information
mp-wp_genesis 1246 */
mp-wp_genesis 1247 function wp_get_single_post($postid = 0, $mode = OBJECT) {
mp-wp_genesis 1248 $postid = (int) $postid;
mp-wp_genesis 1249
mp-wp_genesis 1250 $post = get_post($postid, $mode);
mp-wp_genesis 1251
mp-wp_genesis 1252 // Set categories and tags
mp-wp_genesis 1253 if($mode == OBJECT) {
mp-wp_genesis 1254 $post->post_category = wp_get_post_categories($postid);
mp-wp_genesis 1255 $post->tags_input = wp_get_post_tags($postid, array('fields' => 'names'));
mp-wp_genesis 1256 }
mp-wp_genesis 1257 else {
mp-wp_genesis 1258 $post['post_category'] = wp_get_post_categories($postid);
mp-wp_genesis 1259 $post['tags_input'] = wp_get_post_tags($postid, array('fields' => 'names'));
mp-wp_genesis 1260 }
mp-wp_genesis 1261
mp-wp_genesis 1262 return $post;
mp-wp_genesis 1263 }
mp-wp_genesis 1264
mp-wp_genesis 1265 /**
mp-wp_genesis 1266 * Insert a post.
mp-wp_genesis 1267 *
mp-wp_genesis 1268 * If the $postarr parameter has 'ID' set to a value, then post will be updated.
mp-wp_genesis 1269 *
mp-wp_genesis 1270 * You can set the post date manually, but setting the values for 'post_date'
mp-wp_genesis 1271 * and 'post_date_gmt' keys. You can close the comments or open the comments by
mp-wp_genesis 1272 * setting the value for 'comment_status' key.
mp-wp_genesis 1273 *
mp-wp_genesis 1274 * The defaults for the parameter $postarr are:
mp-wp_genesis 1275 * 'post_status' - Default is 'draft'.
mp-wp_genesis 1276 * 'post_type' - Default is 'post'.
mp-wp_genesis 1277 * 'post_author' - Default is current user ID. The ID of the user, who added
mp-wp_genesis 1278 * the post.
mp-wp_genesis 1279 * 'ping_status' - Default is the value in default ping status option.
mp-wp_genesis 1280 * Whether the attachment can accept pings.
mp-wp_genesis 1281 * 'post_parent' - Default is 0. Set this for the post it belongs to, if
mp-wp_genesis 1282 * any.
mp-wp_genesis 1283 * 'menu_order' - Default is 0. The order it is displayed.
mp-wp_genesis 1284 * 'to_ping' - Whether to ping.
mp-wp_genesis 1285 * 'pinged' - Default is empty string.
mp-wp_genesis 1286 * 'post_password' - Default is empty string. The password to access the
mp-wp_genesis 1287 * attachment.
mp-wp_genesis 1288 * 'guid' - Global Unique ID for referencing the attachment.
mp-wp_genesis 1289 * 'post_content_filtered' - Post content filtered.
mp-wp_genesis 1290 * 'post_excerpt' - Post excerpt.
mp-wp_genesis 1291 *
mp-wp_genesis 1292 * @since 1.0.0
mp-wp_genesis 1293 * @uses $wpdb
mp-wp_genesis 1294 * @uses $wp_rewrite
mp-wp_genesis 1295 * @uses $user_ID
mp-wp_genesis 1296 *
mp-wp_genesis 1297 * @param array $postarr Optional. Override defaults.
mp-wp_genesis 1298 * @param bool $wp_error Optional. Allow return of WP_Error on failure.
mp-wp_genesis 1299 * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success.
mp-wp_genesis 1300 */
mp-wp_genesis 1301 function wp_insert_post($postarr = array(), $wp_error = false) {
mp-wp_genesis 1302 global $wpdb, $wp_rewrite, $user_ID;
mp-wp_genesis 1303
mp-wp_genesis 1304 $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
mp-wp_genesis 1305 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
mp-wp_genesis 1306 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '',
mp-wp_genesis 1307 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0);
mp-wp_genesis 1308
mp-wp_genesis 1309 $postarr = wp_parse_args($postarr, $defaults);
mp-wp_genesis 1310 $postarr = sanitize_post($postarr, 'db');
mp-wp_genesis 1311
mp-wp_genesis 1312 // export array as variables
mp-wp_genesis 1313 extract($postarr, EXTR_SKIP);
mp-wp_genesis 1314
mp-wp_genesis 1315 // Are we updating or creating?
mp-wp_genesis 1316 $update = false;
mp-wp_genesis 1317 if ( !empty($ID) ) {
mp-wp_genesis 1318 $update = true;
mp-wp_genesis 1319 $previous_status = get_post_field('post_status', $ID);
mp-wp_genesis 1320 } else {
mp-wp_genesis 1321 $previous_status = 'new';
mp-wp_genesis 1322 }
mp-wp_genesis 1323
mp-wp_genesis 1324 if ( ('' == $post_content) && ('' == $post_title) && ('' == $post_excerpt) ) {
mp-wp_genesis 1325 if ( $wp_error )
mp-wp_genesis 1326 return new WP_Error('empty_content', __('Content, title, and excerpt are empty.'));
mp-wp_genesis 1327 else
mp-wp_genesis 1328 return 0;
mp-wp_genesis 1329 }
mp-wp_genesis 1330
mp-wp_genesis 1331 // Make sure we set a valid category
mp-wp_genesis 1332 if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
mp-wp_genesis 1333 $post_category = array(get_option('default_category'));
mp-wp_genesis 1334 }
mp-wp_genesis 1335
mp-wp_genesis 1336 //Set the default tag list
mp-wp_genesis 1337 if ( !isset($tags_input) )
mp-wp_genesis 1338 $tags_input = array();
mp-wp_genesis 1339
mp-wp_genesis 1340 if ( empty($post_author) )
mp-wp_genesis 1341 $post_author = $user_ID;
mp-wp_genesis 1342
mp-wp_genesis 1343 if ( empty($post_status) )
mp-wp_genesis 1344 $post_status = 'draft';
mp-wp_genesis 1345
mp-wp_genesis 1346 if ( empty($post_type) )
mp-wp_genesis 1347 $post_type = 'post';
mp-wp_genesis 1348
mp-wp_genesis 1349 $post_ID = 0;
mp-wp_genesis 1350
mp-wp_genesis 1351 // Get the post ID and GUID
mp-wp_genesis 1352 if ( $update ) {
mp-wp_genesis 1353 $post_ID = (int) $ID;
mp-wp_genesis 1354 $guid = get_post_field( 'guid', $post_ID );
mp-wp_genesis 1355 }
mp-wp_genesis 1356
mp-wp_genesis 1357 // Don't allow contributors to set to set the post slug for pending review posts
mp-wp_genesis 1358 if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) )
mp-wp_genesis 1359 $post_name = '';
mp-wp_genesis 1360
mp-wp_genesis 1361 // Create a valid post name. Drafts and pending posts are allowed to have an empty
mp-wp_genesis 1362 // post name.
mp-wp_genesis 1363 if ( empty($post_name) ) {
mp-wp_genesis 1364 if ( !in_array( $post_status, array( 'draft', 'pending' ) ) )
mp-wp_genesis 1365 $post_name = sanitize_title($post_title);
mp-wp_genesis 1366 } else {
mp-wp_genesis 1367 $post_name = sanitize_title($post_name);
mp-wp_genesis 1368 }
mp-wp_genesis 1369
mp-wp_genesis 1370 // 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 1371 if ( empty($post_date) || '0000-00-00 00:00:00' == $post_date )
mp-wp_genesis 1372 $post_date = current_time('mysql');
mp-wp_genesis 1373
mp-wp_genesis 1374 if ( empty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) {
mp-wp_genesis 1375 if ( !in_array( $post_status, array( 'draft', 'pending' ) ) )
mp-wp_genesis 1376 $post_date_gmt = get_gmt_from_date($post_date);
mp-wp_genesis 1377 else
mp-wp_genesis 1378 $post_date_gmt = '0000-00-00 00:00:00';
mp-wp_genesis 1379 }
mp-wp_genesis 1380
mp-wp_genesis 1381 if ( $update || '0000-00-00 00:00:00' == $post_date ) {
mp-wp_genesis 1382 $post_modified = current_time( 'mysql' );
mp-wp_genesis 1383 $post_modified_gmt = current_time( 'mysql', 1 );
mp-wp_genesis 1384 } else {
mp-wp_genesis 1385 $post_modified = $post_date;
mp-wp_genesis 1386 $post_modified_gmt = $post_date_gmt;
mp-wp_genesis 1387 }
mp-wp_genesis 1388
mp-wp_genesis 1389 if ( 'publish' == $post_status ) {
mp-wp_genesis 1390 $now = gmdate('Y-m-d H:i:59');
mp-wp_genesis 1391 if ( mysql2date('U', $post_date_gmt) > mysql2date('U', $now) )
mp-wp_genesis 1392 $post_status = 'future';
mp-wp_genesis 1393 }
mp-wp_genesis 1394
mp-wp_genesis 1395 if ( empty($comment_status) ) {
mp-wp_genesis 1396 if ( $update )
mp-wp_genesis 1397 $comment_status = 'closed';
mp-wp_genesis 1398 else
mp-wp_genesis 1399 $comment_status = get_option('default_comment_status');
mp-wp_genesis 1400 }
mp-wp_genesis 1401 if ( empty($ping_status) )
mp-wp_genesis 1402 $ping_status = get_option('default_ping_status');
mp-wp_genesis 1403
mp-wp_genesis 1404 if ( isset($to_ping) )
mp-wp_genesis 1405 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
mp-wp_genesis 1406 else
mp-wp_genesis 1407 $to_ping = '';
mp-wp_genesis 1408
mp-wp_genesis 1409 if ( ! isset($pinged) )
mp-wp_genesis 1410 $pinged = '';
mp-wp_genesis 1411
mp-wp_genesis 1412 if ( isset($post_parent) )
mp-wp_genesis 1413 $post_parent = (int) $post_parent;
mp-wp_genesis 1414 else
mp-wp_genesis 1415 $post_parent = 0;
mp-wp_genesis 1416
mp-wp_genesis 1417 if ( !empty($post_ID) ) {
mp-wp_genesis 1418 if ( $post_parent == $post_ID ) {
mp-wp_genesis 1419 // Post can't be its own parent
mp-wp_genesis 1420 $post_parent = 0;
mp-wp_genesis 1421 } elseif ( !empty($post_parent) ) {
mp-wp_genesis 1422 $parent_post = get_post($post_parent);
mp-wp_genesis 1423 // Check for circular dependency
mp-wp_genesis 1424 if ( $parent_post->post_parent == $post_ID )
mp-wp_genesis 1425 $post_parent = 0;
mp-wp_genesis 1426 }
mp-wp_genesis 1427 }
mp-wp_genesis 1428
mp-wp_genesis 1429 if ( isset($menu_order) )
mp-wp_genesis 1430 $menu_order = (int) $menu_order;
mp-wp_genesis 1431 else
mp-wp_genesis 1432 $menu_order = 0;
mp-wp_genesis 1433
mp-wp_genesis 1434 if ( !isset($post_password) || 'private' == $post_status )
mp-wp_genesis 1435 $post_password = '';
mp-wp_genesis 1436
mp-wp_genesis 1437 if ( !in_array( $post_status, array( 'draft', 'pending' ) ) ) {
mp-wp_genesis 1438 $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 1439
mp-wp_genesis 1440 if ($post_name_check || in_array($post_name, $wp_rewrite->feeds) ) {
mp-wp_genesis 1441 $suffix = 2;
mp-wp_genesis 1442 do {
mp-wp_genesis 1443 $alt_post_name = substr($post_name, 0, 200-(strlen($suffix)+1)). "-$suffix";
mp-wp_genesis 1444 $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 1445 $suffix++;
mp-wp_genesis 1446 } while ($post_name_check);
mp-wp_genesis 1447 $post_name = $alt_post_name;
mp-wp_genesis 1448 }
mp-wp_genesis 1449 }
mp-wp_genesis 1450
mp-wp_genesis 1451 // expected_slashed (everything!)
mp-wp_genesis 1452 $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 1453 $data = apply_filters('wp_insert_post_data', $data, $postarr);
mp-wp_genesis 1454 $data = stripslashes_deep( $data );
mp-wp_genesis 1455 $where = array( 'ID' => $post_ID );
mp-wp_genesis 1456
mp-wp_genesis 1457 if ($update) {
mp-wp_genesis 1458 do_action( 'pre_post_update', $post_ID );
mp-wp_genesis 1459 if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
mp-wp_genesis 1460 if ( $wp_error )
mp-wp_genesis 1461 return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error);
mp-wp_genesis 1462 else
mp-wp_genesis 1463 return 0;
mp-wp_genesis 1464 }
mp-wp_genesis 1465 } else {
mp-wp_genesis 1466 if ( isset($post_mime_type) )
mp-wp_genesis 1467 $data['post_mime_type'] = stripslashes( $post_mime_type ); // This isn't in the update
mp-wp_genesis 1468 // If there is a suggested ID, use it if not already present
mp-wp_genesis 1469 if ( !empty($import_id) ) {
mp-wp_genesis 1470 $import_id = (int) $import_id;
mp-wp_genesis 1471 if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {
mp-wp_genesis 1472 $data['ID'] = $import_id;
mp-wp_genesis 1473 }
mp-wp_genesis 1474 }
mp-wp_genesis 1475 if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
mp-wp_genesis 1476 if ( $wp_error )
mp-wp_genesis 1477 return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error);
mp-wp_genesis 1478 else
mp-wp_genesis 1479 return 0;
mp-wp_genesis 1480 }
mp-wp_genesis 1481 $post_ID = (int) $wpdb->insert_id;
mp-wp_genesis 1482
mp-wp_genesis 1483 // use the newly generated $post_ID
mp-wp_genesis 1484 $where = array( 'ID' => $post_ID );
mp-wp_genesis 1485 }
mp-wp_genesis 1486
mp-wp_genesis 1487 if ( empty($post_name) && !in_array( $post_status, array( 'draft', 'pending' ) ) ) {
mp-wp_genesis 1488 $post_name = sanitize_title($post_title, $post_ID);
mp-wp_genesis 1489 $wpdb->update( $wpdb->posts, compact( 'post_name' ), $where );
mp-wp_genesis 1490 }
mp-wp_genesis 1491
mp-wp_genesis 1492 wp_set_post_categories( $post_ID, $post_category );
mp-wp_genesis 1493 wp_set_post_tags( $post_ID, $tags_input );
mp-wp_genesis 1494
mp-wp_genesis 1495 $current_guid = get_post_field( 'guid', $post_ID );
mp-wp_genesis 1496
mp-wp_genesis 1497 if ( 'page' == $post_type )
mp-wp_genesis 1498 clean_page_cache($post_ID);
mp-wp_genesis 1499 else
mp-wp_genesis 1500 clean_post_cache($post_ID);
mp-wp_genesis 1501
mp-wp_genesis 1502 // Set GUID
mp-wp_genesis 1503 if ( !$update && '' == $current_guid )
mp-wp_genesis 1504 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
mp-wp_genesis 1505
mp-wp_genesis 1506 $post = get_post($post_ID);
mp-wp_genesis 1507
mp-wp_genesis 1508 if ( !empty($page_template) && 'page' == $post_type ) {
mp-wp_genesis 1509 $post->page_template = $page_template;
mp-wp_genesis 1510 $page_templates = get_page_templates();
mp-wp_genesis 1511 if ( 'default' != $page_template && !in_array($page_template, $page_templates) ) {
mp-wp_genesis 1512 if ( $wp_error )
mp-wp_genesis 1513 return new WP_Error('invalid_page_template', __('The page template is invalid.'));
mp-wp_genesis 1514 else
mp-wp_genesis 1515 return 0;
mp-wp_genesis 1516 }
mp-wp_genesis 1517 update_post_meta($post_ID, '_wp_page_template', $page_template);
mp-wp_genesis 1518 }
mp-wp_genesis 1519
mp-wp_genesis 1520 wp_transition_post_status($post_status, $previous_status, $post);
mp-wp_genesis 1521
mp-wp_genesis 1522 if ( $update)
mp-wp_genesis 1523 do_action('edit_post', $post_ID, $post);
mp-wp_genesis 1524
mp-wp_genesis 1525 do_action('save_post', $post_ID, $post);
mp-wp_genesis 1526 do_action('wp_insert_post', $post_ID, $post);
mp-wp_genesis 1527
mp-wp_genesis 1528 return $post_ID;
mp-wp_genesis 1529 }
mp-wp_genesis 1530
mp-wp_genesis 1531 /**
mp-wp_genesis 1532 * Update a post with new post data.
mp-wp_genesis 1533 *
mp-wp_genesis 1534 * The date does not have to be set for drafts. You can set the date and it will
mp-wp_genesis 1535 * not be overridden.
mp-wp_genesis 1536 *
mp-wp_genesis 1537 * @since 1.0.0
mp-wp_genesis 1538 *
mp-wp_genesis 1539 * @param array|object $postarr Post data.
mp-wp_genesis 1540 * @return int 0 on failure, Post ID on success.
mp-wp_genesis 1541 */
mp-wp_genesis 1542 function wp_update_post($postarr = array()) {
mp-wp_genesis 1543 if ( is_object($postarr) )
mp-wp_genesis 1544 $postarr = get_object_vars($postarr);
mp-wp_genesis 1545
mp-wp_genesis 1546 // First, get all of the original fields
mp-wp_genesis 1547 $post = wp_get_single_post($postarr['ID'], ARRAY_A);
mp-wp_genesis 1548
mp-wp_genesis 1549 // Escape data pulled from DB.
mp-wp_genesis 1550 $post = add_magic_quotes($post);
mp-wp_genesis 1551
mp-wp_genesis 1552 // Passed post category list overwrites existing category list if not empty.
mp-wp_genesis 1553 if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
mp-wp_genesis 1554 && 0 != count($postarr['post_category']) )
mp-wp_genesis 1555 $post_cats = $postarr['post_category'];
mp-wp_genesis 1556 else
mp-wp_genesis 1557 $post_cats = $post['post_category'];
mp-wp_genesis 1558
mp-wp_genesis 1559 // Drafts shouldn't be assigned a date unless explicitly done so by the user
mp-wp_genesis 1560 if ( in_array($post['post_status'], array('draft', 'pending')) && empty($postarr['edit_date']) &&
mp-wp_genesis 1561 ('0000-00-00 00:00:00' == $post['post_date_gmt']) )
mp-wp_genesis 1562 $clear_date = true;
mp-wp_genesis 1563 else
mp-wp_genesis 1564 $clear_date = false;
mp-wp_genesis 1565
mp-wp_genesis 1566 // Merge old and new fields with new fields overwriting old ones.
mp-wp_genesis 1567 $postarr = array_merge($post, $postarr);
mp-wp_genesis 1568 $postarr['post_category'] = $post_cats;
mp-wp_genesis 1569 if ( $clear_date ) {
mp-wp_genesis 1570 $postarr['post_date'] = current_time('mysql');
mp-wp_genesis 1571 $postarr['post_date_gmt'] = '';
mp-wp_genesis 1572 }
mp-wp_genesis 1573
mp-wp_genesis 1574 if ($postarr['post_type'] == 'attachment')
mp-wp_genesis 1575 return wp_insert_attachment($postarr);
mp-wp_genesis 1576
mp-wp_genesis 1577 return wp_insert_post($postarr);
mp-wp_genesis 1578 }
mp-wp_genesis 1579
mp-wp_genesis 1580 /**
mp-wp_genesis 1581 * Publish a post by transitioning the post status.
mp-wp_genesis 1582 *
mp-wp_genesis 1583 * @since 2.1.0
mp-wp_genesis 1584 * @uses $wpdb
mp-wp_genesis 1585 * @uses do_action() Calls 'edit_post', 'save_post', and 'wp_insert_post' on post_id and post data.
mp-wp_genesis 1586 *
mp-wp_genesis 1587 * @param int $post_id Post ID.
mp-wp_genesis 1588 * @return null
mp-wp_genesis 1589 */
mp-wp_genesis 1590 function wp_publish_post($post_id) {
mp-wp_genesis 1591 global $wpdb;
mp-wp_genesis 1592
mp-wp_genesis 1593 $post = get_post($post_id);
mp-wp_genesis 1594
mp-wp_genesis 1595 if ( empty($post) )
mp-wp_genesis 1596 return;
mp-wp_genesis 1597
mp-wp_genesis 1598 if ( 'publish' == $post->post_status )
mp-wp_genesis 1599 return;
mp-wp_genesis 1600
mp-wp_genesis 1601 $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post_id ) );
mp-wp_genesis 1602
mp-wp_genesis 1603 $old_status = $post->post_status;
mp-wp_genesis 1604 $post->post_status = 'publish';
mp-wp_genesis 1605 wp_transition_post_status('publish', $old_status, $post);
mp-wp_genesis 1606
mp-wp_genesis 1607 // Update counts for the post's terms.
mp-wp_genesis 1608 foreach ( (array) get_object_taxonomies('post') as $taxonomy ) {
mp-wp_genesis 1609 $tt_ids = wp_get_object_terms($post_id, $taxonomy, 'fields=tt_ids');
mp-wp_genesis 1610 wp_update_term_count($tt_ids, $taxonomy);
mp-wp_genesis 1611 }
mp-wp_genesis 1612
mp-wp_genesis 1613 do_action('edit_post', $post_id, $post);
mp-wp_genesis 1614 do_action('save_post', $post_id, $post);
mp-wp_genesis 1615 do_action('wp_insert_post', $post_id, $post);
mp-wp_genesis 1616 }
mp-wp_genesis 1617
mp-wp_genesis 1618 /**
mp-wp_genesis 1619 * Publish future post and make sure post ID has future post status.
mp-wp_genesis 1620 *
mp-wp_genesis 1621 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
mp-wp_genesis 1622 * from publishing drafts, etc.
mp-wp_genesis 1623 *
mp-wp_genesis 1624 * @since 2.5.0
mp-wp_genesis 1625 *
mp-wp_genesis 1626 * @param int $post_id Post ID.
mp-wp_genesis 1627 * @return null Nothing is returned. Which can mean that no action is required or post was published.
mp-wp_genesis 1628 */
mp-wp_genesis 1629 function check_and_publish_future_post($post_id) {
mp-wp_genesis 1630
mp-wp_genesis 1631 $post = get_post($post_id);
mp-wp_genesis 1632
mp-wp_genesis 1633 if ( empty($post) )
mp-wp_genesis 1634 return;
mp-wp_genesis 1635
mp-wp_genesis 1636 if ( 'future' != $post->post_status )
mp-wp_genesis 1637 return;
mp-wp_genesis 1638
mp-wp_genesis 1639 $time = strtotime( $post->post_date_gmt . ' GMT' );
mp-wp_genesis 1640
mp-wp_genesis 1641 if ( $time > time() ) { // Uh oh, someone jumped the gun!
mp-wp_genesis 1642 wp_clear_scheduled_hook( 'publish_future_post', $post_id ); // clear anything else in the system
mp-wp_genesis 1643 wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) );
mp-wp_genesis 1644 return;
mp-wp_genesis 1645 }
mp-wp_genesis 1646
mp-wp_genesis 1647 return wp_publish_post($post_id);
mp-wp_genesis 1648 }
mp-wp_genesis 1649
mp-wp_genesis 1650 /**
mp-wp_genesis 1651 * Adds tags to a post.
mp-wp_genesis 1652 *
mp-wp_genesis 1653 * @uses wp_set_post_tags() Same first two parameters, but the last parameter is always set to true.
mp-wp_genesis 1654 *
mp-wp_genesis 1655 * @package WordPress
mp-wp_genesis 1656 * @subpackage Post
mp-wp_genesis 1657 * @since 2.3.0
mp-wp_genesis 1658 *
mp-wp_genesis 1659 * @param int $post_id Post ID
mp-wp_genesis 1660 * @param string $tags The tags to set for the post, separated by commas.
mp-wp_genesis 1661 * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise
mp-wp_genesis 1662 */
mp-wp_genesis 1663 function wp_add_post_tags($post_id = 0, $tags = '') {
mp-wp_genesis 1664 return wp_set_post_tags($post_id, $tags, true);
mp-wp_genesis 1665 }
mp-wp_genesis 1666
mp-wp_genesis 1667
mp-wp_genesis 1668 /**
mp-wp_genesis 1669 * Set the tags for a post.
mp-wp_genesis 1670 *
mp-wp_genesis 1671 * @since 2.3.0
mp-wp_genesis 1672 * @uses wp_set_object_terms() Sets the tags for the post.
mp-wp_genesis 1673 *
mp-wp_genesis 1674 * @param int $post_id Post ID.
mp-wp_genesis 1675 * @param string $tags The tags to set for the post, separated by commas.
mp-wp_genesis 1676 * @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 1677 * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise
mp-wp_genesis 1678 */
mp-wp_genesis 1679 function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
mp-wp_genesis 1680
mp-wp_genesis 1681 $post_id = (int) $post_id;
mp-wp_genesis 1682
mp-wp_genesis 1683 if ( !$post_id )
mp-wp_genesis 1684 return false;
mp-wp_genesis 1685
mp-wp_genesis 1686 if ( empty($tags) )
mp-wp_genesis 1687 $tags = array();
mp-wp_genesis 1688 $tags = (is_array($tags)) ? $tags : explode( ',', trim($tags, " \n\t\r\0\x0B,") );
mp-wp_genesis 1689 wp_set_object_terms($post_id, $tags, 'post_tag', $append);
mp-wp_genesis 1690 }
mp-wp_genesis 1691
mp-wp_genesis 1692 /**
mp-wp_genesis 1693 * Set categories for a post.
mp-wp_genesis 1694 *
mp-wp_genesis 1695 * If the post categories parameter is not set, then the default category is
mp-wp_genesis 1696 * going used.
mp-wp_genesis 1697 *
mp-wp_genesis 1698 * @since 2.1.0
mp-wp_genesis 1699 *
mp-wp_genesis 1700 * @param int $post_ID Post ID.
mp-wp_genesis 1701 * @param array $post_categories Optional. List of categories.
mp-wp_genesis 1702 * @return bool|mixed
mp-wp_genesis 1703 */
mp-wp_genesis 1704 function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
mp-wp_genesis 1705 $post_ID = (int) $post_ID;
mp-wp_genesis 1706 // If $post_categories isn't already an array, make it one:
mp-wp_genesis 1707 if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories))
mp-wp_genesis 1708 $post_categories = array(get_option('default_category'));
mp-wp_genesis 1709 else if ( 1 == count($post_categories) && '' == $post_categories[0] )
mp-wp_genesis 1710 return true;
mp-wp_genesis 1711
mp-wp_genesis 1712 $post_categories = array_map('intval', $post_categories);
mp-wp_genesis 1713 $post_categories = array_unique($post_categories);
mp-wp_genesis 1714
mp-wp_genesis 1715 return wp_set_object_terms($post_ID, $post_categories, 'category');
mp-wp_genesis 1716 }
mp-wp_genesis 1717
mp-wp_genesis 1718 /**
mp-wp_genesis 1719 * Transition the post status of a post.
mp-wp_genesis 1720 *
mp-wp_genesis 1721 * Calls hooks to transition post status. If the new post status is not the same
mp-wp_genesis 1722 * as the previous post status, then two hooks will be ran, the first is
mp-wp_genesis 1723 * 'transition_post_status' with new status, old status, and post data. The
mp-wp_genesis 1724 * next action called is 'OLDSTATUS_to_NEWSTATUS' the NEWSTATUS is the
mp-wp_genesis 1725 * $new_status parameter and the OLDSTATUS is $old_status parameter; it has the
mp-wp_genesis 1726 * post data.
mp-wp_genesis 1727 *
mp-wp_genesis 1728 * The final action will run whether or not the post statuses are the same. The
mp-wp_genesis 1729 * action is named 'NEWSTATUS_POSTTYPE', NEWSTATUS is from the $new_status
mp-wp_genesis 1730 * parameter and POSTTYPE is post_type post data.
mp-wp_genesis 1731 *
mp-wp_genesis 1732 * @since 2.3.0
mp-wp_genesis 1733 *
mp-wp_genesis 1734 * @param string $new_status Transition to this post status.
mp-wp_genesis 1735 * @param string $old_status Previous post status.
mp-wp_genesis 1736 * @param object $post Post data.
mp-wp_genesis 1737 */
mp-wp_genesis 1738 function wp_transition_post_status($new_status, $old_status, $post) {
mp-wp_genesis 1739 if ( $new_status != $old_status ) {
mp-wp_genesis 1740 do_action('transition_post_status', $new_status, $old_status, $post);
mp-wp_genesis 1741 do_action("${old_status}_to_$new_status", $post);
mp-wp_genesis 1742 }
mp-wp_genesis 1743 do_action("${new_status}_$post->post_type", $post->ID, $post);
mp-wp_genesis 1744 }
mp-wp_genesis 1745
mp-wp_genesis 1746 //
mp-wp_genesis 1747 // Trackback and ping functions
mp-wp_genesis 1748 //
mp-wp_genesis 1749
mp-wp_genesis 1750 /**
mp-wp_genesis 1751 * Add a URL to those already pung.
mp-wp_genesis 1752 *
mp-wp_genesis 1753 * @since 1.5.0
mp-wp_genesis 1754 * @uses $wpdb
mp-wp_genesis 1755 *
mp-wp_genesis 1756 * @param int $post_id Post ID.
mp-wp_genesis 1757 * @param string $uri Ping URI.
mp-wp_genesis 1758 * @return int How many rows were updated.
mp-wp_genesis 1759 */
mp-wp_genesis 1760 function add_ping($post_id, $uri) {
mp-wp_genesis 1761 global $wpdb;
mp-wp_genesis 1762 $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
mp-wp_genesis 1763 $pung = trim($pung);
mp-wp_genesis 1764 $pung = preg_split('/\s/', $pung);
mp-wp_genesis 1765 $pung[] = $uri;
mp-wp_genesis 1766 $new = implode("\n", $pung);
mp-wp_genesis 1767 $new = apply_filters('add_ping', $new);
mp-wp_genesis 1768 // expected_slashed ($new)
mp-wp_genesis 1769 $new = stripslashes($new);
mp-wp_genesis 1770 return $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post_id ) );
mp-wp_genesis 1771 }
mp-wp_genesis 1772
mp-wp_genesis 1773 /**
mp-wp_genesis 1774 * Retrieve enclosures already enclosed for a post.
mp-wp_genesis 1775 *
mp-wp_genesis 1776 * @since 1.5.0
mp-wp_genesis 1777 * @uses $wpdb
mp-wp_genesis 1778 *
mp-wp_genesis 1779 * @param int $post_id Post ID.
mp-wp_genesis 1780 * @return array List of enclosures
mp-wp_genesis 1781 */
mp-wp_genesis 1782 function get_enclosed($post_id) {
mp-wp_genesis 1783 $custom_fields = get_post_custom( $post_id );
mp-wp_genesis 1784 $pung = array();
mp-wp_genesis 1785 if ( !is_array( $custom_fields ) )
mp-wp_genesis 1786 return $pung;
mp-wp_genesis 1787
mp-wp_genesis 1788 foreach ( $custom_fields as $key => $val ) {
mp-wp_genesis 1789 if ( 'enclosure' != $key || !is_array( $val ) )
mp-wp_genesis 1790 continue;
mp-wp_genesis 1791 foreach( $val as $enc ) {
mp-wp_genesis 1792 $enclosure = split( "\n", $enc );
mp-wp_genesis 1793 $pung[] = trim( $enclosure[ 0 ] );
mp-wp_genesis 1794 }
mp-wp_genesis 1795 }
mp-wp_genesis 1796 $pung = apply_filters('get_enclosed', $pung);
mp-wp_genesis 1797 return $pung;
mp-wp_genesis 1798 }
mp-wp_genesis 1799
mp-wp_genesis 1800 /**
mp-wp_genesis 1801 * Retrieve URLs already pinged for a post.
mp-wp_genesis 1802 *
mp-wp_genesis 1803 * @since 1.5.0
mp-wp_genesis 1804 * @uses $wpdb
mp-wp_genesis 1805 *
mp-wp_genesis 1806 * @param int $post_id Post ID.
mp-wp_genesis 1807 * @return array
mp-wp_genesis 1808 */
mp-wp_genesis 1809 function get_pung($post_id) {
mp-wp_genesis 1810 global $wpdb;
mp-wp_genesis 1811 $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
mp-wp_genesis 1812 $pung = trim($pung);
mp-wp_genesis 1813 $pung = preg_split('/\s/', $pung);
mp-wp_genesis 1814 $pung = apply_filters('get_pung', $pung);
mp-wp_genesis 1815 return $pung;
mp-wp_genesis 1816 }
mp-wp_genesis 1817
mp-wp_genesis 1818 /**
mp-wp_genesis 1819 * Retrieve URLs that need to be pinged.
mp-wp_genesis 1820 *
mp-wp_genesis 1821 * @since 1.5.0
mp-wp_genesis 1822 * @uses $wpdb
mp-wp_genesis 1823 *
mp-wp_genesis 1824 * @param int $post_id Post ID
mp-wp_genesis 1825 * @return array
mp-wp_genesis 1826 */
mp-wp_genesis 1827 function get_to_ping($post_id) {
mp-wp_genesis 1828 global $wpdb;
mp-wp_genesis 1829 $to_ping = $wpdb->get_var( $wpdb->prepare( "SELECT to_ping FROM $wpdb->posts WHERE ID = %d", $post_id ));
mp-wp_genesis 1830 $to_ping = trim($to_ping);
mp-wp_genesis 1831 $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
mp-wp_genesis 1832 $to_ping = apply_filters('get_to_ping', $to_ping);
mp-wp_genesis 1833 return $to_ping;
mp-wp_genesis 1834 }
mp-wp_genesis 1835
mp-wp_genesis 1836 /**
mp-wp_genesis 1837 * Do trackbacks for a list of URLs.
mp-wp_genesis 1838 *
mp-wp_genesis 1839 * @since 1.0.0
mp-wp_genesis 1840 *
mp-wp_genesis 1841 * @param string $tb_list Comma separated list of URLs
mp-wp_genesis 1842 * @param int $post_id Post ID
mp-wp_genesis 1843 */
mp-wp_genesis 1844 function trackback_url_list($tb_list, $post_id) {
mp-wp_genesis 1845 if ( ! empty( $tb_list ) ) {
mp-wp_genesis 1846 // get post data
mp-wp_genesis 1847 $postdata = wp_get_single_post($post_id, ARRAY_A);
mp-wp_genesis 1848
mp-wp_genesis 1849 // import postdata as variables
mp-wp_genesis 1850 extract($postdata, EXTR_SKIP);
mp-wp_genesis 1851
mp-wp_genesis 1852 // form an excerpt
mp-wp_genesis 1853 $excerpt = strip_tags($post_excerpt ? $post_excerpt : $post_content);
mp-wp_genesis 1854
mp-wp_genesis 1855 if (strlen($excerpt) > 255) {
mp-wp_genesis 1856 $excerpt = substr($excerpt,0,252) . '...';
mp-wp_genesis 1857 }
mp-wp_genesis 1858
mp-wp_genesis 1859 $trackback_urls = explode(',', $tb_list);
mp-wp_genesis 1860 foreach( (array) $trackback_urls as $tb_url) {
mp-wp_genesis 1861 $tb_url = trim($tb_url);
mp-wp_genesis 1862 trackback($tb_url, stripslashes($post_title), $excerpt, $post_id);
mp-wp_genesis 1863 }
mp-wp_genesis 1864 }
mp-wp_genesis 1865 }
mp-wp_genesis 1866
mp-wp_genesis 1867 //
mp-wp_genesis 1868 // Page functions
mp-wp_genesis 1869 //
mp-wp_genesis 1870
mp-wp_genesis 1871 /**
mp-wp_genesis 1872 * Get a list of page IDs.
mp-wp_genesis 1873 *
mp-wp_genesis 1874 * @since 2.0.0
mp-wp_genesis 1875 * @uses $wpdb
mp-wp_genesis 1876 *
mp-wp_genesis 1877 * @return array List of page IDs.
mp-wp_genesis 1878 */
mp-wp_genesis 1879 function get_all_page_ids() {
mp-wp_genesis 1880 global $wpdb;
mp-wp_genesis 1881
mp-wp_genesis 1882 if ( ! $page_ids = wp_cache_get('all_page_ids', 'posts') ) {
mp-wp_genesis 1883 $page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'");
mp-wp_genesis 1884 wp_cache_add('all_page_ids', $page_ids, 'posts');
mp-wp_genesis 1885 }
mp-wp_genesis 1886
mp-wp_genesis 1887 return $page_ids;
mp-wp_genesis 1888 }
mp-wp_genesis 1889
mp-wp_genesis 1890 /**
mp-wp_genesis 1891 * Retrieves page data given a page ID or page object.
mp-wp_genesis 1892 *
mp-wp_genesis 1893 * @since 1.5.1
mp-wp_genesis 1894 *
mp-wp_genesis 1895 * @param mixed $page Page object or page ID. Passed by reference.
mp-wp_genesis 1896 * @param string $output What to output. OBJECT, ARRAY_A, or ARRAY_N.
mp-wp_genesis 1897 * @param string $filter How the return value should be filtered.
mp-wp_genesis 1898 * @return mixed Page data.
mp-wp_genesis 1899 */
mp-wp_genesis 1900 function &get_page(&$page, $output = OBJECT, $filter = 'raw') {
mp-wp_genesis 1901 if ( empty($page) ) {
mp-wp_genesis 1902 if ( isset( $GLOBALS['page'] ) && isset( $GLOBALS['page']->ID ) ) {
mp-wp_genesis 1903 return get_post($GLOBALS['page'], $output, $filter);
mp-wp_genesis 1904 } else {
mp-wp_genesis 1905 $page = null;
mp-wp_genesis 1906 return $page;
mp-wp_genesis 1907 }
mp-wp_genesis 1908 }
mp-wp_genesis 1909
mp-wp_genesis 1910 $the_page = get_post($page, $output, $filter);
mp-wp_genesis 1911 return $the_page;
mp-wp_genesis 1912 }
mp-wp_genesis 1913
mp-wp_genesis 1914 /**
mp-wp_genesis 1915 * Retrieves a page given its path.
mp-wp_genesis 1916 *
mp-wp_genesis 1917 * @since 2.1.0
mp-wp_genesis 1918 * @uses $wpdb
mp-wp_genesis 1919 *
mp-wp_genesis 1920 * @param string $page_path Page path
mp-wp_genesis 1921 * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
mp-wp_genesis 1922 * @return mixed Null when complete.
mp-wp_genesis 1923 */
mp-wp_genesis 1924 function get_page_by_path($page_path, $output = OBJECT) {
mp-wp_genesis 1925 global $wpdb;
mp-wp_genesis 1926 $page_path = rawurlencode(urldecode($page_path));
mp-wp_genesis 1927 $page_path = str_replace('%2F', '/', $page_path);
mp-wp_genesis 1928 $page_path = str_replace('%20', ' ', $page_path);
mp-wp_genesis 1929 $page_paths = '/' . trim($page_path, '/');
mp-wp_genesis 1930 $leaf_path = sanitize_title(basename($page_paths));
mp-wp_genesis 1931 $page_paths = explode('/', $page_paths);
mp-wp_genesis 1932 $full_path = '';
mp-wp_genesis 1933 foreach( (array) $page_paths as $pathdir)
mp-wp_genesis 1934 $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
mp-wp_genesis 1935
mp-wp_genesis 1936 $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 1937
mp-wp_genesis 1938 if ( empty($pages) )
mp-wp_genesis 1939 return null;
mp-wp_genesis 1940
mp-wp_genesis 1941 foreach ($pages as $page) {
mp-wp_genesis 1942 $path = '/' . $leaf_path;
mp-wp_genesis 1943 $curpage = $page;
mp-wp_genesis 1944 while ($curpage->post_parent != 0) {
mp-wp_genesis 1945 $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 1946 $path = '/' . $curpage->post_name . $path;
mp-wp_genesis 1947 }
mp-wp_genesis 1948
mp-wp_genesis 1949 if ( $path == $full_path )
mp-wp_genesis 1950 return get_page($page->ID, $output);
mp-wp_genesis 1951 }
mp-wp_genesis 1952
mp-wp_genesis 1953 return null;
mp-wp_genesis 1954 }
mp-wp_genesis 1955
mp-wp_genesis 1956 /**
mp-wp_genesis 1957 * Retrieve a page given its title.
mp-wp_genesis 1958 *
mp-wp_genesis 1959 * @since 2.1.0
mp-wp_genesis 1960 * @uses $wpdb
mp-wp_genesis 1961 *
mp-wp_genesis 1962 * @param string $page_title Page title
mp-wp_genesis 1963 * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
mp-wp_genesis 1964 * @return mixed
mp-wp_genesis 1965 */
mp-wp_genesis 1966 function get_page_by_title($page_title, $output = OBJECT) {
mp-wp_genesis 1967 global $wpdb;
mp-wp_genesis 1968 $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='page'", $page_title ));
mp-wp_genesis 1969 if ( $page )
mp-wp_genesis 1970 return get_page($page, $output);
mp-wp_genesis 1971
mp-wp_genesis 1972 return null;
mp-wp_genesis 1973 }
mp-wp_genesis 1974
mp-wp_genesis 1975 /**
mp-wp_genesis 1976 * Retrieve child pages from list of pages matching page ID.
mp-wp_genesis 1977 *
mp-wp_genesis 1978 * Matches against the pages parameter against the page ID. Also matches all
mp-wp_genesis 1979 * children for the same to retrieve all children of a page. Does not make any
mp-wp_genesis 1980 * SQL queries to get the children.
mp-wp_genesis 1981 *
mp-wp_genesis 1982 * @since 1.5.1
mp-wp_genesis 1983 *
mp-wp_genesis 1984 * @param int $page_id Page ID.
mp-wp_genesis 1985 * @param array $pages List of pages' objects.
mp-wp_genesis 1986 * @return array
mp-wp_genesis 1987 */
mp-wp_genesis 1988 function &get_page_children($page_id, $pages) {
mp-wp_genesis 1989 $page_list = array();
mp-wp_genesis 1990 foreach ( (array) $pages as $page ) {
mp-wp_genesis 1991 if ( $page->post_parent == $page_id ) {
mp-wp_genesis 1992 $page_list[] = $page;
mp-wp_genesis 1993 if ( $children = get_page_children($page->ID, $pages) )
mp-wp_genesis 1994 $page_list = array_merge($page_list, $children);
mp-wp_genesis 1995 }
mp-wp_genesis 1996 }
mp-wp_genesis 1997 return $page_list;
mp-wp_genesis 1998 }
mp-wp_genesis 1999
mp-wp_genesis 2000 /**
mp-wp_genesis 2001 * Order the pages with children under parents in a flat list.
mp-wp_genesis 2002 *
mp-wp_genesis 2003 * Fetches the pages returned as a FLAT list, but arranged in order of their
mp-wp_genesis 2004 * hierarchy, i.e., child parents immediately follow their parents.
mp-wp_genesis 2005 *
mp-wp_genesis 2006 * @since 2.0.0
mp-wp_genesis 2007 *
mp-wp_genesis 2008 * @param array $posts Posts array.
mp-wp_genesis 2009 * @param int $parent Parent page ID.
mp-wp_genesis 2010 * @return array
mp-wp_genesis 2011 */
mp-wp_genesis 2012 function get_page_hierarchy($posts, $parent = 0) {
mp-wp_genesis 2013 $result = array ( );
mp-wp_genesis 2014 if ($posts) { foreach ( (array) $posts as $post) {
mp-wp_genesis 2015 if ($post->post_parent == $parent) {
mp-wp_genesis 2016 $result[$post->ID] = $post->post_name;
mp-wp_genesis 2017 $children = get_page_hierarchy($posts, $post->ID);
mp-wp_genesis 2018 $result += $children; //append $children to $result
mp-wp_genesis 2019 }
mp-wp_genesis 2020 } }
mp-wp_genesis 2021 return $result;
mp-wp_genesis 2022 }
mp-wp_genesis 2023
mp-wp_genesis 2024 /**
mp-wp_genesis 2025 * Builds URI for a page.
mp-wp_genesis 2026 *
mp-wp_genesis 2027 * Sub pages will be in the "directory" under the parent page post name.
mp-wp_genesis 2028 *
mp-wp_genesis 2029 * @since 1.5.0
mp-wp_genesis 2030 *
mp-wp_genesis 2031 * @param int $page_id Page ID.
mp-wp_genesis 2032 * @return string Page URI.
mp-wp_genesis 2033 */
mp-wp_genesis 2034 function get_page_uri($page_id) {
mp-wp_genesis 2035 $page = get_page($page_id);
mp-wp_genesis 2036 $uri = $page->post_name;
mp-wp_genesis 2037
mp-wp_genesis 2038 // A page cannot be it's own parent.
mp-wp_genesis 2039 if ( $page->post_parent == $page->ID )
mp-wp_genesis 2040 return $uri;
mp-wp_genesis 2041
mp-wp_genesis 2042 while ($page->post_parent != 0) {
mp-wp_genesis 2043 $page = get_page($page->post_parent);
mp-wp_genesis 2044 $uri = $page->post_name . "/" . $uri;
mp-wp_genesis 2045 }
mp-wp_genesis 2046
mp-wp_genesis 2047 return $uri;
mp-wp_genesis 2048 }
mp-wp_genesis 2049
mp-wp_genesis 2050 /**
mp-wp_genesis 2051 * Retrieve a list of pages.
mp-wp_genesis 2052 *
mp-wp_genesis 2053 * The defaults that can be overridden are the following: 'child_of',
mp-wp_genesis 2054 * 'sort_order', 'sort_column', 'post_title', 'hierarchical', 'exclude',
mp-wp_genesis 2055 * 'include', 'meta_key', 'meta_value', and 'authors'.
mp-wp_genesis 2056 *
mp-wp_genesis 2057 * @since 1.5.0
mp-wp_genesis 2058 * @uses $wpdb
mp-wp_genesis 2059 *
mp-wp_genesis 2060 * @param mixed $args Optional. Array or string of options that overrides defaults.
mp-wp_genesis 2061 * @return array List of pages matching defaults or $args
mp-wp_genesis 2062 */
mp-wp_genesis 2063 function &get_pages($args = '') {
mp-wp_genesis 2064 global $wpdb;
mp-wp_genesis 2065
mp-wp_genesis 2066 $defaults = array(
mp-wp_genesis 2067 'child_of' => 0, 'sort_order' => 'ASC',
mp-wp_genesis 2068 'sort_column' => 'post_title', 'hierarchical' => 1,
mp-wp_genesis 2069 'exclude' => '', 'include' => '',
mp-wp_genesis 2070 'meta_key' => '', 'meta_value' => '',
mp-wp_genesis 2071 'authors' => '', 'parent' => -1, 'exclude_tree' => ''
mp-wp_genesis 2072 );
mp-wp_genesis 2073
mp-wp_genesis 2074 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 2075 extract( $r, EXTR_SKIP );
mp-wp_genesis 2076
mp-wp_genesis 2077 $key = md5( serialize( compact(array_keys($defaults)) ) );
mp-wp_genesis 2078 if ( $cache = wp_cache_get( 'get_pages', 'posts' ) ) {
mp-wp_genesis 2079 if ( isset( $cache[ $key ] ) ) {
mp-wp_genesis 2080 $pages = apply_filters('get_pages', $cache[ $key ], $r );
mp-wp_genesis 2081 return $pages;
mp-wp_genesis 2082 }
mp-wp_genesis 2083 }
mp-wp_genesis 2084
mp-wp_genesis 2085 $inclusions = '';
mp-wp_genesis 2086 if ( !empty($include) ) {
mp-wp_genesis 2087 $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include
mp-wp_genesis 2088 $parent = -1;
mp-wp_genesis 2089 $exclude = '';
mp-wp_genesis 2090 $meta_key = '';
mp-wp_genesis 2091 $meta_value = '';
mp-wp_genesis 2092 $hierarchical = false;
mp-wp_genesis 2093 $incpages = preg_split('/[\s,]+/',$include);
mp-wp_genesis 2094 if ( count($incpages) ) {
mp-wp_genesis 2095 foreach ( $incpages as $incpage ) {
mp-wp_genesis 2096 if (empty($inclusions))
mp-wp_genesis 2097 $inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpage);
mp-wp_genesis 2098 else
mp-wp_genesis 2099 $inclusions .= $wpdb->prepare(' OR ID = %d ', $incpage);
mp-wp_genesis 2100 }
mp-wp_genesis 2101 }
mp-wp_genesis 2102 }
mp-wp_genesis 2103 if (!empty($inclusions))
mp-wp_genesis 2104 $inclusions .= ')';
mp-wp_genesis 2105
mp-wp_genesis 2106 $exclusions = '';
mp-wp_genesis 2107 if ( !empty($exclude) ) {
mp-wp_genesis 2108 $expages = preg_split('/[\s,]+/',$exclude);
mp-wp_genesis 2109 if ( count($expages) ) {
mp-wp_genesis 2110 foreach ( $expages as $expage ) {
mp-wp_genesis 2111 if (empty($exclusions))
mp-wp_genesis 2112 $exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expage);
mp-wp_genesis 2113 else
mp-wp_genesis 2114 $exclusions .= $wpdb->prepare(' AND ID <> %d ', $expage);
mp-wp_genesis 2115 }
mp-wp_genesis 2116 }
mp-wp_genesis 2117 }
mp-wp_genesis 2118 if (!empty($exclusions))
mp-wp_genesis 2119 $exclusions .= ')';
mp-wp_genesis 2120
mp-wp_genesis 2121 $author_query = '';
mp-wp_genesis 2122 if (!empty($authors)) {
mp-wp_genesis 2123 $post_authors = preg_split('/[\s,]+/',$authors);
mp-wp_genesis 2124
mp-wp_genesis 2125 if ( count($post_authors) ) {
mp-wp_genesis 2126 foreach ( $post_authors as $post_author ) {
mp-wp_genesis 2127 //Do we have an author id or an author login?
mp-wp_genesis 2128 if ( 0 == intval($post_author) ) {
mp-wp_genesis 2129 $post_author = get_userdatabylogin($post_author);
mp-wp_genesis 2130 if ( empty($post_author) )
mp-wp_genesis 2131 continue;
mp-wp_genesis 2132 if ( empty($post_author->ID) )
mp-wp_genesis 2133 continue;
mp-wp_genesis 2134 $post_author = $post_author->ID;
mp-wp_genesis 2135 }
mp-wp_genesis 2136
mp-wp_genesis 2137 if ( '' == $author_query )
mp-wp_genesis 2138 $author_query = $wpdb->prepare(' post_author = %d ', $post_author);
mp-wp_genesis 2139 else
mp-wp_genesis 2140 $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author);
mp-wp_genesis 2141 }
mp-wp_genesis 2142 if ( '' != $author_query )
mp-wp_genesis 2143 $author_query = " AND ($author_query)";
mp-wp_genesis 2144 }
mp-wp_genesis 2145 }
mp-wp_genesis 2146
mp-wp_genesis 2147 $join = '';
mp-wp_genesis 2148 $where = "$exclusions $inclusions ";
mp-wp_genesis 2149 if ( ! empty( $meta_key ) || ! empty( $meta_value ) ) {
mp-wp_genesis 2150 $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )";
mp-wp_genesis 2151
mp-wp_genesis 2152 // meta_key and meta_value might be slashed
mp-wp_genesis 2153 $meta_key = stripslashes($meta_key);
mp-wp_genesis 2154 $meta_value = stripslashes($meta_value);
mp-wp_genesis 2155 if ( ! empty( $meta_key ) )
mp-wp_genesis 2156 $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key);
mp-wp_genesis 2157 if ( ! empty( $meta_value ) )
mp-wp_genesis 2158 $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value);
mp-wp_genesis 2159
mp-wp_genesis 2160 }
mp-wp_genesis 2161
mp-wp_genesis 2162 if ( $parent >= 0 )
mp-wp_genesis 2163 $where .= $wpdb->prepare(' AND post_parent = %d ', $parent);
mp-wp_genesis 2164
mp-wp_genesis 2165 $query = "SELECT * FROM $wpdb->posts $join WHERE (post_type = 'page' AND post_status = 'publish') $where ";
mp-wp_genesis 2166 $query .= $author_query;
mp-wp_genesis 2167 $query .= " ORDER BY " . $sort_column . " " . $sort_order ;
mp-wp_genesis 2168
mp-wp_genesis 2169 $pages = $wpdb->get_results($query);
mp-wp_genesis 2170
mp-wp_genesis 2171 if ( empty($pages) ) {
mp-wp_genesis 2172 $pages = apply_filters('get_pages', array(), $r);
mp-wp_genesis 2173 return $pages;
mp-wp_genesis 2174 }
mp-wp_genesis 2175
mp-wp_genesis 2176 // Update cache.
mp-wp_genesis 2177 update_page_cache($pages);
mp-wp_genesis 2178
mp-wp_genesis 2179 if ( $child_of || $hierarchical )
mp-wp_genesis 2180 $pages = & get_page_children($child_of, $pages);
mp-wp_genesis 2181
mp-wp_genesis 2182 if ( !empty($exclude_tree) ) {
mp-wp_genesis 2183 $exclude = array();
mp-wp_genesis 2184
mp-wp_genesis 2185 $exclude = (int) $exclude_tree;
mp-wp_genesis 2186 $children = get_page_children($exclude, $pages);
mp-wp_genesis 2187 $excludes = array();
mp-wp_genesis 2188 foreach ( $children as $child )
mp-wp_genesis 2189 $excludes[] = $child->ID;
mp-wp_genesis 2190 $excludes[] = $exclude;
mp-wp_genesis 2191 $total = count($pages);
mp-wp_genesis 2192 for ( $i = 0; $i < $total; $i++ ) {
mp-wp_genesis 2193 if ( in_array($pages[$i]->ID, $excludes) )
mp-wp_genesis 2194 unset($pages[$i]);
mp-wp_genesis 2195 }
mp-wp_genesis 2196 }
mp-wp_genesis 2197
mp-wp_genesis 2198 $cache[ $key ] = $pages;
mp-wp_genesis 2199 wp_cache_set( 'get_pages', $cache, 'posts' );
mp-wp_genesis 2200
mp-wp_genesis 2201 $pages = apply_filters('get_pages', $pages, $r);
mp-wp_genesis 2202
mp-wp_genesis 2203 return $pages;
mp-wp_genesis 2204 }
mp-wp_genesis 2205
mp-wp_genesis 2206 //
mp-wp_genesis 2207 // Attachment functions
mp-wp_genesis 2208 //
mp-wp_genesis 2209
mp-wp_genesis 2210 /**
mp-wp_genesis 2211 * Check if the attachment URI is local one and is really an attachment.
mp-wp_genesis 2212 *
mp-wp_genesis 2213 * @since 2.0.0
mp-wp_genesis 2214 *
mp-wp_genesis 2215 * @param string $url URL to check
mp-wp_genesis 2216 * @return bool True on success, false on failure.
mp-wp_genesis 2217 */
mp-wp_genesis 2218 function is_local_attachment($url) {
mp-wp_genesis 2219 if (strpos($url, get_bloginfo('url')) === false)
mp-wp_genesis 2220 return false;
mp-wp_genesis 2221 if (strpos($url, get_bloginfo('url') . '/?attachment_id=') !== false)
mp-wp_genesis 2222 return true;
mp-wp_genesis 2223 if ( $id = url_to_postid($url) ) {
mp-wp_genesis 2224 $post = & get_post($id);
mp-wp_genesis 2225 if ( 'attachment' == $post->post_type )
mp-wp_genesis 2226 return true;
mp-wp_genesis 2227 }
mp-wp_genesis 2228 return false;
mp-wp_genesis 2229 }
mp-wp_genesis 2230
mp-wp_genesis 2231 /**
mp-wp_genesis 2232 * Insert an attachment.
mp-wp_genesis 2233 *
mp-wp_genesis 2234 * If you set the 'ID' in the $object parameter, it will mean that you are
mp-wp_genesis 2235 * updating and attempt to update the attachment. You can also set the
mp-wp_genesis 2236 * attachment name or title by setting the key 'post_name' or 'post_title'.
mp-wp_genesis 2237 *
mp-wp_genesis 2238 * You can set the dates for the attachment manually by setting the 'post_date'
mp-wp_genesis 2239 * and 'post_date_gmt' keys' values.
mp-wp_genesis 2240 *
mp-wp_genesis 2241 * By default, the comments will use the default settings for whether the
mp-wp_genesis 2242 * comments are allowed. You can close them manually or keep them open by
mp-wp_genesis 2243 * setting the value for the 'comment_status' key.
mp-wp_genesis 2244 *
mp-wp_genesis 2245 * The $object parameter can have the following:
mp-wp_genesis 2246 * 'post_status' - Default is 'draft'. Can not be override, set the same as
mp-wp_genesis 2247 * parent post.
mp-wp_genesis 2248 * 'post_type' - Default is 'post', will be set to attachment. Can not
mp-wp_genesis 2249 * override.
mp-wp_genesis 2250 * 'post_author' - Default is current user ID. The ID of the user, who added
mp-wp_genesis 2251 * the attachment.
mp-wp_genesis 2252 * 'ping_status' - Default is the value in default ping status option.
mp-wp_genesis 2253 * Whether the attachment can accept pings.
mp-wp_genesis 2254 * 'post_parent' - Default is 0. Can use $parent parameter or set this for
mp-wp_genesis 2255 * the post it belongs to, if any.
mp-wp_genesis 2256 * 'menu_order' - Default is 0. The order it is displayed.
mp-wp_genesis 2257 * 'to_ping' - Whether to ping.
mp-wp_genesis 2258 * 'pinged' - Default is empty string.
mp-wp_genesis 2259 * 'post_password' - Default is empty string. The password to access the
mp-wp_genesis 2260 * attachment.
mp-wp_genesis 2261 * 'guid' - Global Unique ID for referencing the attachment.
mp-wp_genesis 2262 * 'post_content_filtered' - Attachment post content filtered.
mp-wp_genesis 2263 * 'post_excerpt' - Attachment excerpt.
mp-wp_genesis 2264 *
mp-wp_genesis 2265 * @since 2.0.0
mp-wp_genesis 2266 * @uses $wpdb
mp-wp_genesis 2267 * @uses $user_ID
mp-wp_genesis 2268 *
mp-wp_genesis 2269 * @param string|array $object Arguments to override defaults.
mp-wp_genesis 2270 * @param string $file Optional filename.
mp-wp_genesis 2271 * @param int $post_parent Parent post ID.
mp-wp_genesis 2272 * @return int Attachment ID.
mp-wp_genesis 2273 */
mp-wp_genesis 2274 function wp_insert_attachment($object, $file = false, $parent = 0) {
mp-wp_genesis 2275 global $wpdb, $user_ID;
mp-wp_genesis 2276
mp-wp_genesis 2277 $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID,
mp-wp_genesis 2278 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
mp-wp_genesis 2279 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '',
mp-wp_genesis 2280 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0);
mp-wp_genesis 2281
mp-wp_genesis 2282 $object = wp_parse_args($object, $defaults);
mp-wp_genesis 2283 if ( !empty($parent) )
mp-wp_genesis 2284 $object['post_parent'] = $parent;
mp-wp_genesis 2285
mp-wp_genesis 2286 $object = sanitize_post($object, 'db');
mp-wp_genesis 2287
mp-wp_genesis 2288 // export array as variables
mp-wp_genesis 2289 extract($object, EXTR_SKIP);
mp-wp_genesis 2290
mp-wp_genesis 2291 // Make sure we set a valid category
mp-wp_genesis 2292 if ( !isset($post_category) || 0 == count($post_category) || !is_array($post_category)) {
mp-wp_genesis 2293 $post_category = array(get_option('default_category'));
mp-wp_genesis 2294 }
mp-wp_genesis 2295
mp-wp_genesis 2296 if ( empty($post_author) )
mp-wp_genesis 2297 $post_author = $user_ID;
mp-wp_genesis 2298
mp-wp_genesis 2299 $post_type = 'attachment';
mp-wp_genesis 2300 $post_status = 'inherit';
mp-wp_genesis 2301
mp-wp_genesis 2302 // Are we updating or creating?
mp-wp_genesis 2303 if ( !empty($ID) ) {
mp-wp_genesis 2304 $update = true;
mp-wp_genesis 2305 $post_ID = (int) $ID;
mp-wp_genesis 2306 } else {
mp-wp_genesis 2307 $update = false;
mp-wp_genesis 2308 $post_ID = 0;
mp-wp_genesis 2309 }
mp-wp_genesis 2310
mp-wp_genesis 2311 // Create a valid post name.
mp-wp_genesis 2312 if ( empty($post_name) )
mp-wp_genesis 2313 $post_name = sanitize_title($post_title);
mp-wp_genesis 2314 else
mp-wp_genesis 2315 $post_name = sanitize_title($post_name);
mp-wp_genesis 2316
mp-wp_genesis 2317 // expected_slashed ($post_name)
mp-wp_genesis 2318 $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 2319
mp-wp_genesis 2320 if ($post_name_check) {
mp-wp_genesis 2321 $suffix = 2;
mp-wp_genesis 2322 while ($post_name_check) {
mp-wp_genesis 2323 $alt_post_name = $post_name . "-$suffix";
mp-wp_genesis 2324 // expected_slashed ($alt_post_name, $post_name)
mp-wp_genesis 2325 $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 2326 $suffix++;
mp-wp_genesis 2327 }
mp-wp_genesis 2328 $post_name = $alt_post_name;
mp-wp_genesis 2329 }
mp-wp_genesis 2330
mp-wp_genesis 2331 if ( empty($post_date) )
mp-wp_genesis 2332 $post_date = current_time('mysql');
mp-wp_genesis 2333 if ( empty($post_date_gmt) )
mp-wp_genesis 2334 $post_date_gmt = current_time('mysql', 1);
mp-wp_genesis 2335
mp-wp_genesis 2336 if ( empty($post_modified) )
mp-wp_genesis 2337 $post_modified = $post_date;
mp-wp_genesis 2338 if ( empty($post_modified_gmt) )
mp-wp_genesis 2339 $post_modified_gmt = $post_date_gmt;
mp-wp_genesis 2340
mp-wp_genesis 2341 if ( empty($comment_status) ) {
mp-wp_genesis 2342 if ( $update )
mp-wp_genesis 2343 $comment_status = 'closed';
mp-wp_genesis 2344 else
mp-wp_genesis 2345 $comment_status = get_option('default_comment_status');
mp-wp_genesis 2346 }
mp-wp_genesis 2347 if ( empty($ping_status) )
mp-wp_genesis 2348 $ping_status = get_option('default_ping_status');
mp-wp_genesis 2349
mp-wp_genesis 2350 if ( isset($to_ping) )
mp-wp_genesis 2351 $to_ping = preg_replace('|\s+|', "\n", $to_ping);
mp-wp_genesis 2352 else
mp-wp_genesis 2353 $to_ping = '';
mp-wp_genesis 2354
mp-wp_genesis 2355 if ( isset($post_parent) )
mp-wp_genesis 2356 $post_parent = (int) $post_parent;
mp-wp_genesis 2357 else
mp-wp_genesis 2358 $post_parent = 0;
mp-wp_genesis 2359
mp-wp_genesis 2360 if ( isset($menu_order) )
mp-wp_genesis 2361 $menu_order = (int) $menu_order;
mp-wp_genesis 2362 else
mp-wp_genesis 2363 $menu_order = 0;
mp-wp_genesis 2364
mp-wp_genesis 2365 if ( !isset($post_password) )
mp-wp_genesis 2366 $post_password = '';
mp-wp_genesis 2367
mp-wp_genesis 2368 if ( ! isset($pinged) )
mp-wp_genesis 2369 $pinged = '';
mp-wp_genesis 2370
mp-wp_genesis 2371 // expected_slashed (everything!)
mp-wp_genesis 2372 $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 2373 $data = stripslashes_deep( $data );
mp-wp_genesis 2374
mp-wp_genesis 2375 if ( $update ) {
mp-wp_genesis 2376 $wpdb->update( $wpdb->posts, $data, array( 'ID' => $post_ID ) );
mp-wp_genesis 2377 } else {
mp-wp_genesis 2378 // If there is a suggested ID, use it if not already present
mp-wp_genesis 2379 if ( !empty($import_id) ) {
mp-wp_genesis 2380 $import_id = (int) $import_id;
mp-wp_genesis 2381 if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {
mp-wp_genesis 2382 $data['ID'] = $import_id;
mp-wp_genesis 2383 }
mp-wp_genesis 2384 }
mp-wp_genesis 2385
mp-wp_genesis 2386 $wpdb->insert( $wpdb->posts, $data );
mp-wp_genesis 2387 $post_ID = (int) $wpdb->insert_id;
mp-wp_genesis 2388 }
mp-wp_genesis 2389
mp-wp_genesis 2390 if ( empty($post_name) ) {
mp-wp_genesis 2391 $post_name = sanitize_title($post_title, $post_ID);
mp-wp_genesis 2392 $wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) );
mp-wp_genesis 2393 }
mp-wp_genesis 2394
mp-wp_genesis 2395 wp_set_post_categories($post_ID, $post_category);
mp-wp_genesis 2396
mp-wp_genesis 2397 if ( $file )
mp-wp_genesis 2398 update_attached_file( $post_ID, $file );
mp-wp_genesis 2399
mp-wp_genesis 2400 clean_post_cache($post_ID);
mp-wp_genesis 2401
mp-wp_genesis 2402 if ( $update) {
mp-wp_genesis 2403 do_action('edit_attachment', $post_ID);
mp-wp_genesis 2404 } else {
mp-wp_genesis 2405 do_action('add_attachment', $post_ID);
mp-wp_genesis 2406 }
mp-wp_genesis 2407
mp-wp_genesis 2408 return $post_ID;
mp-wp_genesis 2409 }
mp-wp_genesis 2410
mp-wp_genesis 2411 /**
mp-wp_genesis 2412 * Delete an attachment.
mp-wp_genesis 2413 *
mp-wp_genesis 2414 * Will remove the file also, when the attachment is removed. Removes all post
mp-wp_genesis 2415 * meta fields, taxonomy, comments, etc associated with the attachment (except
mp-wp_genesis 2416 * the main post).
mp-wp_genesis 2417 *
mp-wp_genesis 2418 * @since 2.0.0
mp-wp_genesis 2419 * @uses $wpdb
mp-wp_genesis 2420 * @uses do_action() Calls 'delete_attachment' hook on Attachment ID.
mp-wp_genesis 2421 *
mp-wp_genesis 2422 * @param int $postid Attachment ID.
mp-wp_genesis 2423 * @return mixed False on failure. Post data on success.
mp-wp_genesis 2424 */
mp-wp_genesis 2425 function wp_delete_attachment($postid) {
mp-wp_genesis 2426 global $wpdb;
mp-wp_genesis 2427
mp-wp_genesis 2428 if ( !$post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) )
mp-wp_genesis 2429 return $post;
mp-wp_genesis 2430
mp-wp_genesis 2431 if ( 'attachment' != $post->post_type )
mp-wp_genesis 2432 return false;
mp-wp_genesis 2433
mp-wp_genesis 2434 $meta = wp_get_attachment_metadata( $postid );
mp-wp_genesis 2435 $file = get_attached_file( $postid );
mp-wp_genesis 2436
mp-wp_genesis 2437 /** @todo Delete for pluggable post taxonomies too */
mp-wp_genesis 2438 wp_delete_object_term_relationships($postid, array('category', 'post_tag'));
mp-wp_genesis 2439
mp-wp_genesis 2440 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid ));
mp-wp_genesis 2441
mp-wp_genesis 2442 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ));
mp-wp_genesis 2443
mp-wp_genesis 2444 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d ", $postid ));
mp-wp_genesis 2445
mp-wp_genesis 2446 $uploadPath = wp_upload_dir();
mp-wp_genesis 2447
mp-wp_genesis 2448 if ( ! empty($meta['thumb']) ) {
mp-wp_genesis 2449 // Don't delete the thumb if another attachment uses it
mp-wp_genesis 2450 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 2451 $thumbfile = str_replace(basename($file), $meta['thumb'], $file);
mp-wp_genesis 2452 $thumbfile = apply_filters('wp_delete_file', $thumbfile);
mp-wp_genesis 2453 @ unlink( path_join($uploadPath['basedir'], $thumbfile) );
mp-wp_genesis 2454 }
mp-wp_genesis 2455 }
mp-wp_genesis 2456
mp-wp_genesis 2457 // remove intermediate images if there are any
mp-wp_genesis 2458 $sizes = apply_filters('intermediate_image_sizes', array('thumbnail', 'medium', 'large'));
mp-wp_genesis 2459 foreach ( $sizes as $size ) {
mp-wp_genesis 2460 if ( $intermediate = image_get_intermediate_size($postid, $size) ) {
mp-wp_genesis 2461 $intermediate_file = apply_filters('wp_delete_file', $intermediate['path']);
mp-wp_genesis 2462 @ unlink( path_join($uploadPath['basedir'], $intermediate_file) );
mp-wp_genesis 2463 }
mp-wp_genesis 2464 }
mp-wp_genesis 2465
mp-wp_genesis 2466 $file = apply_filters('wp_delete_file', $file);
mp-wp_genesis 2467
mp-wp_genesis 2468 if ( ! empty($file) )
mp-wp_genesis 2469 @ unlink($file);
mp-wp_genesis 2470
mp-wp_genesis 2471 clean_post_cache($postid);
mp-wp_genesis 2472
mp-wp_genesis 2473 do_action('delete_attachment', $postid);
mp-wp_genesis 2474
mp-wp_genesis 2475 return $post;
mp-wp_genesis 2476 }
mp-wp_genesis 2477
mp-wp_genesis 2478 /**
mp-wp_genesis 2479 * Retrieve attachment meta field for attachment ID.
mp-wp_genesis 2480 *
mp-wp_genesis 2481 * @since 2.1.0
mp-wp_genesis 2482 *
mp-wp_genesis 2483 * @param int $post_id Attachment ID
mp-wp_genesis 2484 * @param bool $unfiltered Optional, default is false. If true, filters are not run.
mp-wp_genesis 2485 * @return string|bool Attachment meta field. False on failure.
mp-wp_genesis 2486 */
mp-wp_genesis 2487 function wp_get_attachment_metadata( $post_id, $unfiltered = false ) {
mp-wp_genesis 2488 $post_id = (int) $post_id;
mp-wp_genesis 2489 if ( !$post =& get_post( $post_id ) )
mp-wp_genesis 2490 return false;
mp-wp_genesis 2491
mp-wp_genesis 2492 $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
mp-wp_genesis 2493 if ( $unfiltered )
mp-wp_genesis 2494 return $data;
mp-wp_genesis 2495 return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID );
mp-wp_genesis 2496 }
mp-wp_genesis 2497
mp-wp_genesis 2498 /**
mp-wp_genesis 2499 * Update metadata for an attachment.
mp-wp_genesis 2500 *
mp-wp_genesis 2501 * @since 2.1.0
mp-wp_genesis 2502 *
mp-wp_genesis 2503 * @param int $post_id Attachment ID.
mp-wp_genesis 2504 * @param array $data Attachment data.
mp-wp_genesis 2505 * @return int
mp-wp_genesis 2506 */
mp-wp_genesis 2507 function wp_update_attachment_metadata( $post_id, $data ) {
mp-wp_genesis 2508 $post_id = (int) $post_id;
mp-wp_genesis 2509 if ( !$post =& get_post( $post_id ) )
mp-wp_genesis 2510 return false;
mp-wp_genesis 2511
mp-wp_genesis 2512 $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID );
mp-wp_genesis 2513
mp-wp_genesis 2514 return update_post_meta( $post->ID, '_wp_attachment_metadata', $data);
mp-wp_genesis 2515 }
mp-wp_genesis 2516
mp-wp_genesis 2517 /**
mp-wp_genesis 2518 * Retrieve the URL for an attachment.
mp-wp_genesis 2519 *
mp-wp_genesis 2520 * @since 2.1.0
mp-wp_genesis 2521 *
mp-wp_genesis 2522 * @param int $post_id Attachment ID.
mp-wp_genesis 2523 * @return string
mp-wp_genesis 2524 */
mp-wp_genesis 2525 function wp_get_attachment_url( $post_id = 0 ) {
mp-wp_genesis 2526 $post_id = (int) $post_id;
mp-wp_genesis 2527 if ( !$post =& get_post( $post_id ) )
mp-wp_genesis 2528 return false;
mp-wp_genesis 2529
mp-wp_genesis 2530 $url = '';
mp-wp_genesis 2531 if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) { //Get attached file
mp-wp_genesis 2532 if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { //Get upload directory
mp-wp_genesis 2533 if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location
mp-wp_genesis 2534 $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location
mp-wp_genesis 2535 }
mp-wp_genesis 2536 }
mp-wp_genesis 2537
mp-wp_genesis 2538 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 2539 $url = get_the_guid( $post->ID );
mp-wp_genesis 2540
mp-wp_genesis 2541 if ( 'attachment' != $post->post_type || empty($url) )
mp-wp_genesis 2542 return false;
mp-wp_genesis 2543
mp-wp_genesis 2544 return apply_filters( 'wp_get_attachment_url', $url, $post->ID );
mp-wp_genesis 2545 }
mp-wp_genesis 2546
mp-wp_genesis 2547 /**
mp-wp_genesis 2548 * Retrieve thumbnail for an attachment.
mp-wp_genesis 2549 *
mp-wp_genesis 2550 * @since 2.1.0
mp-wp_genesis 2551 *
mp-wp_genesis 2552 * @param int $post_id Attachment ID.
mp-wp_genesis 2553 * @return mixed False on failure. Thumbnail file path on success.
mp-wp_genesis 2554 */
mp-wp_genesis 2555 function wp_get_attachment_thumb_file( $post_id = 0 ) {
mp-wp_genesis 2556 $post_id = (int) $post_id;
mp-wp_genesis 2557 if ( !$post =& get_post( $post_id ) )
mp-wp_genesis 2558 return false;
mp-wp_genesis 2559 if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) )
mp-wp_genesis 2560 return false;
mp-wp_genesis 2561
mp-wp_genesis 2562 $file = get_attached_file( $post->ID );
mp-wp_genesis 2563
mp-wp_genesis 2564 if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) )
mp-wp_genesis 2565 return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );
mp-wp_genesis 2566 return false;
mp-wp_genesis 2567 }
mp-wp_genesis 2568
mp-wp_genesis 2569 /**
mp-wp_genesis 2570 * Retrieve URL for an attachment thumbnail.
mp-wp_genesis 2571 *
mp-wp_genesis 2572 * @since 2.1.0
mp-wp_genesis 2573 *
mp-wp_genesis 2574 * @param int $post_id Attachment ID
mp-wp_genesis 2575 * @return string|bool False on failure. Thumbnail URL on success.
mp-wp_genesis 2576 */
mp-wp_genesis 2577 function wp_get_attachment_thumb_url( $post_id = 0 ) {
mp-wp_genesis 2578 $post_id = (int) $post_id;
mp-wp_genesis 2579 if ( !$post =& get_post( $post_id ) )
mp-wp_genesis 2580 return false;
mp-wp_genesis 2581 if ( !$url = wp_get_attachment_url( $post->ID ) )
mp-wp_genesis 2582 return false;
mp-wp_genesis 2583
mp-wp_genesis 2584 $sized = image_downsize( $post_id, 'thumbnail' );
mp-wp_genesis 2585 if ( $sized )
mp-wp_genesis 2586 return $sized[0];
mp-wp_genesis 2587
mp-wp_genesis 2588 if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) )
mp-wp_genesis 2589 return false;
mp-wp_genesis 2590
mp-wp_genesis 2591 $url = str_replace(basename($url), basename($thumb), $url);
mp-wp_genesis 2592
mp-wp_genesis 2593 return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID );
mp-wp_genesis 2594 }
mp-wp_genesis 2595
mp-wp_genesis 2596 /**
mp-wp_genesis 2597 * Check if the attachment is an image.
mp-wp_genesis 2598 *
mp-wp_genesis 2599 * @since 2.1.0
mp-wp_genesis 2600 *
mp-wp_genesis 2601 * @param int $post_id Attachment ID
mp-wp_genesis 2602 * @return bool
mp-wp_genesis 2603 */
mp-wp_genesis 2604 function wp_attachment_is_image( $post_id = 0 ) {
mp-wp_genesis 2605 $post_id = (int) $post_id;
mp-wp_genesis 2606 if ( !$post =& get_post( $post_id ) )
mp-wp_genesis 2607 return false;
mp-wp_genesis 2608
mp-wp_genesis 2609 if ( !$file = get_attached_file( $post->ID ) )
mp-wp_genesis 2610 return false;
mp-wp_genesis 2611
mp-wp_genesis 2612 $ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false;
mp-wp_genesis 2613
mp-wp_genesis 2614 $image_exts = array('jpg', 'jpeg', 'gif', 'png');
mp-wp_genesis 2615
mp-wp_genesis 2616 if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) )
mp-wp_genesis 2617 return true;
mp-wp_genesis 2618 return false;
mp-wp_genesis 2619 }
mp-wp_genesis 2620
mp-wp_genesis 2621 /**
mp-wp_genesis 2622 * Retrieve the icon for a MIME type.
mp-wp_genesis 2623 *
mp-wp_genesis 2624 * @since 2.1.0
mp-wp_genesis 2625 *
mp-wp_genesis 2626 * @param string $mime MIME type
mp-wp_genesis 2627 * @return string|bool
mp-wp_genesis 2628 */
mp-wp_genesis 2629 function wp_mime_type_icon( $mime = 0 ) {
mp-wp_genesis 2630 if ( !is_numeric($mime) )
mp-wp_genesis 2631 $icon = wp_cache_get("mime_type_icon_$mime");
mp-wp_genesis 2632 if ( empty($icon) ) {
mp-wp_genesis 2633 $post_id = 0;
mp-wp_genesis 2634 $post_mimes = array();
mp-wp_genesis 2635 if ( is_numeric($mime) ) {
mp-wp_genesis 2636 $mime = (int) $mime;
mp-wp_genesis 2637 if ( $post =& get_post( $mime ) ) {
mp-wp_genesis 2638 $post_id = (int) $post->ID;
mp-wp_genesis 2639 $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $post->guid);
mp-wp_genesis 2640 if ( !empty($ext) ) {
mp-wp_genesis 2641 $post_mimes[] = $ext;
mp-wp_genesis 2642 if ( $ext_type = wp_ext2type( $ext ) )
mp-wp_genesis 2643 $post_mimes[] = $ext_type;
mp-wp_genesis 2644 }
mp-wp_genesis 2645 $mime = $post->post_mime_type;
mp-wp_genesis 2646 } else {
mp-wp_genesis 2647 $mime = 0;
mp-wp_genesis 2648 }
mp-wp_genesis 2649 } else {
mp-wp_genesis 2650 $post_mimes[] = $mime;
mp-wp_genesis 2651 }
mp-wp_genesis 2652
mp-wp_genesis 2653 $icon_files = wp_cache_get('icon_files');
mp-wp_genesis 2654
mp-wp_genesis 2655 if ( !is_array($icon_files) ) {
mp-wp_genesis 2656 $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' );
mp-wp_genesis 2657 $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url('images/crystal') );
mp-wp_genesis 2658 $dirs = apply_filters( 'icon_dirs', array($icon_dir => $icon_dir_uri) );
mp-wp_genesis 2659 $icon_files = array();
mp-wp_genesis 2660 while ( $dirs ) {
mp-wp_genesis 2661 $dir = array_shift($keys = array_keys($dirs));
mp-wp_genesis 2662 $uri = array_shift($dirs);
mp-wp_genesis 2663 if ( $dh = opendir($dir) ) {
mp-wp_genesis 2664 while ( false !== $file = readdir($dh) ) {
mp-wp_genesis 2665 $file = basename($file);
mp-wp_genesis 2666 if ( substr($file, 0, 1) == '.' )
mp-wp_genesis 2667 continue;
mp-wp_genesis 2668 if ( !in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg') ) ) {
mp-wp_genesis 2669 if ( is_dir("$dir/$file") )
mp-wp_genesis 2670 $dirs["$dir/$file"] = "$uri/$file";
mp-wp_genesis 2671 continue;
mp-wp_genesis 2672 }
mp-wp_genesis 2673 $icon_files["$dir/$file"] = "$uri/$file";
mp-wp_genesis 2674 }
mp-wp_genesis 2675 closedir($dh);
mp-wp_genesis 2676 }
mp-wp_genesis 2677 }
mp-wp_genesis 2678 wp_cache_set('icon_files', $icon_files, 600);
mp-wp_genesis 2679 }
mp-wp_genesis 2680
mp-wp_genesis 2681 // Icon basename - extension = MIME wildcard
mp-wp_genesis 2682 foreach ( $icon_files as $file => $uri )
mp-wp_genesis 2683 $types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file];
mp-wp_genesis 2684
mp-wp_genesis 2685 if ( ! empty($mime) ) {
mp-wp_genesis 2686 $post_mimes[] = substr($mime, 0, strpos($mime, '/'));
mp-wp_genesis 2687 $post_mimes[] = substr($mime, strpos($mime, '/') + 1);
mp-wp_genesis 2688 $post_mimes[] = str_replace('/', '_', $mime);
mp-wp_genesis 2689 }
mp-wp_genesis 2690
mp-wp_genesis 2691 $matches = wp_match_mime_types(array_keys($types), $post_mimes);
mp-wp_genesis 2692 $matches['default'] = array('default');
mp-wp_genesis 2693
mp-wp_genesis 2694 foreach ( $matches as $match => $wilds ) {
mp-wp_genesis 2695 if ( isset($types[$wilds[0]])) {
mp-wp_genesis 2696 $icon = $types[$wilds[0]];
mp-wp_genesis 2697 if ( !is_numeric($mime) )
mp-wp_genesis 2698 wp_cache_set("mime_type_icon_$mime", $icon);
mp-wp_genesis 2699 break;
mp-wp_genesis 2700 }
mp-wp_genesis 2701 }
mp-wp_genesis 2702 }
mp-wp_genesis 2703
mp-wp_genesis 2704 return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); // Last arg is 0 if function pass mime type.
mp-wp_genesis 2705 }
mp-wp_genesis 2706
mp-wp_genesis 2707 /**
mp-wp_genesis 2708 * Checked for changed slugs for published posts and save old slug.
mp-wp_genesis 2709 *
mp-wp_genesis 2710 * The function is used along with form POST data. It checks for the wp-old-slug
mp-wp_genesis 2711 * POST field. Will only be concerned with published posts and the slug actually
mp-wp_genesis 2712 * changing.
mp-wp_genesis 2713 *
mp-wp_genesis 2714 * If the slug was changed and not already part of the old slugs then it will be
mp-wp_genesis 2715 * added to the post meta field ('_wp_old_slug') for storing old slugs for that
mp-wp_genesis 2716 * post.
mp-wp_genesis 2717 *
mp-wp_genesis 2718 * The most logically usage of this function is redirecting changed posts, so
mp-wp_genesis 2719 * that those that linked to an changed post will be redirected to the new post.
mp-wp_genesis 2720 *
mp-wp_genesis 2721 * @since 2.1.0
mp-wp_genesis 2722 *
mp-wp_genesis 2723 * @param int $post_id Post ID.
mp-wp_genesis 2724 * @return int Same as $post_id
mp-wp_genesis 2725 */
mp-wp_genesis 2726 function wp_check_for_changed_slugs($post_id) {
mp-wp_genesis 2727 if ( !isset($_POST['wp-old-slug']) || !strlen($_POST['wp-old-slug']) )
mp-wp_genesis 2728 return $post_id;
mp-wp_genesis 2729
mp-wp_genesis 2730 $post = &get_post($post_id);
mp-wp_genesis 2731
mp-wp_genesis 2732 // we're only concerned with published posts
mp-wp_genesis 2733 if ( $post->post_status != 'publish' || $post->post_type != 'post' )
mp-wp_genesis 2734 return $post_id;
mp-wp_genesis 2735
mp-wp_genesis 2736 // only bother if the slug has changed
mp-wp_genesis 2737 if ( $post->post_name == $_POST['wp-old-slug'] )
mp-wp_genesis 2738 return $post_id;
mp-wp_genesis 2739
mp-wp_genesis 2740 $old_slugs = (array) get_post_meta($post_id, '_wp_old_slug');
mp-wp_genesis 2741
mp-wp_genesis 2742 // if we haven't added this old slug before, add it now
mp-wp_genesis 2743 if ( !count($old_slugs) || !in_array($_POST['wp-old-slug'], $old_slugs) )
mp-wp_genesis 2744 add_post_meta($post_id, '_wp_old_slug', $_POST['wp-old-slug']);
mp-wp_genesis 2745
mp-wp_genesis 2746 // if the new slug was used previously, delete it from the list
mp-wp_genesis 2747 if ( in_array($post->post_name, $old_slugs) )
mp-wp_genesis 2748 delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
mp-wp_genesis 2749
mp-wp_genesis 2750 return $post_id;
mp-wp_genesis 2751 }
mp-wp_genesis 2752
mp-wp_genesis 2753 /**
mp-wp_genesis 2754 * Retrieve the private post SQL based on capability.
mp-wp_genesis 2755 *
mp-wp_genesis 2756 * This function provides a standardized way to appropriately select on the
mp-wp_genesis 2757 * post_status of posts/pages. The function will return a piece of SQL code that
mp-wp_genesis 2758 * can be added to a WHERE clause; this SQL is constructed to allow all
mp-wp_genesis 2759 * published posts, and all private posts to which the user has access.
mp-wp_genesis 2760 *
mp-wp_genesis 2761 * It also allows plugins that define their own post type to control the cap by
mp-wp_genesis 2762 * using the hook 'pub_priv_sql_capability'. The plugin is expected to return
mp-wp_genesis 2763 * the capability the user must have to read the private post type.
mp-wp_genesis 2764 *
mp-wp_genesis 2765 * @since 2.2.0
mp-wp_genesis 2766 *
mp-wp_genesis 2767 * @uses $user_ID
mp-wp_genesis 2768 * @uses apply_filters() Call 'pub_priv_sql_capability' filter for plugins with different post types.
mp-wp_genesis 2769 *
mp-wp_genesis 2770 * @param string $post_type currently only supports 'post' or 'page'.
mp-wp_genesis 2771 * @return string SQL code that can be added to a where clause.
mp-wp_genesis 2772 */
mp-wp_genesis 2773 function get_private_posts_cap_sql($post_type) {
mp-wp_genesis 2774 global $user_ID;
mp-wp_genesis 2775 $cap = '';
mp-wp_genesis 2776
mp-wp_genesis 2777 // Private posts
mp-wp_genesis 2778 if ($post_type == 'post') {
mp-wp_genesis 2779 $cap = 'read_private_posts';
mp-wp_genesis 2780 // Private pages
mp-wp_genesis 2781 } elseif ($post_type == 'page') {
mp-wp_genesis 2782 $cap = 'read_private_pages';
mp-wp_genesis 2783 // Dunno what it is, maybe plugins have their own post type?
mp-wp_genesis 2784 } else {
mp-wp_genesis 2785 $cap = apply_filters('pub_priv_sql_capability', $cap);
mp-wp_genesis 2786
mp-wp_genesis 2787 if (empty($cap)) {
mp-wp_genesis 2788 // We don't know what it is, filters don't change anything,
mp-wp_genesis 2789 // so set the SQL up to return nothing.
mp-wp_genesis 2790 return '1 = 0';
mp-wp_genesis 2791 }
mp-wp_genesis 2792 }
mp-wp_genesis 2793
mp-wp_genesis 2794 $sql = '(post_status = \'publish\'';
mp-wp_genesis 2795
mp-wp_genesis 2796 if (current_user_can($cap)) {
mp-wp_genesis 2797 // Does the user have the capability to view private posts? Guess so.
mp-wp_genesis 2798 $sql .= ' OR post_status = \'private\'';
mp-wp_genesis 2799 } elseif (is_user_logged_in()) {
mp-wp_genesis 2800 // Users can view their own private posts.
mp-wp_genesis 2801 $sql .= ' OR post_status = \'private\' AND post_author = \'' . $user_ID . '\'';
mp-wp_genesis 2802 }
mp-wp_genesis 2803
mp-wp_genesis 2804 $sql .= ')';
mp-wp_genesis 2805
mp-wp_genesis 2806 return $sql;
mp-wp_genesis 2807 }
mp-wp_genesis 2808
mp-wp_genesis 2809 /**
mp-wp_genesis 2810 * Retrieve the date the the last post was published.
mp-wp_genesis 2811 *
mp-wp_genesis 2812 * The server timezone is the default and is the difference between GMT and
mp-wp_genesis 2813 * server time. The 'blog' value is the date when the last post was posted. The
mp-wp_genesis 2814 * 'gmt' is when the last post was posted in GMT formatted date.
mp-wp_genesis 2815 *
mp-wp_genesis 2816 * @since 0.71
mp-wp_genesis 2817 *
mp-wp_genesis 2818 * @uses $wpdb
mp-wp_genesis 2819 * @uses $blog_id
mp-wp_genesis 2820 * @uses apply_filters() Calls 'get_lastpostdate' filter
mp-wp_genesis 2821 *
mp-wp_genesis 2822 * @global mixed $cache_lastpostdate Stores the last post date
mp-wp_genesis 2823 * @global mixed $pagenow The current page being viewed
mp-wp_genesis 2824 *
mp-wp_genesis 2825 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
mp-wp_genesis 2826 * @return string The date of the last post.
mp-wp_genesis 2827 */
mp-wp_genesis 2828 function get_lastpostdate($timezone = 'server') {
mp-wp_genesis 2829 global $cache_lastpostdate, $wpdb, $blog_id;
mp-wp_genesis 2830 $add_seconds_server = date('Z');
mp-wp_genesis 2831 if ( !isset($cache_lastpostdate[$blog_id][$timezone]) ) {
mp-wp_genesis 2832 switch(strtolower($timezone)) {
mp-wp_genesis 2833 case 'gmt':
mp-wp_genesis 2834 $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 2835 break;
mp-wp_genesis 2836 case 'blog':
mp-wp_genesis 2837 $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 2838 break;
mp-wp_genesis 2839 case 'server':
mp-wp_genesis 2840 $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 2841 break;
mp-wp_genesis 2842 }
mp-wp_genesis 2843 $cache_lastpostdate[$blog_id][$timezone] = $lastpostdate;
mp-wp_genesis 2844 } else {
mp-wp_genesis 2845 $lastpostdate = $cache_lastpostdate[$blog_id][$timezone];
mp-wp_genesis 2846 }
mp-wp_genesis 2847 return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone );
mp-wp_genesis 2848 }
mp-wp_genesis 2849
mp-wp_genesis 2850 /**
mp-wp_genesis 2851 * Retrieve last post modified date depending on timezone.
mp-wp_genesis 2852 *
mp-wp_genesis 2853 * The server timezone is the default and is the difference between GMT and
mp-wp_genesis 2854 * server time. The 'blog' value is just when the last post was modified. The
mp-wp_genesis 2855 * 'gmt' is when the last post was modified in GMT time.
mp-wp_genesis 2856 *
mp-wp_genesis 2857 * @since 1.2.0
mp-wp_genesis 2858 * @uses $wpdb
mp-wp_genesis 2859 * @uses $blog_id
mp-wp_genesis 2860 * @uses apply_filters() Calls 'get_lastpostmodified' filter
mp-wp_genesis 2861 *
mp-wp_genesis 2862 * @global mixed $cache_lastpostmodified Stores the date the last post was modified
mp-wp_genesis 2863 *
mp-wp_genesis 2864 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
mp-wp_genesis 2865 * @return string The date the post was last modified.
mp-wp_genesis 2866 */
mp-wp_genesis 2867 function get_lastpostmodified($timezone = 'server') {
mp-wp_genesis 2868 global $cache_lastpostmodified, $wpdb, $blog_id;
mp-wp_genesis 2869 $add_seconds_server = date('Z');
mp-wp_genesis 2870 if ( !isset($cache_lastpostmodified[$blog_id][$timezone]) ) {
mp-wp_genesis 2871 switch(strtolower($timezone)) {
mp-wp_genesis 2872 case 'gmt':
mp-wp_genesis 2873 $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 2874 break;
mp-wp_genesis 2875 case 'blog':
mp-wp_genesis 2876 $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 2877 break;
mp-wp_genesis 2878 case 'server':
mp-wp_genesis 2879 $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 2880 break;
mp-wp_genesis 2881 }
mp-wp_genesis 2882 $lastpostdate = get_lastpostdate($timezone);
mp-wp_genesis 2883 if ( $lastpostdate > $lastpostmodified ) {
mp-wp_genesis 2884 $lastpostmodified = $lastpostdate;
mp-wp_genesis 2885 }
mp-wp_genesis 2886 $cache_lastpostmodified[$blog_id][$timezone] = $lastpostmodified;
mp-wp_genesis 2887 } else {
mp-wp_genesis 2888 $lastpostmodified = $cache_lastpostmodified[$blog_id][$timezone];
mp-wp_genesis 2889 }
mp-wp_genesis 2890 return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
mp-wp_genesis 2891 }
mp-wp_genesis 2892
mp-wp_genesis 2893 /**
mp-wp_genesis 2894 * Updates posts in cache.
mp-wp_genesis 2895 *
mp-wp_genesis 2896 * @usedby update_page_cache() Aliased by this function.
mp-wp_genesis 2897 *
mp-wp_genesis 2898 * @package WordPress
mp-wp_genesis 2899 * @subpackage Cache
mp-wp_genesis 2900 * @since 1.5.1
mp-wp_genesis 2901 *
mp-wp_genesis 2902 * @param array $posts Array of post objects
mp-wp_genesis 2903 */
mp-wp_genesis 2904 function update_post_cache(&$posts) {
mp-wp_genesis 2905 if ( !$posts )
mp-wp_genesis 2906 return;
mp-wp_genesis 2907
mp-wp_genesis 2908 foreach ( $posts as $post )
mp-wp_genesis 2909 wp_cache_add($post->ID, $post, 'posts');
mp-wp_genesis 2910 }
mp-wp_genesis 2911
mp-wp_genesis 2912 /**
mp-wp_genesis 2913 * Will clean the post in the cache.
mp-wp_genesis 2914 *
mp-wp_genesis 2915 * Cleaning means delete from the cache of the post. Will call to clean the term
mp-wp_genesis 2916 * object cache associated with the post ID.
mp-wp_genesis 2917 *
mp-wp_genesis 2918 * @package WordPress
mp-wp_genesis 2919 * @subpackage Cache
mp-wp_genesis 2920 * @since 2.0.0
mp-wp_genesis 2921 *
mp-wp_genesis 2922 * @uses do_action() Will call the 'clean_post_cache' hook action.
mp-wp_genesis 2923 *
mp-wp_genesis 2924 * @param int $id The Post ID in the cache to clean
mp-wp_genesis 2925 */
mp-wp_genesis 2926 function clean_post_cache($id) {
mp-wp_genesis 2927 global $_wp_suspend_cache_invalidation, $wpdb;
mp-wp_genesis 2928
mp-wp_genesis 2929 if ( !empty($_wp_suspend_cache_invalidation) )
mp-wp_genesis 2930 return;
mp-wp_genesis 2931
mp-wp_genesis 2932 $id = (int) $id;
mp-wp_genesis 2933
mp-wp_genesis 2934 wp_cache_delete($id, 'posts');
mp-wp_genesis 2935 wp_cache_delete($id, 'post_meta');
mp-wp_genesis 2936
mp-wp_genesis 2937 clean_object_term_cache($id, 'post');
mp-wp_genesis 2938
mp-wp_genesis 2939 wp_cache_delete( 'wp_get_archives', 'general' );
mp-wp_genesis 2940
mp-wp_genesis 2941 do_action('clean_post_cache', $id);
mp-wp_genesis 2942
mp-wp_genesis 2943 if ( $children = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d", $id) ) ) {
mp-wp_genesis 2944 foreach( $children as $cid )
mp-wp_genesis 2945 clean_post_cache( $cid );
mp-wp_genesis 2946 }
mp-wp_genesis 2947 }
mp-wp_genesis 2948
mp-wp_genesis 2949 /**
mp-wp_genesis 2950 * Alias of update_post_cache().
mp-wp_genesis 2951 *
mp-wp_genesis 2952 * @see update_post_cache() Posts and pages are the same, alias is intentional
mp-wp_genesis 2953 *
mp-wp_genesis 2954 * @package WordPress
mp-wp_genesis 2955 * @subpackage Cache
mp-wp_genesis 2956 * @since 1.5.1
mp-wp_genesis 2957 *
mp-wp_genesis 2958 * @param array $pages list of page objects
mp-wp_genesis 2959 */
mp-wp_genesis 2960 function update_page_cache(&$pages) {
mp-wp_genesis 2961 update_post_cache($pages);
mp-wp_genesis 2962 }
mp-wp_genesis 2963
mp-wp_genesis 2964 /**
mp-wp_genesis 2965 * Will clean the page in the cache.
mp-wp_genesis 2966 *
mp-wp_genesis 2967 * Clean (read: delete) page from cache that matches $id. Will also clean cache
mp-wp_genesis 2968 * associated with 'all_page_ids' and 'get_pages'.
mp-wp_genesis 2969 *
mp-wp_genesis 2970 * @package WordPress
mp-wp_genesis 2971 * @subpackage Cache
mp-wp_genesis 2972 * @since 2.0.0
mp-wp_genesis 2973 *
mp-wp_genesis 2974 * @uses do_action() Will call the 'clean_page_cache' hook action.
mp-wp_genesis 2975 *
mp-wp_genesis 2976 * @param int $id Page ID to clean
mp-wp_genesis 2977 */
mp-wp_genesis 2978 function clean_page_cache($id) {
mp-wp_genesis 2979 clean_post_cache($id);
mp-wp_genesis 2980
mp-wp_genesis 2981 wp_cache_delete( 'all_page_ids', 'posts' );
mp-wp_genesis 2982 wp_cache_delete( 'get_pages', 'posts' );
mp-wp_genesis 2983
mp-wp_genesis 2984 do_action('clean_page_cache', $id);
mp-wp_genesis 2985 }
mp-wp_genesis 2986
mp-wp_genesis 2987 /**
mp-wp_genesis 2988 * Call major cache updating functions for list of Post objects.
mp-wp_genesis 2989 *
mp-wp_genesis 2990 * @package WordPress
mp-wp_genesis 2991 * @subpackage Cache
mp-wp_genesis 2992 * @since 1.5.0
mp-wp_genesis 2993 *
mp-wp_genesis 2994 * @uses $wpdb
mp-wp_genesis 2995 * @uses update_post_cache()
mp-wp_genesis 2996 * @uses update_object_term_cache()
mp-wp_genesis 2997 * @uses update_postmeta_cache()
mp-wp_genesis 2998 *
mp-wp_genesis 2999 * @param array $posts Array of Post objects
mp-wp_genesis 3000 */
mp-wp_genesis 3001 function update_post_caches(&$posts) {
mp-wp_genesis 3002 // No point in doing all this work if we didn't match any posts.
mp-wp_genesis 3003 if ( !$posts )
mp-wp_genesis 3004 return;
mp-wp_genesis 3005
mp-wp_genesis 3006 update_post_cache($posts);
mp-wp_genesis 3007
mp-wp_genesis 3008 $post_ids = array();
mp-wp_genesis 3009
mp-wp_genesis 3010 for ($i = 0; $i < count($posts); $i++)
mp-wp_genesis 3011 $post_ids[] = $posts[$i]->ID;
mp-wp_genesis 3012
mp-wp_genesis 3013 update_object_term_cache($post_ids, 'post');
mp-wp_genesis 3014
mp-wp_genesis 3015 update_postmeta_cache($post_ids);
mp-wp_genesis 3016 }
mp-wp_genesis 3017
mp-wp_genesis 3018 /**
mp-wp_genesis 3019 * Updates metadata cache for list of post IDs.
mp-wp_genesis 3020 *
mp-wp_genesis 3021 * Performs SQL query to retrieve the metadata for the post IDs and updates the
mp-wp_genesis 3022 * metadata cache for the posts. Therefore, the functions, which call this
mp-wp_genesis 3023 * function, do not need to perform SQL queries on their own.
mp-wp_genesis 3024 *
mp-wp_genesis 3025 * @package WordPress
mp-wp_genesis 3026 * @subpackage Cache
mp-wp_genesis 3027 * @since 2.1.0
mp-wp_genesis 3028 *
mp-wp_genesis 3029 * @uses $wpdb
mp-wp_genesis 3030 *
mp-wp_genesis 3031 * @param array $post_ids List of post IDs.
mp-wp_genesis 3032 * @return bool|array Returns false if there is nothing to update or an array of metadata.
mp-wp_genesis 3033 */
mp-wp_genesis 3034 function update_postmeta_cache($post_ids) {
mp-wp_genesis 3035 global $wpdb;
mp-wp_genesis 3036
mp-wp_genesis 3037 if ( empty( $post_ids ) )
mp-wp_genesis 3038 return false;
mp-wp_genesis 3039
mp-wp_genesis 3040 if ( !is_array($post_ids) ) {
mp-wp_genesis 3041 $post_ids = preg_replace('|[^0-9,]|', '', $post_ids);
mp-wp_genesis 3042 $post_ids = explode(',', $post_ids);
mp-wp_genesis 3043 }
mp-wp_genesis 3044
mp-wp_genesis 3045 $post_ids = array_map('intval', $post_ids);
mp-wp_genesis 3046
mp-wp_genesis 3047 $ids = array();
mp-wp_genesis 3048 foreach ( (array) $post_ids as $id ) {
mp-wp_genesis 3049 if ( false === wp_cache_get($id, 'post_meta') )
mp-wp_genesis 3050 $ids[] = $id;
mp-wp_genesis 3051 }
mp-wp_genesis 3052
mp-wp_genesis 3053 if ( empty( $ids ) )
mp-wp_genesis 3054 return false;
mp-wp_genesis 3055
mp-wp_genesis 3056 // Get post-meta info
mp-wp_genesis 3057 $id_list = join(',', $ids);
mp-wp_genesis 3058 $cache = array();
mp-wp_genesis 3059 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 3060 foreach ( (array) $meta_list as $metarow) {
mp-wp_genesis 3061 $mpid = (int) $metarow['post_id'];
mp-wp_genesis 3062 $mkey = $metarow['meta_key'];
mp-wp_genesis 3063 $mval = $metarow['meta_value'];
mp-wp_genesis 3064
mp-wp_genesis 3065 // Force subkeys to be array type:
mp-wp_genesis 3066 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
mp-wp_genesis 3067 $cache[$mpid] = array();
mp-wp_genesis 3068 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
mp-wp_genesis 3069 $cache[$mpid][$mkey] = array();
mp-wp_genesis 3070
mp-wp_genesis 3071 // Add a value to the current pid/key:
mp-wp_genesis 3072 $cache[$mpid][$mkey][] = $mval;
mp-wp_genesis 3073 }
mp-wp_genesis 3074 }
mp-wp_genesis 3075
mp-wp_genesis 3076 foreach ( (array) $ids as $id ) {
mp-wp_genesis 3077 if ( ! isset($cache[$id]) )
mp-wp_genesis 3078 $cache[$id] = array();
mp-wp_genesis 3079 }
mp-wp_genesis 3080
mp-wp_genesis 3081 foreach ( (array) array_keys($cache) as $post)
mp-wp_genesis 3082 wp_cache_set($post, $cache[$post], 'post_meta');
mp-wp_genesis 3083
mp-wp_genesis 3084 return $cache;
mp-wp_genesis 3085 }
mp-wp_genesis 3086
mp-wp_genesis 3087 //
mp-wp_genesis 3088 // Hooks
mp-wp_genesis 3089 //
mp-wp_genesis 3090
mp-wp_genesis 3091 /**
mp-wp_genesis 3092 * Hook for managing future post transitions to published.
mp-wp_genesis 3093 *
mp-wp_genesis 3094 * @since 2.3.0
mp-wp_genesis 3095 * @access private
mp-wp_genesis 3096 * @uses $wpdb
mp-wp_genesis 3097 *
mp-wp_genesis 3098 * @param string $new_status New post status
mp-wp_genesis 3099 * @param string $old_status Previous post status
mp-wp_genesis 3100 * @param object $post Object type containing the post information
mp-wp_genesis 3101 */
mp-wp_genesis 3102 function _transition_post_status($new_status, $old_status, $post) {
mp-wp_genesis 3103 global $wpdb;
mp-wp_genesis 3104
mp-wp_genesis 3105 if ( $old_status != 'publish' && $new_status == 'publish' ) {
mp-wp_genesis 3106 // Reset GUID if transitioning to publish and it is empty
mp-wp_genesis 3107 if ( '' == get_the_guid($post->ID) )
mp-wp_genesis 3108 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) );
mp-wp_genesis 3109 do_action('private_to_published', $post->ID); // Deprecated, use private_to_publish
mp-wp_genesis 3110 }
mp-wp_genesis 3111
mp-wp_genesis 3112 // Always clears the hook in case the post status bounced from future to draft.
mp-wp_genesis 3113 wp_clear_scheduled_hook('publish_future_post', $post->ID);
mp-wp_genesis 3114 }
mp-wp_genesis 3115
mp-wp_genesis 3116 /**
mp-wp_genesis 3117 * Hook used to schedule publication for a post marked for the future.
mp-wp_genesis 3118 *
mp-wp_genesis 3119 * The $post properties used and must exist are 'ID' and 'post_date_gmt'.
mp-wp_genesis 3120 *
mp-wp_genesis 3121 * @since 2.3.0
mp-wp_genesis 3122 *
mp-wp_genesis 3123 * @param int $deprecated Not Used. Can be set to null.
mp-wp_genesis 3124 * @param object $post Object type containing the post information
mp-wp_genesis 3125 */
mp-wp_genesis 3126 function _future_post_hook($deprecated = '', $post) {
mp-wp_genesis 3127 wp_clear_scheduled_hook( 'publish_future_post', $post->ID );
mp-wp_genesis 3128 wp_schedule_single_event(strtotime($post->post_date_gmt. ' GMT'), 'publish_future_post', array($post->ID));
mp-wp_genesis 3129 }
mp-wp_genesis 3130
mp-wp_genesis 3131 /**
mp-wp_genesis 3132 * Hook to schedule pings and enclosures when a post is published.
mp-wp_genesis 3133 *
mp-wp_genesis 3134 * @since 2.3.0
mp-wp_genesis 3135 * @uses $wpdb
mp-wp_genesis 3136 * @uses XMLRPC_REQUEST
mp-wp_genesis 3137 * @uses APP_REQUEST
mp-wp_genesis 3138 * @uses do_action Calls 'xmlprc_publish_post' action if XMLRPC_REQUEST is defined. Calls 'app_publish_post'
mp-wp_genesis 3139 * action if APP_REQUEST is defined.
mp-wp_genesis 3140 *
mp-wp_genesis 3141 * @param int $post_id The ID in the database table of the post being published
mp-wp_genesis 3142 */
mp-wp_genesis 3143 function _publish_post_hook($post_id) {
mp-wp_genesis 3144 global $wpdb;
mp-wp_genesis 3145
mp-wp_genesis 3146 if ( defined('XMLRPC_REQUEST') )
mp-wp_genesis 3147 do_action('xmlrpc_publish_post', $post_id);
mp-wp_genesis 3148 if ( defined('APP_REQUEST') )
mp-wp_genesis 3149 do_action('app_publish_post', $post_id);
mp-wp_genesis 3150
mp-wp_genesis 3151 if ( defined('WP_IMPORTING') )
mp-wp_genesis 3152 return;
mp-wp_genesis 3153
mp-wp_genesis 3154 $data = array( 'post_id' => $post_id, 'meta_value' => '1' );
mp-wp_genesis 3155 if ( get_option('default_pingback_flag') )
mp-wp_genesis 3156 $wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_pingme' ) );
mp-wp_genesis 3157 $wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_encloseme' ) );
mp-wp_genesis 3158 wp_schedule_single_event(time(), 'do_pings');
mp-wp_genesis 3159 }
mp-wp_genesis 3160
mp-wp_genesis 3161 /**
mp-wp_genesis 3162 * Hook used to prevent page/post cache and rewrite rules from staying dirty.
mp-wp_genesis 3163 *
mp-wp_genesis 3164 * Does two things. If the post is a page and has a template then it will
mp-wp_genesis 3165 * update/add that template to the meta. For both pages and posts, it will clean
mp-wp_genesis 3166 * the post cache to make sure that the cache updates to the changes done
mp-wp_genesis 3167 * recently. For pages, the rewrite rules of WordPress are flushed to allow for
mp-wp_genesis 3168 * any changes.
mp-wp_genesis 3169 *
mp-wp_genesis 3170 * The $post parameter, only uses 'post_type' property and 'page_template'
mp-wp_genesis 3171 * property.
mp-wp_genesis 3172 *
mp-wp_genesis 3173 * @since 2.3.0
mp-wp_genesis 3174 * @uses $wp_rewrite Flushes Rewrite Rules.
mp-wp_genesis 3175 *
mp-wp_genesis 3176 * @param int $post_id The ID in the database table for the $post
mp-wp_genesis 3177 * @param object $post Object type containing the post information
mp-wp_genesis 3178 */
mp-wp_genesis 3179 function _save_post_hook($post_id, $post) {
mp-wp_genesis 3180 if ( $post->post_type == 'page' ) {
mp-wp_genesis 3181 clean_page_cache($post_id);
mp-wp_genesis 3182 // Avoid flushing rules for every post during import.
mp-wp_genesis 3183 if ( !defined('WP_IMPORTING') ) {
mp-wp_genesis 3184 global $wp_rewrite;
mp-wp_genesis 3185 $wp_rewrite->flush_rules();
mp-wp_genesis 3186 }
mp-wp_genesis 3187 } else {
mp-wp_genesis 3188 clean_post_cache($post_id);
mp-wp_genesis 3189 }
mp-wp_genesis 3190 }
mp-wp_genesis 3191
mp-wp_genesis 3192 /**
mp-wp_genesis 3193 * Retrieve post ancestors and append to post ancestors property.
mp-wp_genesis 3194 *
mp-wp_genesis 3195 * Will only retrieve ancestors once, if property is already set, then nothing
mp-wp_genesis 3196 * will be done. If there is not a parent post, or post ID and post parent ID
mp-wp_genesis 3197 * are the same then nothing will be done.
mp-wp_genesis 3198 *
mp-wp_genesis 3199 * The parameter is passed by reference, so nothing needs to be returned. The
mp-wp_genesis 3200 * property will be updated and can be referenced after the function is
mp-wp_genesis 3201 * complete. The post parent will be an ancestor and the parent of the post
mp-wp_genesis 3202 * parent will be an ancestor. There will only be two ancestors at the most.
mp-wp_genesis 3203 *
mp-wp_genesis 3204 * @access private
mp-wp_genesis 3205 * @since unknown
mp-wp_genesis 3206 * @uses $wpdb
mp-wp_genesis 3207 *
mp-wp_genesis 3208 * @param object $_post Post data.
mp-wp_genesis 3209 * @return null When nothing needs to be done.
mp-wp_genesis 3210 */
mp-wp_genesis 3211 function _get_post_ancestors(&$_post) {
mp-wp_genesis 3212 global $wpdb;
mp-wp_genesis 3213
mp-wp_genesis 3214 if ( isset($_post->ancestors) )
mp-wp_genesis 3215 return;
mp-wp_genesis 3216
mp-wp_genesis 3217 $_post->ancestors = array();
mp-wp_genesis 3218
mp-wp_genesis 3219 if ( empty($_post->post_parent) || $_post->ID == $_post->post_parent )
mp-wp_genesis 3220 return;
mp-wp_genesis 3221
mp-wp_genesis 3222 $id = $_post->ancestors[] = $_post->post_parent;
mp-wp_genesis 3223 while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) {
mp-wp_genesis 3224 if ( $id == $ancestor )
mp-wp_genesis 3225 break;
mp-wp_genesis 3226 $id = $_post->ancestors[] = $ancestor;
mp-wp_genesis 3227 }
mp-wp_genesis 3228 }
mp-wp_genesis 3229
mp-wp_genesis 3230 /**
mp-wp_genesis 3231 * Determines which fields of posts are to be saved in revisions.
mp-wp_genesis 3232 *
mp-wp_genesis 3233 * Does two things. If passed a post *array*, it will return a post array ready
mp-wp_genesis 3234 * to be insterted into the posts table as a post revision. Otherwise, returns
mp-wp_genesis 3235 * an array whose keys are the post fields to be saved for post revisions.
mp-wp_genesis 3236 *
mp-wp_genesis 3237 * @package WordPress
mp-wp_genesis 3238 * @subpackage Post_Revisions
mp-wp_genesis 3239 * @since 2.6.0
mp-wp_genesis 3240 * @access private
mp-wp_genesis 3241 *
mp-wp_genesis 3242 * @param array $post Optional a post array to be processed for insertion as a post revision.
mp-wp_genesis 3243 * @param bool $autosave optional Is the revision an autosave?
mp-wp_genesis 3244 * @return array Post array ready to be inserted as a post revision or array of fields that can be versioned.
mp-wp_genesis 3245 */
mp-wp_genesis 3246 function _wp_post_revision_fields( $post = null, $autosave = false ) {
mp-wp_genesis 3247 static $fields = false;
mp-wp_genesis 3248
mp-wp_genesis 3249 if ( !$fields ) {
mp-wp_genesis 3250 // Allow these to be versioned
mp-wp_genesis 3251 $fields = array(
mp-wp_genesis 3252 'post_title' => __( 'Title' ),
mp-wp_genesis 3253 'post_content' => __( 'Content' ),
mp-wp_genesis 3254 'post_excerpt' => __( 'Excerpt' ),
mp-wp_genesis 3255 );
mp-wp_genesis 3256
mp-wp_genesis 3257 // Runs only once
mp-wp_genesis 3258 $fields = apply_filters( '_wp_post_revision_fields', $fields );
mp-wp_genesis 3259
mp-wp_genesis 3260 // WP uses these internally either in versioning or elsewhere - they cannot be versioned
mp-wp_genesis 3261 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 3262 unset( $fields[$protect] );
mp-wp_genesis 3263 }
mp-wp_genesis 3264
mp-wp_genesis 3265 if ( !is_array($post) )
mp-wp_genesis 3266 return $fields;
mp-wp_genesis 3267
mp-wp_genesis 3268 $return = array();
mp-wp_genesis 3269 foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
mp-wp_genesis 3270 $return[$field] = $post[$field];
mp-wp_genesis 3271
mp-wp_genesis 3272 $return['post_parent'] = $post['ID'];
mp-wp_genesis 3273 $return['post_status'] = 'inherit';
mp-wp_genesis 3274 $return['post_type'] = 'revision';
mp-wp_genesis 3275 $return['post_name'] = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";
mp-wp_genesis 3276 $return['post_date'] = isset($post['post_modified']) ? $post['post_modified'] : '';
mp-wp_genesis 3277 $return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
mp-wp_genesis 3278
mp-wp_genesis 3279 return $return;
mp-wp_genesis 3280 }
mp-wp_genesis 3281
mp-wp_genesis 3282 /**
mp-wp_genesis 3283 * Saves an already existing post as a post revision.
mp-wp_genesis 3284 *
mp-wp_genesis 3285 * Typically used immediately prior to post updates.
mp-wp_genesis 3286 *
mp-wp_genesis 3287 * @package WordPress
mp-wp_genesis 3288 * @subpackage Post_Revisions
mp-wp_genesis 3289 * @since 2.6.0
mp-wp_genesis 3290 *
mp-wp_genesis 3291 * @uses _wp_put_post_revision()
mp-wp_genesis 3292 *
mp-wp_genesis 3293 * @param int $post_id The ID of the post to save as a revision.
mp-wp_genesis 3294 * @return mixed Null or 0 if error, new revision ID, if success.
mp-wp_genesis 3295 */
mp-wp_genesis 3296 function wp_save_post_revision( $post_id ) {
mp-wp_genesis 3297 // We do autosaves manually with wp_create_post_autosave()
mp-wp_genesis 3298 if ( @constant( 'DOING_AUTOSAVE' ) )
mp-wp_genesis 3299 return;
mp-wp_genesis 3300
mp-wp_genesis 3301 // WP_POST_REVISIONS = 0, false
mp-wp_genesis 3302 if ( !constant('WP_POST_REVISIONS') )
mp-wp_genesis 3303 return;
mp-wp_genesis 3304
mp-wp_genesis 3305 if ( !$post = get_post( $post_id, ARRAY_A ) )
mp-wp_genesis 3306 return;
mp-wp_genesis 3307
mp-wp_genesis 3308 if ( !in_array( $post['post_type'], array( 'post', 'page' ) ) )
mp-wp_genesis 3309 return;
mp-wp_genesis 3310
mp-wp_genesis 3311 $return = _wp_put_post_revision( $post );
mp-wp_genesis 3312
mp-wp_genesis 3313 // WP_POST_REVISIONS = true (default), -1
mp-wp_genesis 3314 if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 )
mp-wp_genesis 3315 return $return;
mp-wp_genesis 3316
mp-wp_genesis 3317 // all revisions and (possibly) one autosave
mp-wp_genesis 3318 $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
mp-wp_genesis 3319
mp-wp_genesis 3320 // WP_POST_REVISIONS = (int) (# of autasaves to save)
mp-wp_genesis 3321 $delete = count($revisions) - WP_POST_REVISIONS;
mp-wp_genesis 3322
mp-wp_genesis 3323 if ( $delete < 1 )
mp-wp_genesis 3324 return $return;
mp-wp_genesis 3325
mp-wp_genesis 3326 $revisions = array_slice( $revisions, 0, $delete );
mp-wp_genesis 3327
mp-wp_genesis 3328 for ( $i = 0; isset($revisions[$i]); $i++ ) {
mp-wp_genesis 3329 if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) )
mp-wp_genesis 3330 continue;
mp-wp_genesis 3331 wp_delete_post_revision( $revisions[$i]->ID );
mp-wp_genesis 3332 }
mp-wp_genesis 3333
mp-wp_genesis 3334 return $return;
mp-wp_genesis 3335 }
mp-wp_genesis 3336
mp-wp_genesis 3337 /**
mp-wp_genesis 3338 * Retrieve the autosaved data of the specified post.
mp-wp_genesis 3339 *
mp-wp_genesis 3340 * Returns a post object containing the information that was autosaved for the
mp-wp_genesis 3341 * specified post.
mp-wp_genesis 3342 *
mp-wp_genesis 3343 * @package WordPress
mp-wp_genesis 3344 * @subpackage Post_Revisions
mp-wp_genesis 3345 * @since 2.6.0
mp-wp_genesis 3346 *
mp-wp_genesis 3347 * @param int $post_id The post ID.
mp-wp_genesis 3348 * @return object|bool The autosaved data or false on failure or when no autosave exists.
mp-wp_genesis 3349 */
mp-wp_genesis 3350 function wp_get_post_autosave( $post_id ) {
mp-wp_genesis 3351
mp-wp_genesis 3352 if ( !$post = get_post( $post_id ) )
mp-wp_genesis 3353 return false;
mp-wp_genesis 3354
mp-wp_genesis 3355 $q = array(
mp-wp_genesis 3356 'name' => "{$post->ID}-autosave",
mp-wp_genesis 3357 'post_parent' => $post->ID,
mp-wp_genesis 3358 'post_type' => 'revision',
mp-wp_genesis 3359 'post_status' => 'inherit'
mp-wp_genesis 3360 );
mp-wp_genesis 3361
mp-wp_genesis 3362 // Use WP_Query so that the result gets cached
mp-wp_genesis 3363 $autosave_query = new WP_Query;
mp-wp_genesis 3364
mp-wp_genesis 3365 add_action( 'parse_query', '_wp_get_post_autosave_hack' );
mp-wp_genesis 3366 $autosave = $autosave_query->query( $q );
mp-wp_genesis 3367 remove_action( 'parse_query', '_wp_get_post_autosave_hack' );
mp-wp_genesis 3368
mp-wp_genesis 3369 if ( $autosave && is_array($autosave) && is_object($autosave[0]) )
mp-wp_genesis 3370 return $autosave[0];
mp-wp_genesis 3371
mp-wp_genesis 3372 return false;
mp-wp_genesis 3373 }
mp-wp_genesis 3374
mp-wp_genesis 3375 /**
mp-wp_genesis 3376 * Internally used to hack WP_Query into submission.
mp-wp_genesis 3377 *
mp-wp_genesis 3378 * @package WordPress
mp-wp_genesis 3379 * @subpackage Post_Revisions
mp-wp_genesis 3380 * @since 2.6.0
mp-wp_genesis 3381 *
mp-wp_genesis 3382 * @param object $query WP_Query object
mp-wp_genesis 3383 */
mp-wp_genesis 3384 function _wp_get_post_autosave_hack( $query ) {
mp-wp_genesis 3385 $query->is_single = false;
mp-wp_genesis 3386 }
mp-wp_genesis 3387
mp-wp_genesis 3388 /**
mp-wp_genesis 3389 * Determines if the specified post is a revision.
mp-wp_genesis 3390 *
mp-wp_genesis 3391 * @package WordPress
mp-wp_genesis 3392 * @subpackage Post_Revisions
mp-wp_genesis 3393 * @since 2.6.0
mp-wp_genesis 3394 *
mp-wp_genesis 3395 * @param int|object $post Post ID or post object.
mp-wp_genesis 3396 * @return bool|int False if not a revision, ID of revision's parent otherwise.
mp-wp_genesis 3397 */
mp-wp_genesis 3398 function wp_is_post_revision( $post ) {
mp-wp_genesis 3399 if ( !$post = wp_get_post_revision( $post ) )
mp-wp_genesis 3400 return false;
mp-wp_genesis 3401 return (int) $post->post_parent;
mp-wp_genesis 3402 }
mp-wp_genesis 3403
mp-wp_genesis 3404 /**
mp-wp_genesis 3405 * Determines if the specified post is an autosave.
mp-wp_genesis 3406 *
mp-wp_genesis 3407 * @package WordPress
mp-wp_genesis 3408 * @subpackage Post_Revisions
mp-wp_genesis 3409 * @since 2.6.0
mp-wp_genesis 3410 *
mp-wp_genesis 3411 * @param int|object $post Post ID or post object.
mp-wp_genesis 3412 * @return bool|int False if not a revision, ID of autosave's parent otherwise
mp-wp_genesis 3413 */
mp-wp_genesis 3414 function wp_is_post_autosave( $post ) {
mp-wp_genesis 3415 if ( !$post = wp_get_post_revision( $post ) )
mp-wp_genesis 3416 return false;
mp-wp_genesis 3417 if ( "{$post->post_parent}-autosave" !== $post->post_name )
mp-wp_genesis 3418 return false;
mp-wp_genesis 3419 return (int) $post->post_parent;
mp-wp_genesis 3420 }
mp-wp_genesis 3421
mp-wp_genesis 3422 /**
mp-wp_genesis 3423 * Inserts post data into the posts table as a post revision.
mp-wp_genesis 3424 *
mp-wp_genesis 3425 * @package WordPress
mp-wp_genesis 3426 * @subpackage Post_Revisions
mp-wp_genesis 3427 * @since 2.6.0
mp-wp_genesis 3428 *
mp-wp_genesis 3429 * @uses wp_insert_post()
mp-wp_genesis 3430 *
mp-wp_genesis 3431 * @param int|object|array $post Post ID, post object OR post array.
mp-wp_genesis 3432 * @param bool $autosave Optional. Is the revision an autosave?
mp-wp_genesis 3433 * @return mixed Null or 0 if error, new revision ID if success.
mp-wp_genesis 3434 */
mp-wp_genesis 3435 function _wp_put_post_revision( $post = null, $autosave = false ) {
mp-wp_genesis 3436 if ( is_object($post) )
mp-wp_genesis 3437 $post = get_object_vars( $post );
mp-wp_genesis 3438 elseif ( !is_array($post) )
mp-wp_genesis 3439 $post = get_post($post, ARRAY_A);
mp-wp_genesis 3440 if ( !$post || empty($post['ID']) )
mp-wp_genesis 3441 return;
mp-wp_genesis 3442
mp-wp_genesis 3443 if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
mp-wp_genesis 3444 return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
mp-wp_genesis 3445
mp-wp_genesis 3446 $post = _wp_post_revision_fields( $post, $autosave );
mp-wp_genesis 3447
mp-wp_genesis 3448 $revision_id = wp_insert_post( $post );
mp-wp_genesis 3449 if ( is_wp_error($revision_id) )
mp-wp_genesis 3450 return $revision_id;
mp-wp_genesis 3451
mp-wp_genesis 3452 if ( $revision_id )
mp-wp_genesis 3453 do_action( '_wp_put_post_revision', $revision_id );
mp-wp_genesis 3454 return $revision_id;
mp-wp_genesis 3455 }
mp-wp_genesis 3456
mp-wp_genesis 3457 /**
mp-wp_genesis 3458 * Gets a post revision.
mp-wp_genesis 3459 *
mp-wp_genesis 3460 * @package WordPress
mp-wp_genesis 3461 * @subpackage Post_Revisions
mp-wp_genesis 3462 * @since 2.6.0
mp-wp_genesis 3463 *
mp-wp_genesis 3464 * @uses get_post()
mp-wp_genesis 3465 *
mp-wp_genesis 3466 * @param int|object $post Post ID or post object
mp-wp_genesis 3467 * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
mp-wp_genesis 3468 * @param string $filter Optional sanitation filter. @see sanitize_post()
mp-wp_genesis 3469 * @return mixed Null if error or post object if success
mp-wp_genesis 3470 */
mp-wp_genesis 3471 function &wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
mp-wp_genesis 3472 $null = null;
mp-wp_genesis 3473 if ( !$revision = get_post( $post, OBJECT, $filter ) )
mp-wp_genesis 3474 return $revision;
mp-wp_genesis 3475 if ( 'revision' !== $revision->post_type )
mp-wp_genesis 3476 return $null;
mp-wp_genesis 3477
mp-wp_genesis 3478 if ( $output == OBJECT ) {
mp-wp_genesis 3479 return $revision;
mp-wp_genesis 3480 } elseif ( $output == ARRAY_A ) {
mp-wp_genesis 3481 $_revision = get_object_vars($revision);
mp-wp_genesis 3482 return $_revision;
mp-wp_genesis 3483 } elseif ( $output == ARRAY_N ) {
mp-wp_genesis 3484 $_revision = array_values(get_object_vars($revision));
mp-wp_genesis 3485 return $_revision;
mp-wp_genesis 3486 }
mp-wp_genesis 3487
mp-wp_genesis 3488 return $revision;
mp-wp_genesis 3489 }
mp-wp_genesis 3490
mp-wp_genesis 3491 /**
mp-wp_genesis 3492 * Restores a post to the specified revision.
mp-wp_genesis 3493 *
mp-wp_genesis 3494 * Can restore a past using all fields of the post revision, or only selected
mp-wp_genesis 3495 * fields.
mp-wp_genesis 3496 *
mp-wp_genesis 3497 * @package WordPress
mp-wp_genesis 3498 * @subpackage Post_Revisions
mp-wp_genesis 3499 * @since 2.6.0
mp-wp_genesis 3500 *
mp-wp_genesis 3501 * @uses wp_get_post_revision()
mp-wp_genesis 3502 * @uses wp_update_post()
mp-wp_genesis 3503 *
mp-wp_genesis 3504 * @param int|object $revision_id Revision ID or revision object.
mp-wp_genesis 3505 * @param array $fields Optional. What fields to restore from. Defaults to all.
mp-wp_genesis 3506 * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
mp-wp_genesis 3507 */
mp-wp_genesis 3508 function wp_restore_post_revision( $revision_id, $fields = null ) {
mp-wp_genesis 3509 if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
mp-wp_genesis 3510 return $revision;
mp-wp_genesis 3511
mp-wp_genesis 3512 if ( !is_array( $fields ) )
mp-wp_genesis 3513 $fields = array_keys( _wp_post_revision_fields() );
mp-wp_genesis 3514
mp-wp_genesis 3515 $update = array();
mp-wp_genesis 3516 foreach( array_intersect( array_keys( $revision ), $fields ) as $field )
mp-wp_genesis 3517 $update[$field] = $revision[$field];
mp-wp_genesis 3518
mp-wp_genesis 3519 if ( !$update )
mp-wp_genesis 3520 return false;
mp-wp_genesis 3521
mp-wp_genesis 3522 $update['ID'] = $revision['post_parent'];
mp-wp_genesis 3523
mp-wp_genesis 3524 $post_id = wp_update_post( $update );
mp-wp_genesis 3525 if ( is_wp_error( $post_id ) )
mp-wp_genesis 3526 return $post_id;
mp-wp_genesis 3527
mp-wp_genesis 3528 if ( $post_id )
mp-wp_genesis 3529 do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
mp-wp_genesis 3530
mp-wp_genesis 3531 return $post_id;
mp-wp_genesis 3532 }
mp-wp_genesis 3533
mp-wp_genesis 3534 /**
mp-wp_genesis 3535 * Deletes a revision.
mp-wp_genesis 3536 *
mp-wp_genesis 3537 * Deletes the row from the posts table corresponding to the specified revision.
mp-wp_genesis 3538 *
mp-wp_genesis 3539 * @package WordPress
mp-wp_genesis 3540 * @subpackage Post_Revisions
mp-wp_genesis 3541 * @since 2.6.0
mp-wp_genesis 3542 *
mp-wp_genesis 3543 * @uses wp_get_post_revision()
mp-wp_genesis 3544 * @uses wp_delete_post()
mp-wp_genesis 3545 *
mp-wp_genesis 3546 * @param int|object $revision_id Revision ID or revision object.
mp-wp_genesis 3547 * @param array $fields Optional. What fields to restore from. Defaults to all.
mp-wp_genesis 3548 * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
mp-wp_genesis 3549 */
mp-wp_genesis 3550 function wp_delete_post_revision( $revision_id ) {
mp-wp_genesis 3551 if ( !$revision = wp_get_post_revision( $revision_id ) )
mp-wp_genesis 3552 return $revision;
mp-wp_genesis 3553
mp-wp_genesis 3554 $delete = wp_delete_post( $revision->ID );
mp-wp_genesis 3555 if ( is_wp_error( $delete ) )
mp-wp_genesis 3556 return $delete;
mp-wp_genesis 3557
mp-wp_genesis 3558 if ( $delete )
mp-wp_genesis 3559 do_action( 'wp_delete_post_revision', $revision->ID, $revision );
mp-wp_genesis 3560
mp-wp_genesis 3561 return $delete;
mp-wp_genesis 3562 }
mp-wp_genesis 3563
mp-wp_genesis 3564 /**
mp-wp_genesis 3565 * Returns all revisions of specified post.
mp-wp_genesis 3566 *
mp-wp_genesis 3567 * @package WordPress
mp-wp_genesis 3568 * @subpackage Post_Revisions
mp-wp_genesis 3569 * @since 2.6.0
mp-wp_genesis 3570 *
mp-wp_genesis 3571 * @uses get_children()
mp-wp_genesis 3572 *
mp-wp_genesis 3573 * @param int|object $post_id Post ID or post object
mp-wp_genesis 3574 * @return array empty if no revisions
mp-wp_genesis 3575 */
mp-wp_genesis 3576 function wp_get_post_revisions( $post_id = 0, $args = null ) {
mp-wp_genesis 3577 if ( !constant('WP_POST_REVISIONS') )
mp-wp_genesis 3578 return array();
mp-wp_genesis 3579 if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) )
mp-wp_genesis 3580 return array();
mp-wp_genesis 3581
mp-wp_genesis 3582 $defaults = array( 'order' => 'DESC', 'orderby' => 'date' );
mp-wp_genesis 3583 $args = wp_parse_args( $args, $defaults );
mp-wp_genesis 3584 $args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) );
mp-wp_genesis 3585
mp-wp_genesis 3586 if ( !$revisions = get_children( $args ) )
mp-wp_genesis 3587 return array();
mp-wp_genesis 3588 return $revisions;
mp-wp_genesis 3589 }
mp-wp_genesis 3590
mp-wp_genesis 3591 function _set_preview($post) {
mp-wp_genesis 3592
mp-wp_genesis 3593 if ( ! is_object($post) )
mp-wp_genesis 3594 return $post;
mp-wp_genesis 3595
mp-wp_genesis 3596 $preview = wp_get_post_autosave($post->ID);
mp-wp_genesis 3597
mp-wp_genesis 3598 if ( ! is_object($preview) )
mp-wp_genesis 3599 return $post;
mp-wp_genesis 3600
mp-wp_genesis 3601 $preview = sanitize_post($preview);
mp-wp_genesis 3602
mp-wp_genesis 3603 $post->post_content = $preview->post_content;
mp-wp_genesis 3604 $post->post_title = $preview->post_title;
mp-wp_genesis 3605 $post->post_excerpt = $preview->post_excerpt;
mp-wp_genesis 3606
mp-wp_genesis 3607 return $post;
mp-wp_genesis 3608 }
mp-wp_genesis 3609
mp-wp_genesis 3610 function _show_post_preview() {
mp-wp_genesis 3611
mp-wp_genesis 3612 if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
mp-wp_genesis 3613 $id = (int) $_GET['preview_id'];
mp-wp_genesis 3614
mp-wp_genesis 3615 if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
mp-wp_genesis 3616 wp_die( __('You do not have permission to preview drafts.') );
mp-wp_genesis 3617
mp-wp_genesis 3618 add_filter('the_preview', '_set_preview');
mp-wp_genesis 3619 }
mp-wp_genesis 3620 }