wp/wp-includes/post.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Post functions and post utility function.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage Post
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
// Post Type Registration
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * Creates the initial post types when 'init' action is fired.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
function create_initial_post_types() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
	register_post_type( 'post', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
		'labels' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
			'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
		'public'  => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
		'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
		'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
		'capability_type' => 'post',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
		'map_meta_cap' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
		'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
		'rewrite' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
		'query_var' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
		'delete_with_user' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
		'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
	register_post_type( 'page', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
		'labels' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
			'name_admin_bar' => _x( 'Page', 'add new on admin bar' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
		'public' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
		'publicly_queryable' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
		'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
		'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
		'capability_type' => 'page',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
		'map_meta_cap' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
		'hierarchical' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
		'rewrite' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
		'query_var' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
		'delete_with_user' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
	register_post_type( 'attachment', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
		'labels' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
			'name' => _x('Media', 'post type general name'),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
			'name_admin_bar' => _x( 'Media', 'add new from admin bar' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
			'add_new' => _x( 'Add New', 'add new media' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
 			'edit_item' => __( 'Edit Media' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
 			'view_item' => __( 'View Attachment Page' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
		'public' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
		'show_ui' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
		'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
		'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		'capability_type' => 'post',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
		'capabilities' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
			'create_posts' => 'upload_files',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
		'map_meta_cap' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
		'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
		'rewrite' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
		'query_var' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
		'show_in_nav_menus' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
		'delete_with_user' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		'supports' => array( 'title', 'author', 'comments' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    77
	add_post_type_support( 'attachment:audio', 'thumbnail' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    78
	add_post_type_support( 'attachment:video', 'thumbnail' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
	register_post_type( 'revision', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
		'labels' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
			'name' => __( 'Revisions' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
			'singular_name' => __( 'Revision' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
		'public' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
		'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
		'_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
		'capability_type' => 'post',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
		'map_meta_cap' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
		'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
		'rewrite' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
		'query_var' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
		'can_export' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
		'delete_with_user' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
		'supports' => array( 'author' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
	register_post_type( 'nav_menu_item', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
		'labels' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
			'name' => __( 'Navigation Menu Items' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
			'singular_name' => __( 'Navigation Menu Item' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
		'public' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
		'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
		'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		'rewrite' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
		'delete_with_user' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
		'query_var' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
	register_post_status( 'publish', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
		'label'       => _x( 'Published', 'post' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
		'public'      => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
		'_builtin'    => true, /* internal use only. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
		'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
	register_post_status( 'future', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
		'label'       => _x( 'Scheduled', 'post' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		'protected'   => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
		'_builtin'    => true, /* internal use only. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
	register_post_status( 'draft', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
		'label'       => _x( 'Draft', 'post' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
		'protected'   => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
		'_builtin'    => true, /* internal use only. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
		'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
	register_post_status( 'pending', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
		'label'       => _x( 'Pending', 'post' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
		'protected'   => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
		'_builtin'    => true, /* internal use only. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
		'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
	register_post_status( 'private', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
		'label'       => _x( 'Private', 'post' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
		'private'     => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
		'_builtin'    => true, /* internal use only. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
	register_post_status( 'trash', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		'label'       => _x( 'Trash', 'post' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
		'internal'    => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
		'_builtin'    => true, /* internal use only. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
		'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
		'show_in_admin_status_list' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
	register_post_status( 'auto-draft', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
		'label'    => 'auto-draft',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
		'internal' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
		'_builtin' => true, /* internal use only. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
	register_post_status( 'inherit', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
		'label'    => 'inherit',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
		'internal' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
		'_builtin' => true, /* internal use only. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
		'exclude_from_search' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
 * Retrieve attached file path based on attachment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
 * By default the path will go through the 'get_attached_file' filter, but
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
 * passing a true to the $unfiltered argument of get_attached_file() will
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
 * return the file path unfiltered.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
 * The function works by getting the single post meta name, named
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
 * '_wp_attached_file' and returning it. This is a convenience function to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
 * prevent looking up the meta name and provide a mechanism for sending the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
 * attached filename through a filter.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   182
 * @param int  $attachment_id Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   183
 * @param bool $unfiltered    Optional. Whether to apply filters. Default false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
 * @return string|bool The file path to where the attached file should be, false otherwise.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
function get_attached_file( $attachment_id, $unfiltered = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
	$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   188
	// If the file is relative, prepend upload dir.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
	if ( $file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
		$file = $uploads['basedir'] . "/$file";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
	if ( $unfiltered )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
		return $file;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   193
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   194
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   195
	 * Filter the attached file based on the given ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   196
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   197
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   198
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   199
	 * @param string $file          Path to attached file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   200
	 * @param int    $attachment_id Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   201
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
	return apply_filters( 'get_attached_file', $file, $attachment_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
 * Update attachment file path based on attachment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
 * Used to update the file path of the attachment, which uses post meta name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
 * '_wp_attached_file' to store the path of the attachment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
 * @since 2.1.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   212
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   213
 * @param int    $attachment_id Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   214
 * @param string $file          File path for the attachment.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
 * @return bool True on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
function update_attached_file( $attachment_id, $file ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
	if ( !get_post( $attachment_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   221
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   222
	 * Filter the path to the attached file to update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   223
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   224
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   225
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   226
	 * @param string $file          Path to the attached file to update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   227
	 * @param int    $attachment_id Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   228
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
	$file = apply_filters( 'update_attached_file', $file, $attachment_id );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   230
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
	if ( $file = _wp_relative_upload_path( $file ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
		return update_post_meta( $attachment_id, '_wp_attached_file', $file );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
		return delete_post_meta( $attachment_id, '_wp_attached_file' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
 * Return relative path to an uploaded file.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
 * The path is relative to the current upload dir.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   243
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   244
 * @param string $path Full path to the file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   245
 * @return string Relative path on success, unchanged path on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
function _wp_relative_upload_path( $path ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
	$new_path = $path;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
	$uploads = wp_upload_dir();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
	if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
			$new_path = str_replace( $uploads['basedir'], '', $new_path );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
			$new_path = ltrim( $new_path, '/' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   256
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   257
	 * Filter the relative path to an uploaded file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   258
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   259
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   260
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   261
	 * @param string $new_path Relative path to the file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   262
	 * @param string $path     Full path to the file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   263
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
	return apply_filters( '_wp_relative_upload_path', $new_path, $path );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
 * Retrieve all children of the post parent ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
 * Normally, without any enhancements, the children would apply to pages. In the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
 * context of the inner workings of WordPress, pages, posts, and attachments
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
 * share the same table, so therefore the functionality could apply to any one
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
 * of them. It is then noted that while this function does not work on posts, it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
 * does not mean that it won't work on posts. It is recommended that you know
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
 * what context you wish to retrieve the children of.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
 * Attachments may also be made the child of a post, so if that is an accurate
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
 * statement (which needs to be verified), it would then be possible to get
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
 * all of the attachments for a post. Attachments have since changed since
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
 * version 2.5, so this is most likely unaccurate, but serves generally as an
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
 * example of what is possible.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
 * The arguments listed as defaults are for this function and also of the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
 * {@link get_posts()} function. The arguments are combined with the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
 * get_children defaults and are then passed to the {@link get_posts()}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
 * function, which accepts additional arguments. You can replace the defaults in
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
 * this function, listed below and the additional arguments listed in the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
 * {@link get_posts()} function.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
 * The 'post_parent' is the most important argument and important attention
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
 * needs to be paid to the $args parameter. If you pass either an object or an
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
 * integer (number), then just the 'post_parent' is grabbed and everything else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
 * is lost. If you don't specify any arguments, then it is assumed that you are
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
 * in The Loop and the post parent will be grabbed for from the current post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
 * The 'post_parent' argument is the ID to get the children. The 'numberposts'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
 * is the amount of posts to retrieve that has a default of '-1', which is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
 * used to get all of the posts. Giving a number higher than 0 will only
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
 * retrieve that amount of posts.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
 * The 'post_type' and 'post_status' arguments can be used to choose what
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
 * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
 * post types are 'post', 'pages', and 'attachments'. The 'post_status'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
 * argument will accept any post status within the write administration panels.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   308
 * @see get_posts()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   309
 * @todo Check validity of description.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   310
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   311
 * @param mixed  $args   Optional. User defined arguments for replacing the defaults. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   312
 * @param string $output Optional. Constant for return type. Accepts OBJECT, ARRAY_A, ARRAY_N.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   313
 *                       Default OBJECt.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   314
 * @return array Array of children, where the type of each element is determined by $output parameter.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   315
 *               Empty array on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   317
function get_children( $args = '', $output = OBJECT ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
	$kids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
	if ( empty( $args ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
		if ( isset( $GLOBALS['post'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
			$args = array('post_parent' => (int) $GLOBALS['post']->post_parent );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
			return $kids;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
	} elseif ( is_object( $args ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
		$args = array('post_parent' => (int) $args->post_parent );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
	} elseif ( is_numeric( $args ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
		$args = array('post_parent' => (int) $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
		'numberposts' => -1, 'post_type' => 'any',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
		'post_status' => 'any', 'post_parent' => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
	$r = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
	$children = get_posts( $r );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
	if ( ! $children )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
		return $kids;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
	if ( ! empty( $r['fields'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
		return $children;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
	update_post_cache($children);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
	foreach ( $children as $key => $child )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
		$kids[$child->ID] = $children[$key];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
	if ( $output == OBJECT ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
		return $kids;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
	} elseif ( $output == ARRAY_A ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   354
		$weeuns = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   355
		foreach ( (array) $kids as $kid ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
			$weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   357
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
		return $weeuns;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
	} elseif ( $output == ARRAY_N ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   360
		$babes = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   361
		foreach ( (array) $kids as $kid ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
			$babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   363
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
		return $babes;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
		return $kids;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
 * Get extended entry info (<!--more-->).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
 * There should not be any space after the second dash and before the word
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
 * 'more'. There can be text or space(s) after the word 'more', but won't be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
 * referenced.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
 * The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   378
 * the `<!--more-->`. The 'extended' key has the content after the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
 * `<!--more-->` comment. The 'more_text' key has the custom "Read More" text.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
 * @since 1.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
 * @param string $post Post content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
 * @return array Post before ('main'), after ('extended'), and custom readmore ('more_text').
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   386
function get_extended( $post ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   387
	//Match the new style more links.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
	if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
		list($main, $extended) = explode($matches[0], $post, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
		$more_text = $matches[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
		$main = $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
		$extended = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
		$more_text = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   397
	//  leading and trailing whitespace.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
	$main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
	$extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
	$more_text = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $more_text);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
	return array( 'main' => $main, 'extended' => $extended, 'more_text' => $more_text );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
 * Retrieves post data given a post ID or post object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
 * See {@link sanitize_post()} for optional $filter values. Also, the parameter
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
 * $post, must be given as a variable, since it is passed by reference.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
 * @since 1.5.1
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   412
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   413
 * @param int|WP_Post $post   Optional. Post ID or post object. Defaults to global $post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   414
 * @param string      $output Optional, default is Object. Accepts OBJECT, ARRAY_A, or ARRAY_N.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   415
 *                            Default OBJECT.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   416
 * @param string      $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   417
 *                            or 'display'. Default 'raw'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   418
 * @return WP_Post|array|null Type corresponding to $output on success or null on failure.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   419
 *                            When $output is OBJECT, a `WP_Post` instance is returned.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
	if ( empty( $post ) && isset( $GLOBALS['post'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
		$post = $GLOBALS['post'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   425
	if ( $post instanceof WP_Post ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
		$_post = $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
	} elseif ( is_object( $post ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
		if ( empty( $post->filter ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
			$_post = sanitize_post( $post, 'raw' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
			$_post = new WP_Post( $_post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
		} elseif ( 'raw' == $post->filter ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
			$_post = new WP_Post( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
			$_post = WP_Post::get_instance( $post->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
		$_post = WP_Post::get_instance( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
	if ( ! $_post )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
		return null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
	$_post = $_post->filter( $filter );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
	if ( $output == ARRAY_A )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
		return $_post->to_array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
	elseif ( $output == ARRAY_N )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
		return array_values( $_post->to_array() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
	return $_post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
 * WordPress Post class.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   458
 * @property-read array  $ancestors
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   459
 * @property-read string $page_template
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   460
 * @property-read int    $post_category
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   461
 * @property-read string $tag_input
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   462
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
final class WP_Post {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
	 * Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
	 * @var int
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
	public $ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
	 * ID of post author.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
	 * A numeric string, for compatibility reasons.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
	public $post_author = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
	 * The post's local publication time.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
	public $post_date = '0000-00-00 00:00:00';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
	 * The post's GMT publication time.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
	public $post_date_gmt = '0000-00-00 00:00:00';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
	 * The post's content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
	public $post_content = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
	 * The post's title.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
	public $post_title = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
	 * The post's excerpt.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
	public $post_excerpt = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
	 * The post's status.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
	public $post_status = 'publish';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
	 * Whether comments are allowed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
	public $comment_status = 'open';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
	 * Whether pings are allowed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
	public $ping_status = 'open';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
	 * The post's password in plain text.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
	public $post_password = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
	 * The post's slug.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
	public $post_name = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
	 * URLs queued to be pinged.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
	public $to_ping = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
	 * URLs that have been pinged.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
	public $pinged = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
	 * The post's local modified time.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
	public $post_modified = '0000-00-00 00:00:00';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
	 * The post's GMT modified time.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
	public $post_modified_gmt = '0000-00-00 00:00:00';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
	 * A utility DB field for post content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
	public $post_content_filtered = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
	 * ID of a post's parent post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
	 * @var int
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
	public $post_parent = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
	 * The unique identifier for a post, not necessarily a URL, used as the feed GUID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
	public $guid = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
	 * A field used for ordering posts.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
	 * @var int
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
	public $menu_order = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
	 * The post's type, like post or page.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
	public $post_type = 'post';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
	 * An attachment's mime type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
	public $post_mime_type = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
	 * Cached comment count.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
	 * A numeric string, for compatibility reasons.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
	public $comment_count = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
	 * Stores the post object's sanitization level.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
	 * Does not correspond to a DB field.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
	public $filter;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   641
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   642
	 * Retrieve WP_Post instance.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   643
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   644
	 * @static
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   645
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   646
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   647
	 * @param int $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   648
	 * @return WP_Post|bool Post object, false otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   649
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
	public static function get_instance( $post_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
		$post_id = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
		if ( ! $post_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
		$_post = wp_cache_get( $post_id, 'posts' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
		if ( ! $_post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
			$_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
			if ( ! $_post )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
			$_post = sanitize_post( $_post, 'raw' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
			wp_cache_add( $_post->ID, $_post, 'posts' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
		} elseif ( empty( $_post->filter ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
			$_post = sanitize_post( $_post, 'raw' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
		return new WP_Post( $_post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   674
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   675
	 * Constructor.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   676
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   677
	 * @param WP_Post $post Post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   678
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
	public function __construct( $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
		foreach ( get_object_vars( $post ) as $key => $value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
			$this->$key = $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   684
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   685
	 * Isset-er.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   686
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   687
	 * @param string $key Property to check if set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   688
	 * @return bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   689
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
	public function __isset( $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
		if ( 'ancestors' == $key )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
		if ( 'page_template' == $key )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
			return ( 'page' == $this->post_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
		if ( 'post_category' == $key )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
		   return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
		if ( 'tags_input' == $key )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
		   return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
		return metadata_exists( 'post', $this->ID, $key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   706
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   707
	 * Getter.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   708
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   709
	 * @param string $key Key to get.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   710
	 * @return array|mixed
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   711
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
	public function __get( $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
		if ( 'page_template' == $key && $this->__isset( $key ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
			return get_post_meta( $this->ID, '_wp_page_template', true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
		if ( 'post_category' == $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
			if ( is_object_in_taxonomy( $this->post_type, 'category' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   719
				$terms = get_the_terms( $this, 'category' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
			if ( empty( $terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
				return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
			return wp_list_pluck( $terms, 'term_id' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
		if ( 'tags_input' == $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
			if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
				$terms = get_the_terms( $this, 'post_tag' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
			if ( empty( $terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
				return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
			return wp_list_pluck( $terms, 'name' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   737
		// Rest of the values need filtering.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
		if ( 'ancestors' == $key )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
			$value = get_post_ancestors( $this );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
			$value = get_post_meta( $this->ID, $key, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
		if ( $this->filter )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
			$value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
		return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   748
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   749
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   750
	 * {@Missing Summary}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   751
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   752
	 * @param string $filter Filter.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   753
	 * @return $this|array|bool|object|WP_Post
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   754
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
	public function filter( $filter ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
		if ( $this->filter == $filter )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
			return $this;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
		if ( $filter == 'raw' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
			return self::get_instance( $this->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
		return sanitize_post( $this, $filter );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   765
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   766
	 * Convert object to array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   767
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   768
	 * @return array Object as array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   769
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
	public function to_array() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
		$post = get_object_vars( $this );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
		foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
			if ( $this->__isset( $key ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
				$post[ $key ] = $this->__get( $key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
		return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
 * Retrieve ancestors of a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   787
 * @param int|WP_Post $post Post ID or post object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
 * @return array Ancestor IDs or empty array if none are found.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
function get_post_ancestors( $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
	$post = get_post( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
	if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
	$ancestors = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
	$id = $ancestors[] = $post->post_parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
	while ( $ancestor = get_post( $id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
		// Loop detection: If the ancestor has been seen before, break.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
		if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
		$id = $ancestors[] = $ancestor->post_parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
	return $ancestors;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
 * Retrieve data from a post field based on Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
 * Examples of the post field will be, 'post_type', 'post_status', 'post_content',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
 * etc and based off of the post object property or key names.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
 * The context values are based off of the taxonomy filter functions and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
 * supported values are found within those functions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   821
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   822
 * @see sanitize_post_field()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   823
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   824
 * @param string      $field   Post field name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   825
 * @param int|WP_Post $post    Post ID or post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   826
 * @param string      $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   827
 *                             or 'display'. Default 'display'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
 * @return string The value of the post field on success, empty string on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
function get_post_field( $field, $post, $context = 'display' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
	$post = get_post( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
	if ( !$post )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
	if ( !isset($post->$field) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
	return sanitize_post_field($field, $post->$field, $post->ID, $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
 * Retrieve the mime type of an attachment based on the ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
 * This function can be used with any post type, but it makes more sense with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
 * attachments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   850
 * @param int|WP_Post $ID Optional. Post ID or post object. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   851
 * @return string|false The mime type on success, false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   853
function get_post_mime_type( $ID = '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
	$post = get_post($ID);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
	if ( is_object($post) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
		return $post->post_mime_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
 * Retrieve the post status based on the Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
 * If the post ID is of an attachment, then the parent post status will be given
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
 * instead.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   870
 * @param int|WP_Post $ID Optional. Post ID or post object. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   871
 * @return string|false Post status on success, false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   873
function get_post_status( $ID = '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
	$post = get_post($ID);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
	if ( !is_object($post) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
	if ( 'attachment' == $post->post_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
		if ( 'private' == $post->post_status )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
			return 'private';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   883
		// Unattached attachments are assumed to be published.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
		if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
			return 'publish';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   887
		// Inherit status from the parent.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   888
		if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   889
			$parent_post_status = get_post_status( $post->post_parent );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   890
			if ( 'trash' == $parent_post_status ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   891
				return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   892
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   893
				return $parent_post_status;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   894
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   895
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   896
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
	return $post->post_status;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
 * Retrieve all of the WordPress supported post statuses.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
 * Posts have a limited set of valid status values, this provides the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   906
 * post_status values and descriptions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
 * @return array List of post statuses.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
function get_post_statuses() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   913
	$status = array(
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   914
		'draft'   => __( 'Draft' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   915
		'pending' => __( 'Pending Review' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   916
		'private' => __( 'Private' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   917
		'publish' => __( 'Published' )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   918
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   919
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
	return $status;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   924
 * Retrieve all of the WordPress support page statuses.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   926
 * Pages have a limited set of valid status values, this provides the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   927
 * post_status values and descriptions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   929
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   930
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   931
 * @return array List of page statuses.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
function get_page_statuses() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   934
	$status = array(
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   935
		'draft'   => __( 'Draft' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   936
		'private' => __( 'Private' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   937
		'publish' => __( 'Published' )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   938
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   939
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
	return $status;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
 * Register a post status. Do not use before init.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
 * A simple function for creating or modifying a post status based on the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
 * parameters given. The function will accept an array (second optional
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
 * parameter), along with a string for the post status name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   950
 * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
 * @uses $wp_post_statuses Inserts new post status object into the list
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   954
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   955
 * @param string $post_status Name of the post status.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   956
 * @param array|string $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   957
 *     Optional. Array or string of post status arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   958
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   959
 *     @type bool|string $label                     A descriptive name for the post status marked
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   960
 *                                                  for translation. Defaults to value of $post_status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   961
 *     @type bool|array  $label_count               Descriptive text to use for nooped plurals.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   962
 *                                                  Default array of $label, twice
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   963
 *     @type bool        $exclude_from_search       Whether to exclude posts with this post status
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   964
 *                                                  from search results. Default is value of $internal.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   965
 *     @type bool        $_builtin                  Whether the status is built-in. Core-use only.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   966
 *                                                  Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   967
 *     @type bool        $public                    Whether posts of this status should be shown
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   968
 *                                                  in the front end of the site. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   969
 *     @type bool        $internal                  Whether the status is for internal use only.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   970
 *                                                  Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   971
 *     @type bool        $protected                 Whether posts with this status should be protected.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   972
 *                                                  Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   973
 *     @type bool        $private                   Whether posts with this status should be private.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   974
 *                                                  Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   975
 *     @type bool        $publicly_queryable        Whether posts with this status should be publicly-
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   976
 *                                                  queryable. Default is value of $public.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   977
 *     @type bool        $show_in_admin_all_list    Whether to include posts in the edit listing for
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   978
 *                                                  their post type. Default is value of $internal.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   979
 *     @type bool        $show_in_admin_status_list Show in the list of statuses with post counts at
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   980
 *                                                  the top of the edit listings,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   981
 *                                                  e.g. All (12) | Published (9) | My Custom Status (2)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   982
 *                                                  Default is value of $internal.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   983
 * }
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   985
function register_post_status( $post_status, $args = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
	global $wp_post_statuses;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
	if (!is_array($wp_post_statuses))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
		$wp_post_statuses = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
	// Args prefixed with an underscore are reserved for internal use.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
		'label' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   994
		'label_count' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   995
		'exclude_from_search' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
		'_builtin' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   997
		'public' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   998
		'internal' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
		'protected' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
		'private' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
		'publicly_queryable' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
		'show_in_admin_status_list' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
		'show_in_admin_all_list' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
	$args = wp_parse_args($args, $defaults);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
	$args = (object) $args;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
	$post_status = sanitize_key($post_status);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
	$args->name = $post_status;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1011
	// Set various defaults.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
	if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
		$args->internal = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
	if ( null === $args->public  )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
		$args->public = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1017
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
	if ( null === $args->private  )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
		$args->private = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
	if ( null === $args->protected  )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
		$args->protected = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1023
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
	if ( null === $args->internal  )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
		$args->internal = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
	if ( null === $args->publicly_queryable )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
		$args->publicly_queryable = $args->public;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
	if ( null === $args->exclude_from_search )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
		$args->exclude_from_search = $args->internal;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
	if ( null === $args->show_in_admin_all_list )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1034
		$args->show_in_admin_all_list = !$args->internal;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
	if ( null === $args->show_in_admin_status_list )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
		$args->show_in_admin_status_list = !$args->internal;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
	if ( false === $args->label )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
		$args->label = $post_status;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
	if ( false === $args->label_count )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
		$args->label_count = array( $args->label, $args->label );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
	$wp_post_statuses[$post_status] = $args;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
	return $args;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1051
 * Retrieve a post status object by name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1052
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1054
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1055
 * @global array $wp_post_statuses List of post statuses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1056
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1057
 * @see register_post_status()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1058
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1059
 * @param string $post_status The name of a registered post status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1060
 * @return object A post status object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
function get_post_status_object( $post_status ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
	global $wp_post_statuses;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
	if ( empty($wp_post_statuses[$post_status]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
		return null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
	return $wp_post_statuses[$post_status];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1072
 * Get a list of post statuses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1073
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1075
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1076
 * @global array $wp_post_statuses List of post statuses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1077
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1078
 * @see register_post_status()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1079
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1080
 * @param array|string $args     Optional. Array or string of post status arguments to compare against
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1081
 *                               properties of the global `$wp_post_statuses objects`. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1082
 * @param string       $output   Optional. The type of output to return, either 'names' or 'objects'. Default 'names'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1083
 * @param string       $operator Optional. The logical operation to perform. 'or' means only one element
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1084
 *                               from the array needs to match; 'and' means all elements must match.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1085
 *                               Default 'and'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1086
 * @return array A list of post status names or objects.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
	global $wp_post_statuses;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
	$field = ('names' == $output) ? 'name' : false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
	return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1095
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1096
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1097
 * Whether the post type is hierarchical.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1099
 * A false return value might also mean that the post type does not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1100
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1102
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1103
 * @see get_post_type_object()
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1104
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1105
 * @param string $post_type Post type name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1106
 * @return bool Whether post type is hierarchical.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1107
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1108
function is_post_type_hierarchical( $post_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1109
	if ( ! post_type_exists( $post_type ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1110
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1111
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1112
	$post_type = get_post_type_object( $post_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1113
	return $post_type->hierarchical;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1117
 * Check if a post type is registered.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1120
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1121
 * @see get_post_type_object()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1122
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1123
 * @param string $post_type Post type name.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
 * @return bool Whether post type is registered.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1125
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1126
function post_type_exists( $post_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
	return (bool) get_post_type_object( $post_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1128
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1130
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
 * Retrieve the post type of the current post or of a given post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1134
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1135
 * @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1136
 * @return string|false Post type on success, false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
function get_post_type( $post = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
	if ( $post = get_post( $post ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
		return $post->post_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1146
 * Retrieve a post type object by name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1147
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1149
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1150
 * @global array $wp_post_types List of post types.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1151
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1152
 * @see register_post_type()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1153
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1154
 * @param string $post_type The name of a registered post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1155
 * @return object A post type object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1156
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1157
function get_post_type_object( $post_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
	global $wp_post_types;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
	if ( empty($wp_post_types[$post_type]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
		return null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1162
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1163
	return $wp_post_types[$post_type];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1164
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
 * Get a list of all registered post type objects.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1169
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1170
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1171
 * @global array $wp_post_types List of post types.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1172
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1173
 * @see register_post_type() for accepted arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1174
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1175
 * @param array|string $args     Optional. An array of key => value arguments to match against
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1176
 *                               the post type objects. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1177
 * @param string       $output   Optional. The type of output to return. Accepts post type 'names'
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1178
 *                               or 'objects'. Default 'names'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1179
 * @param string       $operator Optional. The logical operation to perform. 'or' means only one
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1180
 *                               element from the array needs to match; 'and' means all elements
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1181
 *                               must match. Accepts 'or' or 'and'. Default 'and'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1182
 * @return array A list of post type names or objects.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
	global $wp_post_types;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
	$field = ('names' == $output) ? 'name' : false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
	return wp_filter_object_list($wp_post_types, $args, $operator, $field);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
 * Register a post type. Do not use before init.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
 * A function for creating or modifying a post type based on the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
 * parameters given. The function will accept an array (second optional
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
 * parameter), along with a string for the post type name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1200
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1201
 * @global array      $wp_post_types List of post types.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1202
 * @global WP_Rewrite $wp_rewrite    Used for default feeds.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1203
 * @global WP         $wp            Used to add query vars.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
 * @param string $post_type Post type key, must not exceed 20 characters.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1206
 * @param array|string $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1207
 *     Array or string of arguments for registering a post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1208
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1209
 *     @type string      $label                Name of the post type shown in the menu. Usually plural.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1210
 *                                             Default is value of $labels['name'].
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1211
 *     @type array       $labels               An array of labels for this post type. If not set, post
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1212
 *                                             labels are inherited for non-hierarchical types and page
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1213
 *                                             labels for hierarchical ones. {@see get_post_type_labels()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1214
 *     @type string      $description          A short descriptive summary of what the post type is.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1215
 *                                             Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1216
 *     @type bool        $public               Whether a post type is intended for use publicly either via
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1217
 *                                             the admin interface or by front-end users. While the default
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1218
 *                                             settings of $exclude_from_search, $publicly_queryable, $show_ui,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1219
 *                                             and $show_in_nav_menus are inherited from public, each does not
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1220
 *                                             rely on this relationship and controls a very specific intention.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1221
 *                                             Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1222
 *     @type bool        $hierarchical         Whether the post type is hierarchical (e.g. page). Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1223
 *     @type bool        $exclude_from_search  Whether to exclude posts with this post type from front end search
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1224
 *                                             results. Default is the opposite value of $public.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1225
 *     @type bool        $publicly_queryable   Whether queries can be performed on the front end for the post type
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1226
 *                                             as part of {@see parse_request()}. Endpoints would include:
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1227
 *                                             * ?post_type={post_type_key}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1228
 *                                             * ?{post_type_key}={single_post_slug}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1229
 *                                             * ?{post_type_query_var}={single_post_slug}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1230
 *                                             If not set, the default is inherited from $public.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1231
 *     @type bool        $show_ui              Whether to generate a default UI for managing this post type in the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1232
 *                                             admin. Default is value of $public.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1233
 *     @type bool        $show_in_menu         Where to show the post type in the admin menu. To work, $show_ui
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1234
 *                                             must be true. If true, the post type is shown in its own top level
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1235
 *                                             menu. If false, no menu is shown. If a string of an existing top
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1236
 *                                             level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1237
 *                                             type will be placed as a sub-menu of that.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1238
 *                                             Default is value of $show_ui.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1239
 *     @type bool        $show_in_nav_menus    Makes this post type available for selection in navigation menus.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1240
 *                                             Default is value $public.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1241
 *     @type bool        $show_in_admin_bar    Makes this post type available via the admin bar. Default is value
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1242
 *                                             of $show_in_menu.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1243
 *     @type int         $menu_position        The position in the menu order the post type should appear. To work,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1244
 *                                             $show_in_menu must be true. Default null (at the bottom).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1245
 *     @type string      $menu_icon            The url to the icon to be used for this menu. Pass a base64-encoded
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1246
 *                                             SVG using a data URI, which will be colored to match the color scheme
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1247
 *                                             -- this should begin with 'data:image/svg+xml;base64,'. Pass the name
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1248
 *                                             of a Dashicons helper class to use a font icon, e.g.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1249
 *                                             'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1250
 *                                             so an icon can be added via CSS. Defaults to use the posts icon.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1251
 *     @type string      $capability_type      The string to use to build the read, edit, and delete capabilities.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1252
 *                                             May be passed as an array to allow for alternative plurals when using
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1253
 *                                             this argument as a base to construct the capabilities, e.g.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1254
 *                                             array('story', 'stories'). Default 'post'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1255
 *     @type array       $capabilities         Array of capabilities for this post type. $capability_type is used
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1256
 *                                             as a base to construct capabilities by default.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1257
 *                                             {@see get_post_type_capabilities()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1258
 *     @type bool        $map_meta_cap         Whether to use the internal default meta capability handling.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1259
 *                                             Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1260
 *     @type array       $supports             An alias for calling {@see add_post_type_support()} directly.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1261
 *                                             Defaults to array containing 'title' & 'editor'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1262
 *     @type callback    $register_meta_box_cb Provide a callback function that sets up the meta boxes for the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1263
 *                                             edit form. Do remove_meta_box() and add_meta_box() calls in the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1264
 *                                             callback. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1265
 *     @type array       $taxonomies           An array of taxonomy identifiers that will be registered for the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1266
 *                                             post type. Taxonomies can be registered later with
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1267
 *                                             {@see register_taxonomy()} or {@see register_taxonomy_for_object_type()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1268
 *                                             Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1269
 *     @type bool|string $has_archive          Whether there should be post type archives, or if a string, the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1270
 *                                             archive slug to use. Will generate the proper rewrite rules if
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1271
 *                                             $rewrite is enabled. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1272
 *     @type bool|array  $rewrite              {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1273
 *         Triggers the handling of rewrites for this post type. To prevent rewrite, set to false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1274
 *         Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1275
 *         passed with any of these keys:
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1276
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1277
 *         @type string $slug       Customize the permastruct slug. Defaults to $post_type key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1278
 *         @type bool   $with_front Whether the permastruct should be prepended with WP_Rewrite::$front.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1279
 *                                  Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1280
 *         @type bool   $feeds      Whether the feed permastruct should be built for this post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1281
 *                                  Default is value of $has_archive.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1282
 *         @type bool   $pages      Whether the permastruct should provide for pagination. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1283
 *         @type const  $ep_mask    Endpoint mask to assign. If not specified and permalink_epmask is set,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1284
 *                                  inherits from $permalink_epmask. If not specified and permalink_epmask
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1285
 *                                  is not set, defaults to EP_PERMALINK.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1286
 *     }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1287
 *     @type string|bool $query_var            Sets the query_var key for this post type. Defaults to $post_type
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1288
 *                                             key. If false, a post type cannot be loaded at
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1289
 *                                             ?{query_var}={post_slug}. If specified as a string, the query
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1290
 *                                             ?{query_var_string}={post_slug} will be valid.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1291
 *     @type bool        $can_export           Whether to allow this post type to be exported. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1292
 *     @type bool        $delete_with_user     Whether to delete posts of this type when deleting a user. If true,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1293
 *                                             posts of this type belonging to the user will be moved to trash
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1294
 *                                             when then user is deleted. If false, posts of this type belonging
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1295
 *                                             to the user will *not* be trashed or deleted. If not set (the default),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1296
 *                                             posts are trashed if post_type_supports('author'). Otherwise posts
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1297
 *                                             are not trashed or deleted. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1298
 *     @type bool        $_builtin             FOR INTERNAL USE ONLY! True if this post type is a native or
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1299
 *                                             "built-in" post_type. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1300
 *     @type string      $_edit_link           FOR INTERNAL USE ONLY! URL segment to use for edit link of
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1301
 *                                             this post type. Default 'post.php?post=%d'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1302
 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1303
 * @return object|WP_Error The registered post type object, or an error object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1304
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1305
function register_post_type( $post_type, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1306
	global $wp_post_types, $wp_rewrite, $wp;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1307
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1308
	if ( ! is_array( $wp_post_types ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1309
		$wp_post_types = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1310
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1311
	// Args prefixed with an underscore are reserved for internal use.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1312
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1313
		'labels'               => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1314
		'description'          => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1315
		'public'               => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1316
		'hierarchical'         => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1317
		'exclude_from_search'  => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1318
		'publicly_queryable'   => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1319
		'show_ui'              => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1320
		'show_in_menu'         => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1321
		'show_in_nav_menus'    => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1322
		'show_in_admin_bar'    => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1323
		'menu_position'        => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1324
		'menu_icon'            => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1325
		'capability_type'      => 'post',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1326
		'capabilities'         => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1327
		'map_meta_cap'         => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1328
		'supports'             => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1329
		'register_meta_box_cb' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1330
		'taxonomies'           => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1331
		'has_archive'          => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1332
		'rewrite'              => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1333
		'query_var'            => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1334
		'can_export'           => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1335
		'delete_with_user'     => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1336
		'_builtin'             => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1337
		'_edit_link'           => 'post.php?post=%d',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1338
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1339
	$args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1340
	$args = (object) $args;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1341
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1342
	$post_type = sanitize_key( $post_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1343
	$args->name = $post_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1344
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1345
	if ( empty( $post_type ) || strlen( $post_type ) > 20 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1346
		_doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1347
		return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1348
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1349
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1350
	// If not set, default to the setting for public.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1351
	if ( null === $args->publicly_queryable )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1352
		$args->publicly_queryable = $args->public;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1353
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1354
	// If not set, default to the setting for public.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1355
	if ( null === $args->show_ui )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1356
		$args->show_ui = $args->public;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1357
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1358
	// If not set, default to the setting for show_ui.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1359
	if ( null === $args->show_in_menu || ! $args->show_ui )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1360
		$args->show_in_menu = $args->show_ui;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1361
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1362
	// If not set, default to the whether the full UI is shown.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1363
	if ( null === $args->show_in_admin_bar )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1364
		$args->show_in_admin_bar = (bool) $args->show_in_menu;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1365
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1366
	// If not set, default to the setting for public.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1367
	if ( null === $args->show_in_nav_menus )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1368
		$args->show_in_nav_menus = $args->public;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1369
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1370
	// If not set, default to true if not public, false if public.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1371
	if ( null === $args->exclude_from_search )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1372
		$args->exclude_from_search = !$args->public;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1373
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1374
	// Back compat with quirky handling in version 3.0. #14122.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1375
	if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1376
		$args->map_meta_cap = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1377
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1378
	// If not set, default to false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1379
	if ( null === $args->map_meta_cap )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1380
		$args->map_meta_cap = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1381
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1382
	$args->cap = get_post_type_capabilities( $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1383
	unset( $args->capabilities );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1384
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1385
	if ( is_array( $args->capability_type ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1386
		$args->capability_type = $args->capability_type[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1387
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1388
	if ( ! empty( $args->supports ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1389
		add_post_type_support( $post_type, $args->supports );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1390
		unset( $args->supports );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1391
	} elseif ( false !== $args->supports ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1392
		// Add default features
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1393
		add_post_type_support( $post_type, array( 'title', 'editor' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1394
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1395
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1396
	if ( false !== $args->query_var && ! empty( $wp ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1397
		if ( true === $args->query_var )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1398
			$args->query_var = $post_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1399
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1400
			$args->query_var = sanitize_title_with_dashes( $args->query_var );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1401
		$wp->add_query_var( $args->query_var );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1402
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1403
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1404
	if ( false !== $args->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1405
		if ( ! is_array( $args->rewrite ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1406
			$args->rewrite = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1407
		if ( empty( $args->rewrite['slug'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1408
			$args->rewrite['slug'] = $post_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1409
		if ( ! isset( $args->rewrite['with_front'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1410
			$args->rewrite['with_front'] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1411
		if ( ! isset( $args->rewrite['pages'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1412
			$args->rewrite['pages'] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1413
		if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1414
			$args->rewrite['feeds'] = (bool) $args->has_archive;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1415
		if ( ! isset( $args->rewrite['ep_mask'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1416
			if ( isset( $args->permalink_epmask ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1417
				$args->rewrite['ep_mask'] = $args->permalink_epmask;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1418
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1419
				$args->rewrite['ep_mask'] = EP_PERMALINK;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1421
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1422
		if ( $args->hierarchical )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1423
			add_rewrite_tag( "%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&pagename=" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1424
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1425
			add_rewrite_tag( "%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1426
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1427
		if ( $args->has_archive ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1428
			$archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1429
			if ( $args->rewrite['with_front'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1430
				$archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1431
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1432
				$archive_slug = $wp_rewrite->root . $archive_slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1433
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1434
			add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1435
			if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1436
				$feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1437
				add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1438
				add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1439
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1440
			if ( $args->rewrite['pages'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1441
				add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1442
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1443
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1444
		$permastruct_args = $args->rewrite;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1445
		$permastruct_args['feed'] = $permastruct_args['feeds'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1446
		add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1447
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1448
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1449
	// Register the post type meta box if a custom callback was specified.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1450
	if ( $args->register_meta_box_cb )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1451
		add_action( 'add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1452
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1453
	$args->labels = get_post_type_labels( $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1454
	$args->label = $args->labels->name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1455
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1456
	$wp_post_types[ $post_type ] = $args;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1457
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1458
	add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1459
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1460
	foreach ( $args->taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1461
		register_taxonomy_for_object_type( $taxonomy, $post_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1462
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1463
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1464
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1465
	 * Fires after a post type is registered.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1466
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1467
	 * @since 3.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1468
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1469
	 * @param string $post_type Post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1470
	 * @param object $args      Arguments used to register the post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1471
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1472
	do_action( 'registered_post_type', $post_type, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1473
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1474
	return $args;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1475
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1476
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1477
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1478
 * Build an object with all post type capabilities out of a post type object
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1479
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1480
 * Post type capabilities use the 'capability_type' argument as a base, if the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1481
 * capability is not set in the 'capabilities' argument array or if the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1482
 * 'capabilities' argument is not supplied.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1483
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1484
 * The capability_type argument can optionally be registered as an array, with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1485
 * the first value being singular and the second plural, e.g. array('story, 'stories')
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1486
 * Otherwise, an 's' will be added to the value for the plural form. After
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1487
 * registration, capability_type will always be a string of the singular value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1488
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1489
 * By default, seven keys are accepted as part of the capabilities array:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1490
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1491
 * - edit_post, read_post, and delete_post are meta capabilities, which are then
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1492
 *   generally mapped to corresponding primitive capabilities depending on the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1493
 *   context, which would be the post being edited/read/deleted and the user or
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1494
 *   role being checked. Thus these capabilities would generally not be granted
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1495
 *   directly to users or roles.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1496
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1497
 * - edit_posts - Controls whether objects of this post type can be edited.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1498
 * - edit_others_posts - Controls whether objects of this type owned by other users
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1499
 *   can be edited. If the post type does not support an author, then this will
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1500
 *   behave like edit_posts.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1501
 * - publish_posts - Controls publishing objects of this post type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1502
 * - read_private_posts - Controls whether private objects can be read.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1503
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1504
 * These four primitive capabilities are checked in core in various locations.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1505
 * There are also seven other primitive capabilities which are not referenced
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1506
 * directly in core, except in map_meta_cap(), which takes the three aforementioned
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1507
 * meta capabilities and translates them into one or more primitive capabilities
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1508
 * that must then be checked against the user or role, depending on the context.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1509
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1510
 * - read - Controls whether objects of this post type can be read.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1511
 * - delete_posts - Controls whether objects of this post type can be deleted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1512
 * - delete_private_posts - Controls whether private objects can be deleted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1513
 * - delete_published_posts - Controls whether published objects can be deleted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1514
 * - delete_others_posts - Controls whether objects owned by other users can be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1515
 *   can be deleted. If the post type does not support an author, then this will
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1516
 *   behave like delete_posts.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1517
 * - edit_private_posts - Controls whether private objects can be edited.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1518
 * - edit_published_posts - Controls whether published objects can be edited.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1519
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1520
 * These additional capabilities are only used in map_meta_cap(). Thus, they are
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1521
 * only assigned by default if the post type is registered with the 'map_meta_cap'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1522
 * argument set to true (default is false).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1523
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1524
 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1525
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1526
 * @see register_post_type()
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1527
 * @see map_meta_cap()
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1528
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1529
 * @param object $args Post type registration arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1530
 * @return object object with all the capabilities as member variables.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1531
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1532
function get_post_type_capabilities( $args ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1533
	if ( ! is_array( $args->capability_type ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1534
		$args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1535
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1536
	// Singular base for meta capabilities, plural base for primitive capabilities.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1537
	list( $singular_base, $plural_base ) = $args->capability_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1538
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1539
	$default_capabilities = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1540
		// Meta capabilities
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1541
		'edit_post'          => 'edit_'         . $singular_base,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1542
		'read_post'          => 'read_'         . $singular_base,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1543
		'delete_post'        => 'delete_'       . $singular_base,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1544
		// Primitive capabilities used outside of map_meta_cap():
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1545
		'edit_posts'         => 'edit_'         . $plural_base,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1546
		'edit_others_posts'  => 'edit_others_'  . $plural_base,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1547
		'publish_posts'      => 'publish_'      . $plural_base,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1548
		'read_private_posts' => 'read_private_' . $plural_base,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1549
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1550
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1551
	// Primitive capabilities used within map_meta_cap():
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1552
	if ( $args->map_meta_cap ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1553
		$default_capabilities_for_mapping = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1554
			'read'                   => 'read',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1555
			'delete_posts'           => 'delete_'           . $plural_base,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1556
			'delete_private_posts'   => 'delete_private_'   . $plural_base,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1557
			'delete_published_posts' => 'delete_published_' . $plural_base,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1558
			'delete_others_posts'    => 'delete_others_'    . $plural_base,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1559
			'edit_private_posts'     => 'edit_private_'     . $plural_base,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1560
			'edit_published_posts'   => 'edit_published_'   . $plural_base,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1561
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1562
		$default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1563
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1564
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1565
	$capabilities = array_merge( $default_capabilities, $args->capabilities );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1566
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1567
	// Post creation capability simply maps to edit_posts by default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1568
	if ( ! isset( $capabilities['create_posts'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1569
		$capabilities['create_posts'] = $capabilities['edit_posts'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1570
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1571
	// Remember meta capabilities for future reference.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1572
	if ( $args->map_meta_cap )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1573
		_post_type_meta_capabilities( $capabilities );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1574
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1575
	return (object) $capabilities;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1576
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1577
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1578
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1579
 * Store or return a list of post type meta caps for map_meta_cap().
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1580
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1581
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1582
 * @access private
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1583
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1584
 * @param null|array $capabilities Post type meta capabilities.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1585
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1586
function _post_type_meta_capabilities( $capabilities = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1587
	static $meta_caps = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1588
	if ( null === $capabilities )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1589
		return $meta_caps;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1590
	foreach ( $capabilities as $core => $custom ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1591
		if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1592
			$meta_caps[ $custom ] = $core;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1593
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1594
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1595
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1596
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1597
 * Build an object with all post type labels out of a post type object
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1598
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1599
 * Accepted keys of the label array in the post type object:
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1600
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1601
 * - name - general name for the post type, usually plural. The same and overridden
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1602
 *          by $post_type_object->label. Default is Posts/Pages
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1603
 * - singular_name - name for one object of this post type. Default is Post/Page
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1604
 * - add_new - Default is Add New for both hierarchical and non-hierarchical types.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1605
 *             When internationalizing this string, please use a gettext context
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1606
 *             {@link https://codex.wordpress.org/I18n_for_WordPress_Developers#Disambiguation_by_context}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1607
 *             matching your post type. Example: `_x( 'Add New', 'product' );`.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1608
 * - add_new_item - Default is Add New Post/Add New Page.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1609
 * - edit_item - Default is Edit Post/Edit Page.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1610
 * - new_item - Default is New Post/New Page.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1611
 * - view_item - Default is View Post/View Page.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1612
 * - search_items - Default is Search Posts/Search Pages.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1613
 * - not_found - Default is No posts found/No pages found.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1614
 * - not_found_in_trash - Default is No posts found in Trash/No pages found in Trash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1615
 * - parent_item_colon - This string isn't used on non-hierarchical types. In hierarchical
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1616
 *                       ones the default is 'Parent Page:'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1617
 * - all_items - String for the submenu. Default is All Posts/All Pages.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1618
 * - menu_name - Default is the same as `name`.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1619
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1620
 * Above, the first default value is for non-hierarchical post types (like posts)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1621
 * and the second one is for hierarchical post types (like pages).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1622
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1623
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1624
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1625
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1626
 * @param object $post_type_object Post type object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1627
 * @return object object with all the labels as member variables.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1628
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1629
function get_post_type_labels( $post_type_object ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1630
	$nohier_vs_hier_defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1631
		'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name') ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1632
		'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name') ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1633
		'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1634
		'add_new_item' => array( __('Add New Post'), __('Add New Page') ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1635
		'edit_item' => array( __('Edit Post'), __('Edit Page') ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1636
		'new_item' => array( __('New Post'), __('New Page') ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1637
		'view_item' => array( __('View Post'), __('View Page') ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1638
		'search_items' => array( __('Search Posts'), __('Search Pages') ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1639
		'not_found' => array( __('No posts found.'), __('No pages found.') ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1640
		'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1641
		'parent_item_colon' => array( null, __('Parent Page:') ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1642
		'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1643
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1644
	$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1645
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1646
	$labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1647
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1648
	$post_type = $post_type_object->name;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1649
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1650
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1651
	 * Filter the labels of a specific post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1652
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1653
	 * The dynamic portion of the hook name, `$post_type`, refers to
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1654
	 * the post type slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1655
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1656
	 * @since 3.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1657
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1658
	 * @see get_post_type_labels() for the full list of labels.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1659
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1660
	 * @param array $labels Array of labels for the given post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1661
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1662
	return apply_filters( "post_type_labels_{$post_type}", $labels );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1663
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1664
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1665
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1666
 * Build an object with custom-something object (post type, taxonomy) labels
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1667
 * out of a custom-something object
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1668
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1669
 * @since 3.0.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1670
 * @access private
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1671
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1672
 * @param object $object                  A custom-something object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1673
 * @param array  $nohier_vs_hier_defaults Hierarchical vs non-hierarchical default labels.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1674
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1675
function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1676
	$object->labels = (array) $object->labels;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1677
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1678
	if ( isset( $object->label ) && empty( $object->labels['name'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1679
		$object->labels['name'] = $object->label;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1680
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1681
	if ( !isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1682
		$object->labels['singular_name'] = $object->labels['name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1683
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1684
	if ( ! isset( $object->labels['name_admin_bar'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1685
		$object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1686
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1687
	if ( !isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1688
		$object->labels['menu_name'] = $object->labels['name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1689
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1690
	if ( !isset( $object->labels['all_items'] ) && isset( $object->labels['menu_name'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1691
		$object->labels['all_items'] = $object->labels['menu_name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1692
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1693
	$defaults = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1694
	foreach ( $nohier_vs_hier_defaults as $key => $value ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1695
		$defaults[$key] = $object->hierarchical ? $value[1] : $value[0];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1696
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1697
	$labels = array_merge( $defaults, $object->labels );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1698
	return (object)$labels;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1699
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1700
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1701
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1702
 * Add submenus for post types.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1703
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1704
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1705
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1706
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1707
function _add_post_type_submenus() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1708
	foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1709
		$ptype_obj = get_post_type_object( $ptype );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1710
		// Sub-menus only.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1711
		if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1712
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1713
		add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1714
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1715
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1716
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1717
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1718
 * Register support of certain features for a post type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1719
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1720
 * All core features are directly associated with a functional area of the edit
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1721
 * screen, such as the editor or a meta box. Features include: 'title', 'editor',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1722
 * 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1723
 * 'thumbnail', 'custom-fields', and 'post-formats'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1724
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1725
 * Additionally, the 'revisions' feature dictates whether the post type will
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1726
 * store revisions, and the 'comments' feature dictates whether the comments
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1727
 * count will show on the edit screen.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1728
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1729
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1730
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1731
 * @param string       $post_type The post type for which to add the feature.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1732
 * @param string|array $feature   The feature being added, accepts an array of
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1733
 *                                feature strings or a single string.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1734
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1735
function add_post_type_support( $post_type, $feature ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1736
	global $_wp_post_type_features;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1737
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1738
	$features = (array) $feature;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1739
	foreach ($features as $feature) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1740
		if ( func_num_args() == 2 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1741
			$_wp_post_type_features[$post_type][$feature] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1742
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1743
			$_wp_post_type_features[$post_type][$feature] = array_slice( func_get_args(), 2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1744
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1745
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1746
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1747
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1748
 * Remove support for a feature from a post type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1749
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1750
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1751
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1752
 * @param string $post_type The post type for which to remove the feature.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1753
 * @param string $feature   The feature being removed.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1754
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1755
function remove_post_type_support( $post_type, $feature ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1756
	global $_wp_post_type_features;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1757
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1758
	if ( isset( $_wp_post_type_features[$post_type][$feature] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1759
		unset( $_wp_post_type_features[$post_type][$feature] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1760
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1761
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1762
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1763
 * Get all the post type features
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1764
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1765
 * @since 3.4.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1766
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1767
 * @param string $post_type The post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1768
 * @return array Post type supports list.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1769
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1770
function get_all_post_type_supports( $post_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1771
	global $_wp_post_type_features;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1772
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1773
	if ( isset( $_wp_post_type_features[$post_type] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1774
		return $_wp_post_type_features[$post_type];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1775
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1776
	return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1777
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1778
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1779
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1780
 * Check a post type's support for a given feature.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1781
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1782
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1783
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1784
 * @param string $post_type The post type being checked.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1785
 * @param string $feature the feature being checked.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1786
 * @return bool Whether the post type supports the given feature.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1787
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1788
function post_type_supports( $post_type, $feature ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1789
	global $_wp_post_type_features;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1790
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1791
	return ( isset( $_wp_post_type_features[$post_type][$feature] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1792
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1793
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1794
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1795
 * Update the post type for the post ID.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1796
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1797
 * The page or post cache will be cleaned for the post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1798
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1799
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1800
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1801
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1802
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1803
 * @param int    $post_id   Optional. Post ID to change post type. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1804
 * @param string $post_type Optional. Post type. Accepts 'post' or 'page' to
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1805
 *                          name a few. Default 'post'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1806
 * @return int Amount of rows changed. Should be 1 for success and 0 for failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1807
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1808
function set_post_type( $post_id = 0, $post_type = 'post' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1809
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1810
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1811
	$post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1812
	$return = $wpdb->update( $wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1813
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1814
	clean_post_cache( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1815
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1816
	return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1817
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1818
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1819
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1820
 * Retrieve list of latest posts or posts matching criteria.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1821
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1822
 * The defaults are as follows:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1823
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1824
 * @since 1.2.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1825
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1826
 * @see WP_Query::parse_query()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1827
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1828
 * @param array $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1829
 *     Optional. Arguments to retrieve posts. {@see WP_Query::parse_query()} for more
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1830
 *     available arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1831
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1832
 *     @type int        $numberposts      Total number of posts to retrieve. Is an alias of $posts_per_page
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1833
 *                                        in WP_Query. Accepts 1+ and -1 for all. Default 5.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1834
 *     @type int        $offset           The number of posts to offset before retrieval. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1835
 *     @type int|string $category         Category ID or comma-separated list of IDs (this or any children).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1836
 *                                        Is an alias of $cat in WP_Query. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1837
 *     @type string     $orderby          Which field to order posts by. Accepts post fields. Default 'date'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1838
 *     @type array      $include          An array of post IDs to retrieve, sticky posts will be included.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1839
 *                                        Is an alias of $post__in in WP_Query. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1840
 *     @type array      $exclude          An array of post IDs not to retrieve. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1841
 *     @type string     $meta_key         Custom field key. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1842
 *     @type mixed      $meta_value       Custom field value. Default empty string.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1843
 *     @type string     $post_type        Post type. Default 'post'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1844
 *     @type bool       $suppress_filters Whether to suppress filters. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1845
 * }
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1846
 * @return array List of posts.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1847
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1848
function get_posts( $args = null ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1849
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1850
		'numberposts' => 5, 'offset' => 0,
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1851
		'category' => 0, 'orderby' => 'date',
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1852
		'order' => 'DESC', 'include' => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1853
		'exclude' => array(), 'meta_key' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1854
		'meta_value' =>'', 'post_type' => 'post',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1855
		'suppress_filters' => true
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1856
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1857
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1858
	$r = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1859
	if ( empty( $r['post_status'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1860
		$r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1861
	if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1862
		$r['posts_per_page'] = $r['numberposts'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1863
	if ( ! empty($r['category']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1864
		$r['cat'] = $r['category'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1865
	if ( ! empty($r['include']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1866
		$incposts = wp_parse_id_list( $r['include'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1867
		$r['posts_per_page'] = count($incposts);  // only the number of posts included
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1868
		$r['post__in'] = $incposts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1869
	} elseif ( ! empty($r['exclude']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1870
		$r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1871
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1872
	$r['ignore_sticky_posts'] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1873
	$r['no_found_rows'] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1874
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1875
	$get_posts = new WP_Query;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1876
	return $get_posts->query($r);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1877
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1878
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1879
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1880
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1881
// Post meta functions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1882
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1883
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1884
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1885
 * Add meta data field to a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1886
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1887
 * Post meta data is called "Custom Fields" on the Administration Screen.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1888
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1889
 * @since 1.5.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1890
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1891
 * @param int    $post_id    Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1892
 * @param string $meta_key   Metadata name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1893
 * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1894
 * @param bool   $unique     Optional. Whether the same key should not be added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1895
 *                           Default false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1896
 * @return int|bool Meta ID on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1897
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1898
function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1899
	// Make sure meta is added to the post, not a revision.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1900
	if ( $the_post = wp_is_post_revision($post_id) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1901
		$post_id = $the_post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1902
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1903
	return add_metadata('post', $post_id, $meta_key, $meta_value, $unique);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1904
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1905
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1906
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1907
 * Remove metadata matching criteria from a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1908
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1909
 * You can match based on the key, or key and value. Removing based on key and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1910
 * value, will keep from removing duplicate metadata with the same key. It also
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1911
 * allows removing all metadata matching key, if needed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1912
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1913
 * @since 1.5.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1914
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1915
 * @param int    $post_id    Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1916
 * @param string $meta_key   Metadata name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1917
 * @param mixed  $meta_value Optional. Metadata value. Must be serializable if
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1918
 *                           non-scalar. Default empty.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1919
 * @return bool True on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1920
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1921
function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1922
	// Make sure meta is added to the post, not a revision.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1923
	if ( $the_post = wp_is_post_revision($post_id) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1924
		$post_id = $the_post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1925
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1926
	return delete_metadata('post', $post_id, $meta_key, $meta_value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1927
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1928
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1929
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1930
 * Retrieve post meta field for a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1931
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1932
 * @since 1.5.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1933
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1934
 * @param int    $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1935
 * @param string $key     Optional. The meta key to retrieve. By default, returns
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1936
 *                        data for all keys. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1937
 * @param bool   $single  Optional. Whether to return a single value. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1938
 * @return mixed Will be an array if $single is false. Will be value of meta data
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1939
 *               field if $single is true.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1940
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1941
function get_post_meta( $post_id, $key = '', $single = false ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1942
	return get_metadata('post', $post_id, $key, $single);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1943
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1944
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1945
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1946
 * Update post meta field based on post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1947
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1948
 * Use the $prev_value parameter to differentiate between meta fields with the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1949
 * same key and post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1950
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1951
 * If the meta field for the post does not exist, it will be added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1952
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1953
 * @since 1.5.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1954
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1955
 * @param int    $post_id    Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1956
 * @param string $meta_key   Metadata key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1957
 * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1958
 * @param mixed  $prev_value Optional. Previous value to check before removing.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1959
 *                           Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1960
 * @return int|bool Meta ID if the key didn't exist, true on successful update,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1961
 *                  false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1962
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1963
function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1964
	// Make sure meta is added to the post, not a revision.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1965
	if ( $the_post = wp_is_post_revision($post_id) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1966
		$post_id = $the_post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1967
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1968
	return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1969
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1970
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1971
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1972
 * Delete everything from post meta matching meta key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1973
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1974
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1975
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1976
 * @param string $post_meta_key Key to search for when deleting.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1977
 * @return bool Whether the post meta key was deleted from the database.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1978
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1979
function delete_post_meta_by_key( $post_meta_key ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1980
	return delete_metadata( 'post', null, $post_meta_key, '', true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1981
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1982
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1983
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1984
 * Retrieve post meta fields, based on post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1985
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1986
 * The post meta fields are retrieved from the cache where possible,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1987
 * so the function is optimized to be called more than once.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1988
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1989
 * @since 1.2.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1990
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1991
 * @param int $post_id Optional. Post ID. Default is ID of the global $post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1992
 * @return array Post meta for the given post.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1993
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1994
function get_post_custom( $post_id = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1995
	$post_id = absint( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1996
	if ( ! $post_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1997
		$post_id = get_the_ID();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1998
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1999
	return get_post_meta( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2000
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2001
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2002
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2003
 * Retrieve meta field names for a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2004
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2005
 * If there are no meta fields, then nothing (null) will be returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2006
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2007
 * @since 1.2.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2008
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2009
 * @param int $post_id Optional. Post ID. Default is ID of the global $post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2010
 * @return array|null Either array of the keys, or null if keys could not be
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2011
 *                    retrieved.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2012
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2013
function get_post_custom_keys( $post_id = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2014
	$custom = get_post_custom( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2015
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2016
	if ( !is_array($custom) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2017
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2018
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2019
	if ( $keys = array_keys($custom) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2020
		return $keys;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2021
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2022
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2023
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2024
 * Retrieve values for a custom post field.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2025
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2026
 * The parameters must not be considered optional. All of the post meta fields
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2027
 * will be retrieved and only the meta field key values returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2028
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2029
 * @since 1.2.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2030
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2031
 * @param string $key     Optional. Meta field key. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2032
 * @param int    $post_id Optional. Post ID. Default is ID of the global $post.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2033
 * @return array Meta field values.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2034
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2035
function get_post_custom_values( $key = '', $post_id = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2036
	if ( !$key )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2037
		return null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2038
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2039
	$custom = get_post_custom($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2040
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2041
	return isset($custom[$key]) ? $custom[$key] : null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2042
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2043
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2044
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2045
 * Check if post is sticky.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2046
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2047
 * Sticky posts should remain at the top of The Loop. If the post ID is not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2048
 * given, then The Loop ID for the current post will be used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2049
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2050
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2051
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2052
 * @param int $post_id Optional. Post ID. Default is ID of the global $post.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2053
 * @return bool Whether post is sticky.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2054
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2055
function is_sticky( $post_id = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2056
	$post_id = absint( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2057
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2058
	if ( ! $post_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2059
		$post_id = get_the_ID();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2060
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2061
	$stickies = get_option( 'sticky_posts' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2062
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2063
	if ( ! is_array( $stickies ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2064
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2065
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2066
	if ( in_array( $post_id, $stickies ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2067
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2068
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2069
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2070
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2071
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2072
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2073
 * Sanitize every post field.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2074
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2075
 * If the context is 'raw', then the post object or array will get minimal
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2076
 * sanitization of the integer fields.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2077
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2078
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2079
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2080
 * @see sanitize_post_field()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2081
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2082
 * @param object|WP_Post|array $post    The Post Object or Array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2083
 * @param string               $context Optional. How to sanitize post fields.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2084
 *                                      Accepts 'raw', 'edit', 'db', or 'display'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2085
 *                                      Default 'display'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2086
 * @return object|WP_Post|array The now sanitized Post Object or Array (will be the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2087
 *                              same type as $post).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2088
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2089
function sanitize_post( $post, $context = 'display' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2090
	if ( is_object($post) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2091
		// Check if post already filtered for this context.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2092
		if ( isset($post->filter) && $context == $post->filter )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2093
			return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2094
		if ( !isset($post->ID) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2095
			$post->ID = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2096
		foreach ( array_keys(get_object_vars($post)) as $field )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2097
			$post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2098
		$post->filter = $context;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2099
	} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2100
		// Check if post already filtered for this context.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2101
		if ( isset($post['filter']) && $context == $post['filter'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2102
			return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2103
		if ( !isset($post['ID']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2104
			$post['ID'] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2105
		foreach ( array_keys($post) as $field )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2106
			$post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2107
		$post['filter'] = $context;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2108
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2109
	return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2110
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2111
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2112
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2113
 * Sanitize post field based on context.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2114
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2115
 * Possible context values are:  'raw', 'edit', 'db', 'display', 'attribute' and
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2116
 * 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2117
 * are treated like 'display' when calling filters.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2118
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2119
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2120
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2121
 * @param string $field   The Post Object field name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2122
 * @param mixed  $value   The Post Object value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2123
 * @param int    $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2124
 * @param string $context How to sanitize post fields. Looks for 'raw', 'edit',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2125
 *                        'db', 'display', 'attribute' and 'js'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2126
 * @return mixed Sanitized value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2127
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2128
function sanitize_post_field($field, $value, $post_id, $context) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2129
	$int_fields = array('ID', 'post_parent', 'menu_order');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2130
	if ( in_array($field, $int_fields) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2131
		$value = (int) $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2132
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2133
	// Fields which contain arrays of integers.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2134
	$array_int_fields = array( 'ancestors' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2135
	if ( in_array($field, $array_int_fields) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2136
		$value = array_map( 'absint', $value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2137
		return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2138
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2139
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2140
	if ( 'raw' == $context )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2141
		return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2143
	$prefixed = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2144
	if ( false !== strpos($field, 'post_') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2145
		$prefixed = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2146
		$field_no_prefix = str_replace('post_', '', $field);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2147
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2148
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2149
	if ( 'edit' == $context ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2150
		$format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2151
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2152
		if ( $prefixed ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2153
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2154
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2155
			 * Filter the value of a specific post field to edit.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2156
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2157
			 * The dynamic portion of the hook name, `$field`, refers to the post
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2158
			 * field name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2159
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2160
			 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2161
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2162
			 * @param mixed $value   Value of the post field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2163
			 * @param int   $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2164
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2165
			$value = apply_filters( "edit_{$field}", $value, $post_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2166
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2167
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2168
			 * Filter the value of a specific post field to edit.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2169
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2170
			 * The dynamic portion of the hook name, `$field_no_prefix`, refers to
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2171
			 * the post field name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2172
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2173
			 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2174
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2175
			 * @param mixed $value   Value of the post field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2176
			 * @param int   $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2177
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2178
			$value = apply_filters( "{$field_no_prefix}_edit_pre", $value, $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2179
		} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2180
			$value = apply_filters( "edit_post_{$field}", $value, $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2181
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2182
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2183
		if ( in_array($field, $format_to_edit) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2184
			if ( 'post_content' == $field )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2185
				$value = format_to_edit($value, user_can_richedit());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2186
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2187
				$value = format_to_edit($value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2188
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2189
			$value = esc_attr($value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2190
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2191
	} elseif ( 'db' == $context ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2192
		if ( $prefixed ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2193
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2194
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2195
			 * Filter the value of a specific post field before saving.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2196
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2197
			 * The dynamic portion of the hook name, `$field`, refers to the post
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2198
			 * field name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2199
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2200
			 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2201
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2202
			 * @param mixed $value Value of the post field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2203
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2204
			$value = apply_filters( "pre_{$field}", $value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2205
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2206
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2207
			 * Filter the value of a specific field before saving.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2208
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2209
			 * The dynamic portion of the hook name, `$field_no_prefix`, refers
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2210
			 * to the post field name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2211
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2212
			 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2213
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2214
			 * @param mixed $value Value of the post field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2215
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2216
			$value = apply_filters( "{$field_no_prefix}_save_pre", $value );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2217
		} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2218
			$value = apply_filters( "pre_post_{$field}", $value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2219
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2220
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2221
			 * Filter the value of a specific post field before saving.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2222
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2223
			 * The dynamic portion of the hook name, `$field`, refers to the post
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2224
			 * field name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2225
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2226
			 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2227
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2228
			 * @param mixed $value Value of the post field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2229
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2230
			$value = apply_filters( "{$field}_pre", $value );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2231
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2232
	} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2233
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2234
		// Use display filters by default.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2235
		if ( $prefixed ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2236
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2237
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2238
			 * Filter the value of a specific post field for display.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2239
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2240
			 * The dynamic portion of the hook name, `$field`, refers to the post
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2241
			 * field name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2242
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2243
			 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2244
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2245
			 * @param mixed  $value   Value of the prefixed post field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2246
			 * @param int    $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2247
			 * @param string $context Context for how to sanitize the field. Possible
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2248
			 *                        values include 'raw', 'edit', 'db', 'display',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2249
			 *                        'attribute' and 'js'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2250
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2251
			$value = apply_filters( $field, $value, $post_id, $context );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2252
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2253
			$value = apply_filters( "post_{$field}", $value, $post_id, $context );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2254
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2255
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2256
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2257
	if ( 'attribute' == $context )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2258
		$value = esc_attr($value);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2259
	elseif ( 'js' == $context )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2260
		$value = esc_js($value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2261
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2262
	return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2263
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2264
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2265
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2266
 * Make a post sticky.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2267
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2268
 * Sticky posts should be displayed at the top of the front page.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2269
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2270
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2271
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2272
 * @param int $post_id Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2273
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2274
function stick_post( $post_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2275
	$stickies = get_option('sticky_posts');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2276
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2277
	if ( !is_array($stickies) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2278
		$stickies = array($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2279
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2280
	if ( ! in_array($post_id, $stickies) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2281
		$stickies[] = $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2282
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2283
	update_option('sticky_posts', $stickies);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2284
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2285
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2286
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2287
 * Un-stick a post.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2288
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2289
 * Sticky posts should be displayed at the top of the front page.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2290
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2291
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2292
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2293
 * @param int $post_id Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2294
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2295
function unstick_post( $post_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2296
	$stickies = get_option('sticky_posts');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2297
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2298
	if ( !is_array($stickies) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2299
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2300
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2301
	if ( ! in_array($post_id, $stickies) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2302
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2303
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2304
	$offset = array_search($post_id, $stickies);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2305
	if ( false === $offset )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2306
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2307
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2308
	array_splice($stickies, $offset, 1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2309
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2310
	update_option('sticky_posts', $stickies);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2311
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2312
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2313
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2314
 * Return the cache key for wp_count_posts() based on the passed arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2315
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2316
 * @since 3.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2317
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2318
 * @param string $type Optional. Post type to retrieve count Default 'post'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2319
 * @param string $perm Optional. 'readable' or empty. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2320
 * @return string The cache key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2321
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2322
function _count_posts_cache_key( $type = 'post', $perm = '' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2323
	$cache_key = 'posts-' . $type;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2324
	if ( 'readable' == $perm && is_user_logged_in() ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2325
		$post_type_object = get_post_type_object( $type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2326
		if ( $post_type_object && ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2327
			$cache_key .= '_' . $perm . '_' . get_current_user_id();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2328
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2329
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2330
	return $cache_key;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2331
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2332
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2333
/**
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2334
 * Count number of posts of a post type and if user has permissions to view.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2335
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2336
 * This function provides an efficient method of finding the amount of post's
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2337
 * type a blog has. Another method is to count the amount of items in
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2338
 * get_posts(), but that method has a lot of overhead with doing so. Therefore,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2339
 * when developing for 2.5+, use this function instead.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2340
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2341
 * The $perm parameter checks for 'readable' value and if the user can read
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2342
 * private posts, it will display that for the user that is signed in.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2343
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2344
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2345
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2346
 * @param string $type Optional. Post type to retrieve count. Default 'post'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2347
 * @param string $perm Optional. 'readable' or empty. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2348
 * @return object Number of posts for each status.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2349
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2350
function wp_count_posts( $type = 'post', $perm = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2351
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2352
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2353
	if ( ! post_type_exists( $type ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2354
		return new stdClass;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2355
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2356
	$cache_key = _count_posts_cache_key( $type, $perm );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2357
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2358
	$counts = wp_cache_get( $cache_key, 'counts' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2359
	if ( false !== $counts ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2360
		/** This filter is documented in wp-includes/post.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2361
		return apply_filters( 'wp_count_posts', $counts, $type, $perm );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2362
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2363
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2364
	$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2365
	if ( 'readable' == $perm && is_user_logged_in() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2366
		$post_type_object = get_post_type_object($type);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2367
		if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2368
			$query .= $wpdb->prepare( " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2369
				get_current_user_id()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2370
			);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2371
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2372
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2373
	$query .= ' GROUP BY post_status';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2374
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2375
	$results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2376
	$counts = array_fill_keys( get_post_stati(), 0 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2377
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2378
	foreach ( $results as $row ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2379
		$counts[ $row['post_status'] ] = $row['num_posts'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2380
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2381
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2382
	$counts = (object) $counts;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2383
	wp_cache_set( $cache_key, $counts, 'counts' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2384
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2385
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2386
	 * Modify returned post counts by status for the current post type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2387
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2388
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2389
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2390
	 * @param object $counts An object containing the current post_type's post
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2391
	 *                       counts by status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2392
	 * @param string $type   Post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2393
	 * @param string $perm   The permission to determine if the posts are 'readable'
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2394
	 *                       by the current user.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2395
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2396
	return apply_filters( 'wp_count_posts', $counts, $type, $perm );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2397
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2398
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2399
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2400
 * Count number of attachments for the mime type(s).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2401
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2402
 * If you set the optional mime_type parameter, then an array will still be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2403
 * returned, but will only have the item you are looking for. It does not give
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2404
 * you the number of attachments that are children of a post. You can get that
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2405
 * by counting the number of children that post has.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2406
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2407
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2408
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2409
 * @param string|array $mime_type Optional. Array or comma-separated list of
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2410
 *                                MIME patterns. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2411
 * @return object An object containing the attachment counts by mime type.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2412
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2413
function wp_count_attachments( $mime_type = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2414
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2415
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2416
	$and = wp_post_mime_type_where( $mime_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2417
	$count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2418
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2419
	$counts = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2420
	foreach( (array) $count as $row ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2421
		$counts[ $row['post_mime_type'] ] = $row['num_posts'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2422
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2423
	$counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2424
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2425
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2426
	 * Modify returned attachment counts by mime type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2427
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2428
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2429
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2430
	 * @param object $counts    An object containing the attachment counts by
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2431
	 *                          mime type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2432
	 * @param string $mime_type The mime type pattern used to filter the attachments
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2433
	 *                          counted.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2434
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2435
	return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2436
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2437
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2438
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2439
 * Get default post mime types.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2440
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2441
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2442
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2443
 * @return array List of post mime types.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2444
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2445
function get_post_mime_types() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2446
	$post_mime_types = array(	//	array( adj, noun )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2447
		'image' => array(__('Images'), __('Manage Images'), _n_noop('Image <span class="count">(%s)</span>', 'Images <span class="count">(%s)</span>')),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2448
		'audio' => array(__('Audio'), __('Manage Audio'), _n_noop('Audio <span class="count">(%s)</span>', 'Audio <span class="count">(%s)</span>')),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2449
		'video' => array(__('Video'), __('Manage Video'), _n_noop('Video <span class="count">(%s)</span>', 'Video <span class="count">(%s)</span>')),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2450
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2451
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2452
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2453
	 * Filter the default list of post mime types.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2454
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2455
	 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2456
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2457
	 * @param array $post_mime_types Default list of post mime types.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2458
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2459
	return apply_filters( 'post_mime_types', $post_mime_types );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2460
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2461
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2462
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2463
 * Check a MIME-Type against a list.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2464
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2465
 * If the wildcard_mime_types parameter is a string, it must be comma separated
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2466
 * list. If the real_mime_types is a string, it is also comma separated to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2467
 * create the list.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2468
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2469
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2470
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2471
 * @param string|array $wildcard_mime_types Mime types, e.g. audio/mpeg or image (same as image/*)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2472
 *                                          or flash (same as *flash*).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2473
 * @param string|array $real_mime_types     Real post mime type values.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2474
 * @return array array(wildcard=>array(real types)).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2475
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2476
function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2477
	$matches = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2478
	if ( is_string( $wildcard_mime_types ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2479
		$wildcard_mime_types = array_map( 'trim', explode( ',', $wildcard_mime_types ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2480
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2481
	if ( is_string( $real_mime_types ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2482
		$real_mime_types = array_map( 'trim', explode( ',', $real_mime_types ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2483
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2484
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2485
	$patternses = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2486
	$wild = '[-._a-z0-9]*';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2487
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2488
	foreach ( (array) $wildcard_mime_types as $type ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2489
		$mimes = array_map( 'trim', explode( ',', $type ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2490
		foreach ( $mimes as $mime ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2491
			$regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2492
			$patternses[][$type] = "^$regex$";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2493
			if ( false === strpos( $mime, '/' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2494
				$patternses[][$type] = "^$regex/";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2495
				$patternses[][$type] = $regex;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2496
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2497
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2498
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2499
	asort( $patternses );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2500
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2501
	foreach ( $patternses as $patterns ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2502
		foreach ( $patterns as $type => $pattern ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2503
			foreach ( (array) $real_mime_types as $real ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2504
				if ( preg_match( "#$pattern#", $real ) && ( empty( $matches[$type] ) || false === array_search( $real, $matches[$type] ) ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2505
					$matches[$type][] = $real;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2506
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2507
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2508
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2509
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2510
	return $matches;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2511
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2512
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2513
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2514
 * Convert MIME types into SQL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2515
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2516
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2517
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2518
 * @param string|array $post_mime_types List of mime types or comma separated string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2519
 *                                      of mime types.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2520
 * @param string       $table_alias     Optional. Specify a table alias, if needed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2521
 *                                      Default empty.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2522
 * @return string The SQL AND clause for mime searching.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2523
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2524
function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2525
	$where = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2526
	$wildcards = array('', '%', '%/%');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2527
	if ( is_string($post_mime_types) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2528
		$post_mime_types = array_map('trim', explode(',', $post_mime_types));
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2529
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2530
	$wheres = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2531
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2532
	foreach ( (array) $post_mime_types as $mime_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2533
		$mime_type = preg_replace('/\s/', '', $mime_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2534
		$slashpos = strpos($mime_type, '/');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2535
		if ( false !== $slashpos ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2536
			$mime_group = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, 0, $slashpos));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2537
			$mime_subgroup = preg_replace('/[^-*.+a-zA-Z0-9]/', '', substr($mime_type, $slashpos + 1));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2538
			if ( empty($mime_subgroup) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2539
				$mime_subgroup = '*';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2540
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2541
				$mime_subgroup = str_replace('/', '', $mime_subgroup);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2542
			$mime_pattern = "$mime_group/$mime_subgroup";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2543
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2544
			$mime_pattern = preg_replace('/[^-*.a-zA-Z0-9]/', '', $mime_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2545
			if ( false === strpos($mime_pattern, '*') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2546
				$mime_pattern .= '/*';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2547
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2548
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2549
		$mime_pattern = preg_replace('/\*+/', '%', $mime_pattern);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2550
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2551
		if ( in_array( $mime_type, $wildcards ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2552
			return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2553
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2554
		if ( false !== strpos($mime_pattern, '%') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2555
			$wheres[] = empty($table_alias) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2556
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2557
			$wheres[] = empty($table_alias) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2558
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2559
	if ( !empty($wheres) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2560
		$where = ' AND (' . join(' OR ', $wheres) . ') ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2561
	return $where;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2562
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2563
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2564
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2565
 * Trash or delete a post or page.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2566
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2567
 * When the post and page is permanently deleted, everything that is tied to
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2568
 * it is deleted also. This includes comments, post meta fields, and terms
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2569
 * associated with the post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2570
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2571
 * The post or page is moved to trash instead of permanently deleted unless
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2572
 * trash is disabled, item is already in the trash, or $force_delete is true.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2573
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2574
 * @since 1.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2575
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2576
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2577
 * @see wp_delete_attachment()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2578
 * @see wp_trash_post()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2579
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2580
 * @param int  $postid       Optional. Post ID. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2581
 * @param bool $force_delete Optional. Whether to bypass trash and force deletion.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2582
 *                           Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2583
 * @return array|bool|WP_Post False on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2584
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2585
function wp_delete_post( $postid = 0, $force_delete = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2586
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2587
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2588
	if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2589
		return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2590
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2591
	if ( !$force_delete && ( $post->post_type == 'post' || $post->post_type == 'page') && get_post_status( $postid ) != 'trash' && EMPTY_TRASH_DAYS )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2592
			return wp_trash_post($postid);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2593
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2594
	if ( $post->post_type == 'attachment' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2595
		return wp_delete_attachment( $postid, $force_delete );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2596
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2597
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2598
	 * Fires before a post is deleted, at the start of wp_delete_post().
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2599
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2600
	 * @since 3.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2601
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2602
	 * @see wp_delete_post()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2603
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2604
	 * @param int $postid Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2605
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2606
	do_action( 'before_delete_post', $postid );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2607
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2608
	delete_post_meta($postid,'_wp_trash_meta_status');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2609
	delete_post_meta($postid,'_wp_trash_meta_time');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2610
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2611
	wp_delete_object_term_relationships($postid, get_object_taxonomies($post->post_type));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2612
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2613
	$parent_data = array( 'post_parent' => $post->post_parent );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2614
	$parent_where = array( 'post_parent' => $postid );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2615
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2616
	if ( is_post_type_hierarchical( $post->post_type ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2617
		// Point children of this page to its parent, also clean the cache of affected children.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2618
		$children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2619
		$children = $wpdb->get_results( $children_query );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2620
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2621
		$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2622
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2623
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2624
	// Do raw query. wp_get_post_revisions() is filtered.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2625
	$revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2626
	// Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2627
	foreach ( $revision_ids as $revision_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2628
		wp_delete_post_revision( $revision_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2629
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2630
	// Point all attachments to this post up one level.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2631
	$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2632
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2633
	$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2634
	foreach ( $comment_ids as $comment_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2635
		wp_delete_comment( $comment_id, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2636
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2637
	$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2638
	foreach ( $post_meta_ids as $mid )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2639
		delete_metadata_by_mid( 'post', $mid );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2640
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2641
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2642
	 * Fires immediately before a post is deleted from the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2643
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2644
	 * @since 1.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2645
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2646
	 * @param int $postid Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2647
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2648
	do_action( 'delete_post', $postid );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2649
	$result = $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2650
	if ( ! $result ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2651
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2652
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2653
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2654
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2655
	 * Fires immediately after a post is deleted from the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2656
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2657
	 * @since 2.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2658
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2659
	 * @param int $postid Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2660
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2661
	do_action( 'deleted_post', $postid );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2662
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2663
	clean_post_cache( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2664
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2665
	if ( is_post_type_hierarchical( $post->post_type ) && $children ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2666
		foreach ( $children as $child )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2667
			clean_post_cache( $child );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2668
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2669
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2670
	wp_clear_scheduled_hook('publish_future_post', array( $postid ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2671
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2672
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2673
	 * Fires after a post is deleted, at the conclusion of wp_delete_post().
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2674
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2675
	 * @since 3.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2676
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2677
	 * @see wp_delete_post()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2678
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2679
	 * @param int $postid Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2680
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2681
	do_action( 'after_delete_post', $postid );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2682
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2683
	return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2684
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2685
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2686
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2687
 * Reset the page_on_front, show_on_front, and page_for_post settings when
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2688
 * a linked page is deleted or trashed.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2689
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2690
 * Also ensures the post is no longer sticky.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2691
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2692
 * @since 3.7.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2693
 * @access private
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2694
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2695
 * @param int $post_id Post ID.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2696
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2697
function _reset_front_page_settings_for_post( $post_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2698
	$post = get_post( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2699
	if ( 'page' == $post->post_type ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2700
	 	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2701
	 	 * If the page is defined in option page_on_front or post_for_posts,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2702
	 	 * adjust the corresponding options.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2703
	 	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2704
		if ( get_option( 'page_on_front' ) == $post->ID ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2705
			update_option( 'show_on_front', 'posts' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2706
			update_option( 'page_on_front', 0 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2707
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2708
		if ( get_option( 'page_for_posts' ) == $post->ID ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2709
			delete_option( 'page_for_posts', 0 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2710
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2711
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2712
	unstick_post( $post->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2713
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2714
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2715
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2716
 * Move a post or page to the Trash
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2717
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2718
 * If trash is disabled, the post or page is permanently deleted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2719
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2720
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2721
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2722
 * @see wp_delete_post()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2723
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2724
 * @param int $post_id Optional. Post ID. Default is ID of the global $post
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2725
 *                     if EMPTY_TRASH_DAYS equals true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2726
 * @return bool|array Post data array, otherwise false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2727
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2728
function wp_trash_post( $post_id = 0 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2729
	if ( !EMPTY_TRASH_DAYS )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2730
		return wp_delete_post($post_id, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2731
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2732
	if ( !$post = get_post($post_id, ARRAY_A) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2733
		return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2734
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2735
	if ( $post['post_status'] == 'trash' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2736
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2737
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2738
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2739
	 * Fires before a post is sent to the trash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2740
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2741
	 * @since 3.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2742
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2743
	 * @param int $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2744
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2745
	do_action( 'wp_trash_post', $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2746
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2747
	add_post_meta($post_id,'_wp_trash_meta_status', $post['post_status']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2748
	add_post_meta($post_id,'_wp_trash_meta_time', time());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2749
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2750
	$post['post_status'] = 'trash';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2751
	wp_insert_post($post);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2752
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2753
	wp_trash_post_comments($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2754
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2755
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2756
	 * Fires after a post is sent to the trash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2757
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2758
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2759
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2760
	 * @param int $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2761
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2762
	do_action( 'trashed_post', $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2763
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2764
	return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2765
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2766
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2767
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2768
 * Restore a post or page from the Trash.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2769
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2770
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2771
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2772
 * @param int $post_id Optional. Post ID. Default is ID of the global $post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2773
 * @return WP_Post|bool WP_Post object. False on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2774
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2775
function wp_untrash_post( $post_id = 0 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2776
	if ( !$post = get_post($post_id, ARRAY_A) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2777
		return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2778
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2779
	if ( $post['post_status'] != 'trash' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2780
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2781
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2782
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2783
	 * Fires before a post is restored from the trash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2784
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2785
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2786
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2787
	 * @param int $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2788
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2789
	do_action( 'untrash_post', $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2790
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2791
	$post_status = get_post_meta($post_id, '_wp_trash_meta_status', true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2792
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2793
	$post['post_status'] = $post_status;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2794
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2795
	delete_post_meta($post_id, '_wp_trash_meta_status');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2796
	delete_post_meta($post_id, '_wp_trash_meta_time');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2797
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2798
	wp_insert_post($post);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2799
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2800
	wp_untrash_post_comments($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2801
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2802
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2803
	 * Fires after a post is restored from the trash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2804
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2805
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2806
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2807
	 * @param int $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2808
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2809
	do_action( 'untrashed_post', $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2810
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2811
	return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2812
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2813
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2814
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2815
 * Moves comments for a post to the trash.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2816
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2817
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2818
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2819
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2820
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2821
 * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2822
 * @return mixed False on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2823
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2824
function wp_trash_post_comments( $post = null ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2825
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2826
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2827
	$post = get_post($post);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2828
	if ( empty($post) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2829
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2830
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2831
	$post_id = $post->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2832
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2833
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2834
	 * Fires before comments are sent to the trash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2835
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2836
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2837
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2838
	 * @param int $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2839
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2840
	do_action( 'trash_post_comments', $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2841
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2842
	$comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2843
	if ( empty($comments) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2844
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2845
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2846
	// Cache current status for each comment.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2847
	$statuses = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2848
	foreach ( $comments as $comment )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2849
		$statuses[$comment->comment_ID] = $comment->comment_approved;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2850
	add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2851
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2852
	// Set status for all comments to post-trashed.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2853
	$result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2854
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2855
	clean_comment_cache( array_keys($statuses) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2856
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2857
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2858
	 * Fires after comments are sent to the trash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2859
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2860
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2861
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2862
	 * @param int   $post_id  Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2863
	 * @param array $statuses Array of comment statuses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2864
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2865
	do_action( 'trashed_post_comments', $post_id, $statuses );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2866
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2867
	return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2868
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2869
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2870
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2871
 * Restore comments for a post from the trash.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2872
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2873
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2874
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2875
 * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2876
 * @return null|bool Null on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2877
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2878
function wp_untrash_post_comments( $post = null ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2879
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2880
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2881
	$post = get_post($post);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2882
	if ( empty($post) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2883
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2884
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2885
	$post_id = $post->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2886
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2887
	$statuses = get_post_meta($post_id, '_wp_trash_meta_comments_status', true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2888
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2889
	if ( empty($statuses) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2890
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2891
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2892
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2893
	 * Fires before comments are restored for a post from the trash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2894
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2895
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2896
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2897
	 * @param int $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2898
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2899
	do_action( 'untrash_post_comments', $post_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2900
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2901
	// Restore each comment to its original status.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2902
	$group_by_status = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2903
	foreach ( $statuses as $comment_id => $comment_status )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2904
		$group_by_status[$comment_status][] = $comment_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2905
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2906
	foreach ( $group_by_status as $status => $comments ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2907
		// Sanity check. This shouldn't happen.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2908
		if ( 'post-trashed' == $status )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2909
			$status = '0';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2910
		$comments_in = implode( "', '", $comments );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2911
		$wpdb->query( "UPDATE $wpdb->comments SET comment_approved = '$status' WHERE comment_ID IN ('" . $comments_in . "')" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2912
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2913
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2914
	clean_comment_cache( array_keys($statuses) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2915
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2916
	delete_post_meta($post_id, '_wp_trash_meta_comments_status');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2917
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2918
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2919
	 * Fires after comments are restored for a post from the trash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2920
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2921
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2922
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2923
	 * @param int $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2924
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2925
	do_action( 'untrashed_post_comments', $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2926
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2927
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2928
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2929
 * Retrieve the list of categories for a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2930
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2931
 * Compatibility layer for themes and plugins. Also an easy layer of abstraction
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2932
 * away from the complexity of the taxonomy layer.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2933
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2934
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2935
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2936
 * @see wp_get_object_terms()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2937
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2938
 * @param int   $post_id Optional. The Post ID. Does not default to the ID of the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2939
 *                       global $post. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2940
 * @param array $args    Optional. Category arguments. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2941
 * @return array List of categories.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2942
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2943
function wp_get_post_categories( $post_id = 0, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2944
	$post_id = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2945
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2946
	$defaults = array('fields' => 'ids');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2947
	$args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2948
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2949
	$cats = wp_get_object_terms($post_id, 'category', $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2950
	return $cats;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2951
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2952
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2953
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2954
 * Retrieve the tags for a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2955
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2956
 * There is only one default for this function, called 'fields' and by default
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2957
 * is set to 'all'. There are other defaults that can be overridden in
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2958
 * {@link wp_get_object_terms()}.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2959
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2960
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2961
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2962
 * @param int   $post_id Optional. The Post ID. Does not default to the ID of the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2963
 *                       global $post. Defualt 0.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2964
 * @param array $args Optional. Overwrite the defaults
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2965
 * @return array List of post tags.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2966
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2967
function wp_get_post_tags( $post_id = 0, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2968
	return wp_get_post_terms( $post_id, 'post_tag', $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2969
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2970
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2971
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2972
 * Retrieve the terms for a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2973
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2974
 * There is only one default for this function, called 'fields' and by default
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2975
 * is set to 'all'. There are other defaults that can be overridden in
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2976
 * {@link wp_get_object_terms()}.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2977
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2978
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2979
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2980
 * @param int    $post_id  Optional. The Post ID. Does not default to the ID of the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2981
 *                         global $post. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2982
 * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2983
 * @param array  $args     Optional. {@link wp_get_object_terms()} arguments. Default empty array.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2984
 * @return array List of post tags.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2985
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2986
function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2987
	$post_id = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2988
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2989
	$defaults = array('fields' => 'all');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2990
	$args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2991
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2992
	$tags = wp_get_object_terms($post_id, $taxonomy, $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2993
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2994
	return $tags;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2995
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2996
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2997
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2998
 * Retrieve a number of recent posts.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2999
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3000
 * @since 1.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3001
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3002
 * @see get_posts()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3003
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3004
 * @param array  $args       Optional. Arguments to retrieve posts. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3005
 * @param string $output     Optional. Type of output. Accepts ARRAY_A or ''. Default ARRAY_A.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3006
 * @return array|bool Associative array if $output equals ARRAY_A, array or false if no results.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3007
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3008
function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3009
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3010
	if ( is_numeric( $args ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3011
		_deprecated_argument( __FUNCTION__, '3.1', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3012
		$args = array( 'numberposts' => absint( $args ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3013
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3014
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3015
	// Set default arguments.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3016
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3017
		'numberposts' => 10, 'offset' => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3018
		'category' => 0, 'orderby' => 'post_date',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3019
		'order' => 'DESC', 'include' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3020
		'exclude' => '', 'meta_key' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3021
		'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3022
		'suppress_filters' => true
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3023
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3024
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3025
	$r = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3026
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3027
	$results = get_posts( $r );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3028
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3029
	// Backward compatibility. Prior to 3.1 expected posts to be returned in array.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3030
	if ( ARRAY_A == $output ){
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3031
		foreach( $results as $key => $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3032
			$results[$key] = get_object_vars( $result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3033
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3034
		return $results ? $results : array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3035
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3036
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3037
	return $results ? $results : false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3038
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3039
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3040
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3041
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3042
 * Insert or update a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3043
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3044
 * If the $postarr parameter has 'ID' set to a value, then post will be updated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3045
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3046
 * You can set the post date manually, by setting the values for 'post_date'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3047
 * and 'post_date_gmt' keys. You can close the comments or open the comments by
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3048
 * setting the value for 'comment_status' key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3049
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3050
 * @since 1.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3051
 * @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3052
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3053
 * @see sanitize_post()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3054
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3055
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3056
 * @param array $postarr {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3057
 *     An array of elements that make up a post to update or insert.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3058
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3059
 *     @type int    $ID                    The post ID. If equal to something other than 0,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3060
 *                                         the post with that ID will be updated. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3061
 *     @type int    $post_author           The ID of the user who added the post. Default is
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3062
 *                                         the current user ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3063
 *     @type string $post_date             The date of the post. Default is the current time.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3064
 *     @type string $post_date_gmt         The date of the post in the GMT timezone. Default is
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3065
 *                                         the value of `$post_date`.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3066
 *     @type mixed  $post_content          The post content. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3067
 *     @type string $post_content_filtered The filtered post content. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3068
 *     @type string $post_title            The post title. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3069
 *     @type string $post_excerpt          The post excerpt. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3070
 *     @type string $post_status           The post status. Default 'draft'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3071
 *     @type string $post_type             The post type. Default 'post'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3072
 *     @type string $comment_status        Whether the post can accept comments. Accepts 'open' or 'closed'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3073
 *                                         Default is the value of 'default_comment_status' option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3074
 *     @type string $ping_status           Whether the post can accept pings. Accepts 'open' or 'closed'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3075
 *                                         Default is the value of 'default_ping_status' option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3076
 *     @type string $post_password         The password to access the post. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3077
 *     @type string $post_name             The post name. Default is the sanitized post title.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3078
 *     @type string $to_ping               Space or carriage return-separated list of URLs to ping.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3079
 *                                         Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3080
 *     @type string $pinged                Space or carriage return-separated list of URLs that have
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3081
 *                                         been pinged. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3082
 *     @type string $post_modified         The date when the post was last modified. Default is
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3083
 *                                         the current time.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3084
 *     @type string $post_modified_gmt     The date when the post was last modified in the GMT
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3085
 *                                         timezone. Default is the current time.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3086
 *     @type int    $post_parent           Set this for the post it belongs to, if any. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3087
 *     @type int    $menu_order            The order the post should be displayed in. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3088
 *     @type string $post_mime_type        The mime type of the post. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3089
 *     @type string $guid                  Global Unique ID for referencing the post. Default empty.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3090
 * }
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3091
 * @param bool  $wp_error Optional. Whether to allow return of WP_Error on failure. Default false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3092
 * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3093
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3094
function wp_insert_post( $postarr, $wp_error = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3095
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3096
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3097
	$user_id = get_current_user_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3098
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3099
	$defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_id,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3100
		'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3101
		'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3102
		'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3103
		'post_content' => '', 'post_title' => '', 'context' => '');
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3104
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3105
	$postarr = wp_parse_args($postarr, $defaults);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3106
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3107
	unset( $postarr[ 'filter' ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3108
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3109
	$postarr = sanitize_post($postarr, 'db');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3110
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3111
	// Are we updating or creating?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3112
	$post_ID = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3113
	$update = false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3114
	$guid = $postarr['guid'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3115
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3116
	if ( ! empty( $postarr['ID'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3117
		$update = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3118
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3119
		// Get the post ID and GUID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3120
		$post_ID = $postarr['ID'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3121
		$post_before = get_post( $post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3122
		if ( is_null( $post_before ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3123
			if ( $wp_error ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3124
				return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3125
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3126
			return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3127
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3128
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3129
		$guid = get_post_field( 'guid', $post_ID );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3130
		$previous_status = get_post_field('post_status', $post_ID );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3131
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3132
		$previous_status = 'new';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3133
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3134
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3135
	$post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3136
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3137
	$post_title = $postarr['post_title'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3138
	$post_content = $postarr['post_content'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3139
	$post_excerpt = $postarr['post_excerpt'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3140
	if ( isset( $postarr['post_name'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3141
		$post_name = $postarr['post_name'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3142
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3143
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3144
	$maybe_empty = 'attachment' !== $post_type
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3145
		&& ! $post_content && ! $post_title && ! $post_excerpt
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3146
		&& post_type_supports( $post_type, 'editor' )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3147
		&& post_type_supports( $post_type, 'title' )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3148
		&& post_type_supports( $post_type, 'excerpt' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3149
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3150
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3151
	 * Filter whether the post should be considered "empty".
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3152
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3153
	 * The post is considered "empty" if both:
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3154
	 * 1. The post type supports the title, editor, and excerpt fields
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3155
	 * 2. The title, editor, and excerpt fields are all empty
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3156
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3157
	 * Returning a truthy value to the filter will effectively short-circuit
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3158
	 * the new post being inserted, returning 0. If $wp_error is true, a WP_Error
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3159
	 * will be returned instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3160
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3161
	 * @since 3.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3162
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3163
	 * @param bool  $maybe_empty Whether the post should be considered "empty".
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3164
	 * @param array $postarr     Array of post data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3165
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3166
	if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3167
		if ( $wp_error ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3168
			return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3169
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3170
			return 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3171
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3172
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3173
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3174
	$post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3175
	if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash' ) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3176
		$post_status = 'inherit';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3177
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3178
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3179
	if ( ! empty( $postarr['post_category'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3180
		// Filter out empty terms.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3181
		$post_category = array_filter( $postarr['post_category'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3182
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3183
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3184
	// Make sure we set a valid category.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3185
	if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3186
		// 'post' requires at least one category.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3187
		if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3188
			$post_category = array( get_option('default_category') );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3189
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3190
			$post_category = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3191
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3192
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3193
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3194
	// Don't allow contributors to set the post slug for pending review posts.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3195
	if ( 'pending' == $post_status && !current_user_can( 'publish_posts' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3196
		$post_name = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3197
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3198
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3199
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3200
	 * Create a valid post name. Drafts and pending posts are allowed to have
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3201
	 * an empty post name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3202
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3203
	if ( empty($post_name) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3204
		if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3205
			$post_name = sanitize_title($post_title);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3206
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3207
			$post_name = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3208
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3209
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3210
		// On updates, we need to check to see if it's using the old, fixed sanitization context.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3211
		$check_name = sanitize_title( $post_name, '', 'old-save' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3212
		if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3213
			$post_name = $check_name;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3214
		} else { // new post, or slug has changed.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3215
			$post_name = sanitize_title($post_name);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3216
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3217
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3218
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3219
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3220
	 * If the post date is empty (due to having been new or a draft) and status
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3221
	 * is not 'draft' or 'pending', set date to now.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3222
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3223
	if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' == $postarr['post_date'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3224
		$post_date = current_time( 'mysql' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3225
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3226
		$post_date = $postarr['post_date'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3227
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3228
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3229
	// Validate the date.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3230
	$mm = substr( $post_date, 5, 2 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3231
	$jj = substr( $post_date, 8, 2 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3232
	$aa = substr( $post_date, 0, 4 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3233
	$valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3234
	if ( ! $valid_date ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3235
		if ( $wp_error ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3236
			return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3237
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3238
			return 0;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3239
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3240
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3241
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3242
	if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3243
		if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3244
			$post_date_gmt = get_gmt_from_date( $post_date );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3245
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3246
			$post_date_gmt = '0000-00-00 00:00:00';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3247
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3248
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3249
		$post_date_gmt = $postarr['post_date_gmt'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3250
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3251
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3252
	if ( $update || '0000-00-00 00:00:00' == $post_date ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3253
		$post_modified     = current_time( 'mysql' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3254
		$post_modified_gmt = current_time( 'mysql', 1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3255
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3256
		$post_modified     = $post_date;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3257
		$post_modified_gmt = $post_date_gmt;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3258
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3259
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3260
	if ( 'attachment' !== $post_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3261
		if ( 'publish' == $post_status ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3262
			$now = gmdate('Y-m-d H:i:59');
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3263
			if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3264
				$post_status = 'future';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3265
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3266
		} elseif( 'future' == $post_status ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3267
			$now = gmdate('Y-m-d H:i:59');
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3268
			if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3269
				$post_status = 'publish';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3270
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3271
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3272
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3273
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3274
	if ( empty( $postarr['comment_status'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3275
		if ( $update ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3276
			$comment_status = 'closed';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3277
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3278
			$comment_status = get_option('default_comment_status');
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3279
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3280
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3281
		$comment_status = $postarr['comment_status'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3282
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3283
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3284
	// These variables are needed by compact() later.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3285
	$post_content_filtered = $postarr['post_content_filtered'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3286
	$post_author = empty( $postarr['post_author'] ) ? $user_id : $postarr['post_author'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3287
	$ping_status = empty( $postarr['ping_status'] ) ? get_option( 'default_ping_status' ) : $postarr['ping_status'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3288
	$to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3289
	$pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3290
	$import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3291
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3292
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3293
	 * The 'wp_insert_post_parent' filter expects all variables to be present.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3294
	 * Previously, these variables would have already been extracted
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3295
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3296
	if ( isset( $postarr['menu_order'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3297
		$menu_order = (int) $postarr['menu_order'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3298
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3299
		$menu_order = 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3300
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3301
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3302
	$post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3303
	if ( 'private' == $post_status ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3304
		$post_password = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3305
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3306
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3307
	if ( isset( $postarr['post_parent'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3308
		$post_parent = (int) $postarr['post_parent'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3309
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3310
		$post_parent = 0;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3311
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3312
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3313
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3314
	 * Filter the post parent -- used to check for and prevent hierarchy loops.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3315
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3316
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3317
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3318
	 * @param int   $post_parent Post parent ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3319
	 * @param int   $post_ID     Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3320
	 * @param array $new_postarr Array of parsed post data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3321
	 * @param array $postarr     Array of sanitized, but otherwise unmodified post data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3322
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3323
	$post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3324
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3325
	$post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3326
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3327
	// Don't unslash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3328
	$post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3329
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3330
	// Expected_slashed (everything!).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3331
	$data = compact( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3332
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3333
	$emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3334
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3335
	foreach( $emoji_fields as $emoji_field ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3336
		if ( isset( $data[ $emoji_field ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3337
			$charset = $wpdb->get_col_charset( $wpdb->posts, $emoji_field );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3338
			if ( 'utf8' === $charset ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3339
				$data[ $emoji_field ] = wp_encode_emoji( $data[ $emoji_field ] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3340
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3341
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3342
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3343
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3344
	if ( 'attachment' === $post_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3345
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3346
		 * Filter attachment post data before it is updated in or added to the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3347
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3348
		 * @since 3.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3349
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3350
		 * @param array $data    An array of sanitized attachment post data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3351
		 * @param array $postarr An array of unsanitized attachment post data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3352
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3353
		$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3354
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3355
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3356
		 * Filter slashed post data just before it is inserted into the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3357
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3358
		 * @since 2.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3359
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3360
		 * @param array $data    An array of slashed post data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3361
		 * @param array $postarr An array of sanitized, but otherwise unmodified post data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3362
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3363
		$data = apply_filters( 'wp_insert_post_data', $data, $postarr );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3364
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3365
	$data = wp_unslash( $data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3366
	$where = array( 'ID' => $post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3367
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3368
	if ( $update ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3369
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3370
		 * Fires immediately before an existing post is updated in the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3371
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3372
		 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3373
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3374
		 * @param int   $post_ID Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3375
		 * @param array $data    Array of unslashed post data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3376
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3377
		do_action( 'pre_post_update', $post_ID, $data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3378
		if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3379
			if ( $wp_error ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3380
				return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3381
			} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3382
				return 0;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3383
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3384
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3385
	} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3386
		// If there is a suggested ID, use it if not already present.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3387
		if ( ! empty( $import_id ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3388
			$import_id = (int) $import_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3389
			if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3390
				$data['ID'] = $import_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3391
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3392
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3393
		if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3394
			if ( $wp_error ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3395
				return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3396
			} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3397
				return 0;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3398
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3399
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3400
		$post_ID = (int) $wpdb->insert_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3401
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3402
		// Use the newly generated $post_ID.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3403
		$where = array( 'ID' => $post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3404
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3405
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3406
	if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3407
		$data['post_name'] = sanitize_title( $data['post_title'], $post_ID );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3408
		$wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3409
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3410
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3411
	if ( is_object_in_taxonomy( $post_type, 'category' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3412
		wp_set_post_categories( $post_ID, $post_category );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3413
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3414
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3415
	if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3416
		wp_set_post_tags( $post_ID, $postarr['tags_input'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3417
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3418
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3419
	// New-style support for all custom taxonomies.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3420
	if ( ! empty( $postarr['tax_input'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3421
		foreach ( $postarr['tax_input'] as $taxonomy => $tags ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3422
			$taxonomy_obj = get_taxonomy($taxonomy);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3423
			// array = hierarchical, string = non-hierarchical.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3424
			if ( is_array( $tags ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3425
				$tags = array_filter($tags);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3426
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3427
			if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3428
				wp_set_post_terms( $post_ID, $tags, $taxonomy );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3429
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3430
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3431
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3432
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3433
	$current_guid = get_post_field( 'guid', $post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3434
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3435
	// Set GUID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3436
	if ( ! $update && '' == $current_guid ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3437
		$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3438
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3439
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3440
	if ( 'attachment' === $postarr['post_type'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3441
		if ( ! empty( $postarr['file'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3442
			update_attached_file( $post_ID, $postarr['file'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3443
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3444
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3445
		if ( ! empty( $postarr['context'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3446
			add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3447
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3448
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3449
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3450
	clean_post_cache( $post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3451
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3452
	$post = get_post( $post_ID );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3453
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3454
	if ( ! empty( $postarr['page_template'] ) && 'page' == $data['post_type'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3455
		$post->page_template = $postarr['page_template'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3456
		$page_templates = wp_get_theme()->get_page_templates( $post );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3457
		if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3458
			if ( $wp_error ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3459
				return new WP_Error('invalid_page_template', __('The page template is invalid.'));
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3460
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3461
			update_post_meta( $post_ID, '_wp_page_template', 'default' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3462
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3463
			update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3464
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3465
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3466
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3467
	if ( 'attachment' !== $postarr['post_type'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3468
		wp_transition_post_status( $data['post_status'], $previous_status, $post );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3469
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3470
		if ( $update ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3471
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3472
			 * Fires once an existing attachment has been updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3473
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3474
			 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3475
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3476
			 * @param int $post_ID Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3477
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3478
			do_action( 'edit_attachment', $post_ID );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3479
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3480
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3481
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3482
			 * Fires once an attachment has been added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3483
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3484
			 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3485
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3486
			 * @param int $post_ID Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3487
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3488
			do_action( 'add_attachment', $post_ID );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3489
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3490
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3491
		return $post_ID;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3492
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3493
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3494
	if ( $update ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3495
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3496
		 * Fires once an existing post has been updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3497
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3498
		 * @since 1.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3499
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3500
		 * @param int     $post_ID Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3501
		 * @param WP_Post $post    Post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3502
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3503
		do_action( 'edit_post', $post_ID, $post );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3504
		$post_after = get_post($post_ID);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3505
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3506
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3507
		 * Fires once an existing post has been updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3508
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3509
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3510
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3511
		 * @param int     $post_ID      Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3512
		 * @param WP_Post $post_after   Post object following the update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3513
		 * @param WP_Post $post_before  Post object before the update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3514
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3515
		do_action( 'post_updated', $post_ID, $post_after, $post_before);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3516
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3517
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3518
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3519
	 * Fires once a post has been saved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3520
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3521
	 * The dynamic portion of the hook name, `$post->post_type`, refers to
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3522
	 * the post type slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3523
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3524
	 * @since 3.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3525
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3526
	 * @param int     $post_ID Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3527
	 * @param WP_Post $post    Post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3528
	 * @param bool    $update  Whether this is an existing post being updated or not.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3529
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3530
	do_action( "save_post_{$post->post_type}", $post_ID, $post, $update );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3531
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3532
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3533
	 * Fires once a post has been saved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3534
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3535
	 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3536
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3537
	 * @param int     $post_ID Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3538
	 * @param WP_Post $post    Post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3539
	 * @param bool    $update  Whether this is an existing post being updated or not.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3540
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3541
	do_action( 'save_post', $post_ID, $post, $update );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3542
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3543
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3544
	 * Fires once a post has been saved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3545
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3546
	 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3547
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3548
	 * @param int     $post_ID Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3549
	 * @param WP_Post $post    Post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3550
	 * @param bool    $update  Whether this is an existing post being updated or not.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3551
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3552
	do_action( 'wp_insert_post', $post_ID, $post, $update );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3553
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3554
	return $post_ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3555
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3556
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3557
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3558
 * Update a post with new post data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3559
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3560
 * The date does not have to be set for drafts. You can set the date and it will
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3561
 * not be overridden.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3562
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3563
 * @since 1.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3564
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3565
 * @param array|object $postarr  Optional. Post data. Arrays are expected to be escaped,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3566
 *                               objects are not. Default array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3567
 * @param bool         $wp_error Optional. Allow return of WP_Error on failure. Default false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3568
 * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3569
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3570
function wp_update_post( $postarr = array(), $wp_error = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3571
	if ( is_object($postarr) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3572
		// Non-escaped post was passed.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3573
		$postarr = get_object_vars($postarr);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3574
		$postarr = wp_slash($postarr);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3575
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3576
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3577
	// First, get all of the original fields.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3578
	$post = get_post($postarr['ID'], ARRAY_A);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3579
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3580
	if ( is_null( $post ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3581
		if ( $wp_error )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3582
			return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3583
		return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3584
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3585
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3586
	// Escape data pulled from DB.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3587
	$post = wp_slash($post);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3588
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3589
	// Passed post category list overwrites existing category list if not empty.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3590
	if ( isset($postarr['post_category']) && is_array($postarr['post_category'])
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3591
			 && 0 != count($postarr['post_category']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3592
		$post_cats = $postarr['post_category'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3593
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3594
		$post_cats = $post['post_category'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3595
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3596
	// Drafts shouldn't be assigned a date unless explicitly done so by the user.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3597
	if ( isset( $post['post_status'] ) && in_array($post['post_status'], array('draft', 'pending', 'auto-draft')) && empty($postarr['edit_date']) &&
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3598
			 ('0000-00-00 00:00:00' == $post['post_date_gmt']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3599
		$clear_date = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3600
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3601
		$clear_date = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3602
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3603
	// Merge old and new fields with new fields overwriting old ones.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3604
	$postarr = array_merge($post, $postarr);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3605
	$postarr['post_category'] = $post_cats;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3606
	if ( $clear_date ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3607
		$postarr['post_date'] = current_time('mysql');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3608
		$postarr['post_date_gmt'] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3609
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3610
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3611
	if ($postarr['post_type'] == 'attachment')
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3612
		return wp_insert_attachment($postarr);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3613
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3614
	return wp_insert_post( $postarr, $wp_error );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3615
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3616
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3617
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3618
 * Publish a post by transitioning the post status.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3619
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3620
 * @since 2.1.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3621
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3622
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3623
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3624
 * @param int|WP_Post $post Post ID or post object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3625
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3626
function wp_publish_post( $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3627
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3628
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3629
	if ( ! $post = get_post( $post ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3630
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3631
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3632
	if ( 'publish' == $post->post_status )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3633
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3634
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3635
	$wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3636
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3637
	clean_post_cache( $post->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3638
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3639
	$old_status = $post->post_status;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3640
	$post->post_status = 'publish';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3641
	wp_transition_post_status( 'publish', $old_status, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3642
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3643
	/** This action is documented in wp-includes/post.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3644
	do_action( 'edit_post', $post->ID, $post );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3645
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3646
	/** This action is documented in wp-includes/post.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3647
	do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3648
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3649
	/** This action is documented in wp-includes/post.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3650
	do_action( 'save_post', $post->ID, $post, true );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3651
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3652
	/** This action is documented in wp-includes/post.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3653
	do_action( 'wp_insert_post', $post->ID, $post, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3654
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3655
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3656
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3657
 * Publish future post and make sure post ID has future post status.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3658
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3659
 * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3660
 * from publishing drafts, etc.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3661
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3662
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3663
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3664
 * @param int|WP_Post $post_id Post ID or post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3665
 * @return null Nothing is returned. Which can mean that no action is required
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3666
 *              or post was published.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3667
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3668
function check_and_publish_future_post( $post_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3669
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3670
	$post = get_post($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3671
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3672
	if ( empty($post) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3673
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3674
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3675
	if ( 'future' != $post->post_status )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3676
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3677
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3678
	$time = strtotime( $post->post_date_gmt . ' GMT' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3679
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3680
	// Uh oh, someone jumped the gun!
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3681
	if ( $time > time() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3682
		wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); // clear anything else in the system
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3683
		wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3684
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3685
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3686
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3687
	return wp_publish_post($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3688
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3689
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3690
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3691
 * Computes a unique slug for the post, when given the desired slug and some post details.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3692
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3693
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3694
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3695
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3696
 * @global WP_Rewrite $wp_rewrite
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3697
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3698
 * @param string $slug        The desired slug (post_name).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3699
 * @param int    $post_ID     Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3700
 * @param string $post_status No uniqueness checks are made if the post is still draft or pending.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3701
 * @param string $post_type   Post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3702
 * @param int    $post_parent Post parent ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3703
 * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3704
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3705
function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3706
	if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3707
		return $slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3708
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3709
	global $wpdb, $wp_rewrite;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3710
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3711
	$original_slug = $slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3712
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3713
	$feeds = $wp_rewrite->feeds;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3714
	if ( ! is_array( $feeds ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3715
		$feeds = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3716
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3717
	if ( 'attachment' == $post_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3718
		// Attachment slugs must be unique across all types.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3719
		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3720
		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3721
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3722
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3723
		 * Filter whether the post slug would make a bad attachment slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3724
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3725
		 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3726
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3727
		 * @param bool   $bad_slug Whether the slug would be bad as an attachment slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3728
		 * @param string $slug     The post slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3729
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3730
		if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3731
			$suffix = 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3732
			do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3733
				$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3734
				$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3735
				$suffix++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3736
			} while ( $post_name_check );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3737
			$slug = $alt_post_name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3738
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3739
	} elseif ( is_post_type_hierarchical( $post_type ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3740
		if ( 'nav_menu_item' == $post_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3741
			return $slug;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3742
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3743
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3744
		 * Page slugs must be unique within their own trees. Pages are in a separate
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3745
		 * namespace than posts so page slugs are allowed to overlap post slugs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3746
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3747
		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3748
		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3749
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3750
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3751
		 * Filter whether the post slug would make a bad hierarchical post slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3752
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3753
		 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3754
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3755
		 * @param bool   $bad_slug    Whether the post slug would be bad in a hierarchical post context.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3756
		 * @param string $slug        The post slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3757
		 * @param string $post_type   Post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3758
		 * @param int    $post_parent Post parent ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3759
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3760
		if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug )  || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3761
			$suffix = 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3762
			do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3763
				$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3764
				$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3765
				$suffix++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3766
			} while ( $post_name_check );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3767
			$slug = $alt_post_name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3768
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3769
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3770
		// Post slugs must be unique across all posts.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3771
		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3772
		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3773
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3774
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3775
		 * Filter whether the post slug would be bad as a flat slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3776
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3777
		 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3778
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3779
		 * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3780
		 * @param string $slug      The post slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3781
		 * @param string $post_type Post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3782
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3783
		if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3784
			$suffix = 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3785
			do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3786
				$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3787
				$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3788
				$suffix++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3789
			} while ( $post_name_check );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3790
			$slug = $alt_post_name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3791
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3792
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3793
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3794
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3795
	 * Filter the unique post slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3796
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3797
	 * @since 3.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3798
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3799
	 * @param string $slug          The post slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3800
	 * @param int    $post_ID       Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3801
	 * @param string $post_status   The post status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3802
	 * @param string $post_type     Post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3803
	 * @param int    $post_parent   Post parent ID
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3804
	 * @param string $original_slug The original post slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3805
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3806
	return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3807
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3808
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3809
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3810
 * Truncate a post slug.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3811
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3812
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3813
 * @access private
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3814
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3815
 * @see utf8_uri_encode()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3816
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3817
 * @param string $slug   The slug to truncate.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3818
 * @param int    $length Optional. Max length of the slug. Default 200 (characters).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3819
 * @return string The truncated slug.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3820
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3821
function _truncate_post_slug( $slug, $length = 200 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3822
	if ( strlen( $slug ) > $length ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3823
		$decoded_slug = urldecode( $slug );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3824
		if ( $decoded_slug === $slug )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3825
			$slug = substr( $slug, 0, $length );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3826
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3827
			$slug = utf8_uri_encode( $decoded_slug, $length );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3828
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3829
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3830
	return rtrim( $slug, '-' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3831
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3832
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3833
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3834
 * Add tags to a post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3835
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3836
 * @see wp_set_post_tags()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3837
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3838
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3839
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3840
 * @param int    $post_id Optional. The Post ID. Does not default to the ID of the global $post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3841
 *                        Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3842
 * @param string $tags    Optional. The tags to set for the post, separated by commas. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3843
 * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3844
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3845
function wp_add_post_tags( $post_id = 0, $tags = '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3846
	return wp_set_post_tags($post_id, $tags, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3847
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3848
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3849
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3850
 * Set the tags for a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3851
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3852
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3853
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3854
 * @see wp_set_object_terms()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3855
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3856
 * @param int    $post_id Optional. The Post ID. Does not default to the ID of the global $post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3857
 * @param string $tags    Optional. The tags to set for the post, separated by commas.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3858
 *                        Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3859
 * @param bool   $append  Optional. If true, don't delete existing tags, just add on. If false,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3860
 *                        replace the tags with the new tags. Default false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3861
 * @return mixed Array of affected term IDs. WP_Error or false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3862
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3863
function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3864
	return wp_set_post_terms( $post_id, $tags, 'post_tag', $append);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3865
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3866
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3867
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3868
 * Set the terms for a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3869
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3870
 * @since 2.8.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3871
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3872
 * @see wp_set_object_terms()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3873
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3874
 * @param int    $post_id  Optional. The Post ID. Does not default to the ID of the global $post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3875
 * @param string $tags     Optional. The tags to set for the post, separated by commas. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3876
 * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3877
 * @param bool   $append   Optional. If true, don't delete existing tags, just add on. If false,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3878
 *                         replace the tags with the new tags. Default false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3879
 * @return mixed Array of affected term IDs. WP_Error or false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3880
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3881
function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3882
	$post_id = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3883
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3884
	if ( !$post_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3885
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3886
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3887
	if ( empty($tags) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3888
		$tags = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3889
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3890
	if ( ! is_array( $tags ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3891
		$comma = _x( ',', 'tag delimiter' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3892
		if ( ',' !== $comma )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3893
			$tags = str_replace( $comma, ',', $tags );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3894
		$tags = explode( ',', trim( $tags, " \n\t\r\0\x0B," ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3895
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3896
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3897
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3898
	 * Hierarchical taxonomies must always pass IDs rather than names so that
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3899
	 * children with the same names but different parents aren't confused.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3900
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3901
	if ( is_taxonomy_hierarchical( $taxonomy ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3902
		$tags = array_unique( array_map( 'intval', $tags ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3903
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3904
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3905
	return wp_set_object_terms( $post_id, $tags, $taxonomy, $append );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3906
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3907
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3908
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3909
 * Set categories for a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3910
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3911
 * If the post categories parameter is not set, then the default category is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3912
 * going used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3913
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3914
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3915
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3916
 * @param int       $post_ID         Optional. The Post ID. Does not default to the ID
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3917
 *                                   of the global $post. Default 0.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3918
 * @param array|int $post_categories Optional. List of categories or ID of category.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3919
 *                                   Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3920
 * @param bool      $append         If true, don't delete existing categories, just add on.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3921
 *                                  If false, replace the categories with the new categories.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3922
 * @return bool|mixed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3923
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3924
function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3925
	$post_ID = (int) $post_ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3926
	$post_type = get_post_type( $post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3927
	$post_status = get_post_status( $post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3928
	// If $post_categories isn't already an array, make it one:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3929
	$post_categories = (array) $post_categories;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3930
	if ( empty( $post_categories ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3931
		if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3932
			$post_categories = array( get_option('default_category') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3933
			$append = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3934
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3935
			$post_categories = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3936
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3937
	} elseif ( 1 == count( $post_categories ) && '' == reset( $post_categories ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3938
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3939
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3940
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3941
	return wp_set_post_terms( $post_ID, $post_categories, 'category', $append );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3942
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3943
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3944
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3945
 * Transition the post status of a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3946
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3947
 * When a post is saved, the post status is "transitioned" from one status to another,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3948
 * though this does not always mean the status has actually changed before and after
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3949
 * the save.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3950
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3951
 * For instance: When publishing a post for the first time, the post status may transition
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3952
 * from 'draft' – or some other status – to 'publish'. However, if a post is already
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3953
 * published and is simply being updated, the "old" and "new" statuses may both be 'publish'
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3954
 * before and after the transition.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3955
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3956
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3957
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3958
 * @param string  $new_status Transition to this post status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3959
 * @param string  $old_status Previous post status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3960
 * @param WP_Post $post Post data.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3961
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3962
function wp_transition_post_status( $new_status, $old_status, $post ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3963
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3964
	 * Fires when a post is transitioned from one status to another.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3965
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3966
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3967
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3968
	 * @param string  $new_status New post status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3969
	 * @param string  $old_status Old post status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3970
	 * @param WP_Post $post       Post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3971
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3972
	do_action( 'transition_post_status', $new_status, $old_status, $post );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3973
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3974
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3975
	 * Fires when a post is transitioned from one status to another.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3976
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3977
	 * The dynamic portions of the hook name, `$new_status` and `$old status`,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3978
	 * refer to the old and new post statuses, respectively.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3979
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3980
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3981
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3982
	 * @param WP_Post $post Post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3983
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3984
	do_action( "{$old_status}_to_{$new_status}", $post );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3985
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3986
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3987
	 * Fires when a post is transitioned from one status to another.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3988
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3989
	 * The dynamic portions of the hook name, `$new_status` and `$post->post_type`,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3990
	 * refer to the new post status and post type, respectively.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3991
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3992
	 * Please note: When this action is hooked using a particular post status (like
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3993
	 * 'publish', as `publish_{$post->post_type}`), it will fire both when a post is
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3994
	 * first transitioned to that status from something else, as well as upon
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3995
	 * subsequent post updates (old and new status are both the same).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3996
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3997
	 * Therefore, if you are looking to only fire a callback when a post is first
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3998
	 * transitioned to a status, use the {@see 'transition_post_status'} hook instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3999
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4000
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4001
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4002
	 * @param int     $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4003
	 * @param WP_Post $post    Post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4004
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4005
	do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4006
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4007
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4008
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4009
// Trackback and ping functions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4010
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4011
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4012
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4013
 * Add a URL to those already pinged.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4014
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4015
 * @since 1.5.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4016
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4017
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4018
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4019
 * @param int    $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4020
 * @param string $uri     Ping URI.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4021
 * @return int How many rows were updated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4022
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4023
function add_ping( $post_id, $uri ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4024
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4025
	$pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4026
	$pung = trim($pung);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4027
	$pung = preg_split('/\s/', $pung);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4028
	$pung[] = $uri;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4029
	$new = implode("\n", $pung);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4030
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4031
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4032
	 * Filter the new ping URL to add for the given post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4033
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4034
	 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4035
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4036
	 * @param string $new New ping URL to add.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4037
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4038
	$new = apply_filters( 'add_ping', $new );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4039
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4040
	// expected_slashed ($new).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4041
	$new = wp_unslash($new);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4042
	return $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4043
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4044
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4045
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4046
 * Retrieve enclosures already enclosed for a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4047
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4048
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4049
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4050
 * @param int $post_id Post ID.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4051
 * @return array List of enclosures.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4052
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4053
function get_enclosed( $post_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4054
	$custom_fields = get_post_custom( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4055
	$pung = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4056
	if ( !is_array( $custom_fields ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4057
		return $pung;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4058
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4059
	foreach ( $custom_fields as $key => $val ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4060
		if ( 'enclosure' != $key || !is_array( $val ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4061
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4062
		foreach( $val as $enc ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4063
			$enclosure = explode( "\n", $enc );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4064
			$pung[] = trim( $enclosure[ 0 ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4065
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4066
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4067
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4068
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4069
	 * Filter the list of enclosures already enclosed for the given post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4070
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4071
	 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4072
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4073
	 * @param array $pung    Array of enclosures for the given post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4074
	 * @param int   $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4075
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4076
	$pung = apply_filters( 'get_enclosed', $pung, $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4077
	return $pung;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4078
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4079
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4080
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4081
 * Retrieve URLs already pinged for a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4082
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4083
 * @since 1.5.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4084
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4085
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4086
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4087
 * @param int $post_id Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4088
 * @return array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4089
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4090
function get_pung( $post_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4091
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4092
	$pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4093
	$pung = trim($pung);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4094
	$pung = preg_split('/\s/', $pung);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4095
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4096
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4097
	 * Filter the list of already-pinged URLs for the given post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4098
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4099
	 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4100
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4101
	 * @param array $pung Array of URLs already pinged for the given post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4102
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4103
	$pung = apply_filters( 'get_pung', $pung );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4104
	return $pung;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4105
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4106
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4107
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4108
 * Retrieve URLs that need to be pinged.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4109
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4110
 * @since 1.5.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4111
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4112
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4113
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4114
 * @param int $post_id Post ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4115
 * @return array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4116
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4117
function get_to_ping( $post_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4118
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4119
	$to_ping = $wpdb->get_var( $wpdb->prepare( "SELECT to_ping FROM $wpdb->posts WHERE ID = %d", $post_id ));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4120
	$to_ping = sanitize_trackback_urls( $to_ping );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4121
	$to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4122
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4123
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4124
	 * Filter the list of URLs yet to ping for the given post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4125
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4126
	 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4127
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4128
	 * @param array $to_ping List of URLs yet to ping.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4129
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4130
	$to_ping = apply_filters( 'get_to_ping', $to_ping );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4131
	return $to_ping;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4132
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4133
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4134
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4135
 * Do trackbacks for a list of URLs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4136
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4137
 * @since 1.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4138
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4139
 * @param string $tb_list Comma separated list of URLs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4140
 * @param int    $post_id Post ID.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4141
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4142
function trackback_url_list( $tb_list, $post_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4143
	if ( ! empty( $tb_list ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4144
		// Get post data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4145
		$postdata = get_post( $post_id, ARRAY_A );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4146
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4147
		// Form an excerpt.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4148
		$excerpt = strip_tags( $postdata['post_excerpt'] ? $postdata['post_excerpt'] : $postdata['post_content'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4149
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4150
		if ( strlen( $excerpt ) > 255 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4151
			$excerpt = substr( $excerpt, 0, 252 ) . '&hellip;';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4152
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4153
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4154
		$trackback_urls = explode( ',', $tb_list );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4155
		foreach( (array) $trackback_urls as $tb_url ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4156
			$tb_url = trim( $tb_url );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4157
			trackback( $tb_url, wp_unslash( $postdata['post_title'] ), $excerpt, $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4158
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4159
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4160
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4161
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4162
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4163
// Page functions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4164
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4165
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4166
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4167
 * Get a list of page IDs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4168
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4169
 * @since 2.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4170
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4171
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4172
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4173
 * @return array List of page IDs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4174
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4175
function get_all_page_ids() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4176
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4177
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4178
	$page_ids = wp_cache_get('all_page_ids', 'posts');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4179
	if ( ! is_array( $page_ids ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4180
		$page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'page'");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4181
		wp_cache_add('all_page_ids', $page_ids, 'posts');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4182
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4183
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4184
	return $page_ids;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4185
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4186
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4187
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4188
 * Retrieves page data given a page ID or page object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4189
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4190
 * Use get_post() instead of get_page().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4191
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4192
 * @since 1.5.1
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4193
 * @deprecated 3.5.0 Use get_post()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4194
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4195
 * @param mixed  $page   Page object or page ID. Passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4196
 * @param string $output Optional. What to output. Accepts OBJECT, ARRAY_A, or ARRAY_N.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4197
 *                       Default OBJECT.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4198
 * @param string $filter Optional. How the return value should be filtered. Accepts 'raw',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4199
 *                       'edit', 'db', 'display'. Default 'raw'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4200
 * @return WP_Post|null WP_Post on success or null on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4201
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4202
function get_page( $page, $output = OBJECT, $filter = 'raw') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4203
	return get_post( $page, $output, $filter );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4204
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4205
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4206
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4207
 * Retrieves a page given its path.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4208
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4209
 * @since 2.1.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4210
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4211
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4212
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4213
 * @param string       $page_path Page path.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4214
 * @param string       $output    Optional. Output type. Accepts OBJECT, ARRAY_N, or ARRAY_A.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4215
 *                                Default OBJECT.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4216
 * @param string|array $post_type Optional. Post type or array of post types. Default 'page'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4217
 * @return WP_Post|null WP_Post on success or null on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4218
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4219
function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4220
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4221
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4222
	$page_path = rawurlencode(urldecode($page_path));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4223
	$page_path = str_replace('%2F', '/', $page_path);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4224
	$page_path = str_replace('%20', ' ', $page_path);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4225
	$parts = explode( '/', trim( $page_path, '/' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4226
	$parts = esc_sql( $parts );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4227
	$parts = array_map( 'sanitize_title_for_query', $parts );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4228
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4229
	$in_string = "'" . implode( "','", $parts ) . "'";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4230
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4231
	if ( is_array( $post_type ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4232
		$post_types = $post_type;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4233
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4234
		$post_types = array( $post_type, 'attachment' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4235
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4236
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4237
	$post_types = esc_sql( $post_types );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4238
	$post_type_in_string = "'" . implode( "','", $post_types ) . "'";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4239
	$sql = "
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4240
		SELECT ID, post_name, post_parent, post_type
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4241
		FROM $wpdb->posts
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4242
		WHERE post_name IN ($in_string)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4243
		AND post_type IN ($post_type_in_string)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4244
	";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4245
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4246
	$pages = $wpdb->get_results( $sql, OBJECT_K );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4247
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4248
	$revparts = array_reverse( $parts );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4249
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4250
	$foundid = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4251
	foreach ( (array) $pages as $page ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4252
		if ( $page->post_name == $revparts[0] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4253
			$count = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4254
			$p = $page;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4255
			while ( $p->post_parent != 0 && isset( $pages[ $p->post_parent ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4256
				$count++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4257
				$parent = $pages[ $p->post_parent ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4258
				if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4259
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4260
				$p = $parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4261
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4262
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4263
			if ( $p->post_parent == 0 && $count+1 == count( $revparts ) && $p->post_name == $revparts[ $count ] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4264
				$foundid = $page->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4265
				if ( $page->post_type == $post_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4266
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4267
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4268
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4269
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4270
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4271
	if ( $foundid )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4272
		return get_post( $foundid, $output );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4273
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4274
	return null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4275
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4276
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4277
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4278
 * Retrieve a page given its title.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4279
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4280
 * @since 2.1.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4281
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4282
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4283
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4284
 * @param string       $page_title Page title
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4285
 * @param string       $output     Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4286
 *                                 Default OBJECT.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4287
 * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4288
 * @return WP_Post|null WP_Post on success or null on failure
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4289
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4290
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4291
	global $wpdb;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4292
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4293
	if ( is_array( $post_type ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4294
		$post_type = esc_sql( $post_type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4295
		$post_type_in_string = "'" . implode( "','", $post_type ) . "'";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4296
		$sql = $wpdb->prepare( "
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4297
			SELECT ID
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4298
			FROM $wpdb->posts
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4299
			WHERE post_title = %s
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4300
			AND post_type IN ($post_type_in_string)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4301
		", $page_title );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4302
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4303
		$sql = $wpdb->prepare( "
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4304
			SELECT ID
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4305
			FROM $wpdb->posts
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4306
			WHERE post_title = %s
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4307
			AND post_type = %s
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4308
		", $page_title, $post_type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4309
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4310
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4311
	$page = $wpdb->get_var( $sql );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4312
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4313
	if ( $page )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4314
		return get_post( $page, $output );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4315
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4316
	return null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4317
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4318
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4319
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4320
 * Retrieve child pages from list of pages matching page ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4321
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4322
 * Matches against the pages parameter against the page ID. Also matches all
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4323
 * children for the same to retrieve all children of a page. Does not make any
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4324
 * SQL queries to get the children.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4325
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4326
 * @since 1.5.1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4327
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4328
 * @param int   $page_id    Page ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4329
 * @param array $pages      List of pages' objects.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4330
 * @return array List of page children.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4331
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4332
function get_page_children( $page_id, $pages ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4333
	$page_list = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4334
	foreach ( (array) $pages as $page ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4335
		if ( $page->post_parent == $page_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4336
			$page_list[] = $page;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4337
			if ( $children = get_page_children( $page->ID, $pages ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4338
				$page_list = array_merge( $page_list, $children );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4339
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4340
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4341
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4342
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4343
	return $page_list;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4344
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4345
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4346
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4347
 * Order the pages with children under parents in a flat list.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4348
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4349
 * It uses auxiliary structure to hold parent-children relationships and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4350
 * runs in O(N) complexity
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4351
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4352
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4353
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4354
 * @param array $pages   Posts array, passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4355
 * @param int   $page_id Optional. Parent page ID. Default 0.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4356
 * @return array A list arranged by hierarchy. Children immediately follow their parents.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4357
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4358
function get_page_hierarchy( &$pages, $page_id = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4359
	if ( empty( $pages ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4360
		$result = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4361
		return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4362
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4363
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4364
	$children = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4365
	foreach ( (array) $pages as $p ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4366
		$parent_id = intval( $p->post_parent );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4367
		$children[ $parent_id ][] = $p;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4368
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4369
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4370
	$result = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4371
	_page_traverse_name( $page_id, $children, $result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4372
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4373
	return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4374
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4375
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4376
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4377
 * Traverse and return all the nested children post names of a root page.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4378
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4379
 * $children contains parent-children relations
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4380
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4381
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4382
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4383
 * @see _page_traverse_name()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4384
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4385
 * @param int   $page_id   Page ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4386
 * @param array &$children Parent-children relations, passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4387
 * @param array &$result   Result, passed by reference.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4388
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4389
function _page_traverse_name( $page_id, &$children, &$result ){
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4390
	if ( isset( $children[ $page_id ] ) ){
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4391
		foreach( (array)$children[ $page_id ] as $child ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4392
			$result[ $child->ID ] = $child->post_name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4393
			_page_traverse_name( $child->ID, $children, $result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4394
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4395
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4396
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4397
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4398
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4399
 * Build URI for a page.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4400
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4401
 * Sub pages will be in the "directory" under the parent page post name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4402
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4403
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4404
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4405
 * @param WP_Post|object|int $page Page object or page ID.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4406
 * @return string|false Page URI, false on error.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4407
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4408
function get_page_uri( $page ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4409
	$page = get_post( $page );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4410
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4411
	if ( ! $page )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4412
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4413
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4414
	$uri = $page->post_name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4415
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4416
	foreach ( $page->ancestors as $parent ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4417
		$uri = get_post( $parent )->post_name . '/' . $uri;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4418
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4419
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4420
	return $uri;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4421
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4422
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4423
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4424
 * Retrieve a list of pages.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4425
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4426
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4427
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4428
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4429
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4430
 * @param array|string $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4431
 *     Optional. Array or string of arguments to retrieve pages.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4432
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4433
 *     @type int          $child_of     Page ID to return child and grandchild pages of.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4434
 *                                      Default 0, or no restriction.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4435
 *     @type string       $sort_order   How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4436
 *     @type string       $sort_column  What columns to sort pages by, comma-separated. Accepts 'post_author',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4437
 *                                      'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4438
 *                                      'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4439
 *                                      'post_' can be omitted for any values that start with it.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4440
 *                                      Default 'post_title'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4441
 *     @type bool         $hierarchical Whether to return pages hierarchically. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4442
 *     @type array        $exclude      Array of page IDs to exclude. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4443
 *     @type array        $include      Array of page IDs to include. Cannot be used with `$child_of`,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4444
 *                                      `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4445
 *                                      Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4446
 *     @type string       $meta_key     Only include pages with this meta key. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4447
 *     @type string       $meta_value   Only include pages with this meta value. Requires `$meta_key`.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4448
 *                                      Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4449
 *     @type string       $authors      A comma-separated list of author IDs. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4450
 *     @type int          $parent       Page ID to return direct children of. `$hierarchical` must be false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4451
 *                                      Default -1, or no restriction.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4452
 *     @type string|array $exclude_tree Comma-separated string or array of page IDs to exclude.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4453
 *                                      Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4454
 *     @type int          $number       The number of pages to return. Default 0, or all pages.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4455
 *     @type int          $offset       The number of pages to skip before returning. Requires `$number`.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4456
 *                                      Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4457
 *     @type string       $post_type    The post type to query. Default 'page'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4458
 *     @type string       $post_status  A comma-separated list of post status types to include.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4459
 *                                      Default 'publish'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4460
 * }
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4461
 * @return array List of pages matching defaults or `$args`.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4462
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4463
function get_pages( $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4464
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4465
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4466
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4467
		'child_of' => 0, 'sort_order' => 'ASC',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4468
		'sort_column' => 'post_title', 'hierarchical' => 1,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4469
		'exclude' => array(), 'include' => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4470
		'meta_key' => '', 'meta_value' => '',
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4471
		'authors' => '', 'parent' => -1, 'exclude_tree' => array(),
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4472
		'number' => '', 'offset' => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4473
		'post_type' => 'page', 'post_status' => 'publish',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4474
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4475
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4476
	$r = wp_parse_args( $args, $defaults );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4477
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4478
	$number = (int) $r['number'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4479
	$offset = (int) $r['offset'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4480
	$child_of = (int) $r['child_of'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4481
	$hierarchical = $r['hierarchical'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4482
	$exclude = $r['exclude'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4483
	$meta_key = $r['meta_key'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4484
	$meta_value = $r['meta_value'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4485
	$parent = $r['parent'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4486
	$post_status = $r['post_status'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4487
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4488
	// Make sure the post type is hierarchical.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4489
	$hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4490
	if ( ! in_array( $r['post_type'], $hierarchical_post_types ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4491
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4492
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4493
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4494
	if ( $parent > 0 && ! $child_of ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4495
		$hierarchical = false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4496
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4497
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4498
	// Make sure we have a valid post status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4499
	if ( ! is_array( $post_status ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4500
		$post_status = explode( ',', $post_status );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4501
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4502
	if ( array_diff( $post_status, get_post_stati() ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4503
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4504
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4505
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4506
	// $args can be whatever, only use the args defined in defaults to compute the key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4507
	$key = md5( serialize( wp_array_slice_assoc( $r, array_keys( $defaults ) ) ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4508
	$last_changed = wp_cache_get( 'last_changed', 'posts' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4509
	if ( ! $last_changed ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4510
		$last_changed = microtime();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4511
		wp_cache_set( 'last_changed', $last_changed, 'posts' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4512
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4513
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4514
	$cache_key = "get_pages:$key:$last_changed";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4515
	if ( $cache = wp_cache_get( $cache_key, 'posts' ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4516
		// Convert to WP_Post instances.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4517
		$pages = array_map( 'get_post', $cache );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4518
		/** This filter is documented in wp-includes/post.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4519
		$pages = apply_filters( 'get_pages', $pages, $r );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4520
		return $pages;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4521
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4522
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4523
	$inclusions = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4524
	if ( ! empty( $r['include'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4525
		$child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4526
		$parent = -1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4527
		$exclude = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4528
		$meta_key = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4529
		$meta_value = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4530
		$hierarchical = false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4531
		$incpages = wp_parse_id_list( $r['include'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4532
		if ( ! empty( $incpages ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4533
			$inclusions = ' AND ID IN (' . implode( ',', $incpages ) .  ')';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4534
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4535
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4536
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4537
	$exclusions = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4538
	if ( ! empty( $exclude ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4539
		$expages = wp_parse_id_list( $exclude );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4540
		if ( ! empty( $expages ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4541
			$exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) .  ')';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4542
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4543
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4544
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4545
	$author_query = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4546
	if ( ! empty( $r['authors'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4547
		$post_authors = preg_split( '/[\s,]+/', $r['authors'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4548
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4549
		if ( ! empty( $post_authors ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4550
			foreach ( $post_authors as $post_author ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4551
				//Do we have an author id or an author login?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4552
				if ( 0 == intval($post_author) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4553
					$post_author = get_user_by('login', $post_author);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4554
					if ( empty( $post_author ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4555
						continue;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4556
					}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4557
					if ( empty( $post_author->ID ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4558
						continue;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4559
					}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4560
					$post_author = $post_author->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4561
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4562
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4563
				if ( '' == $author_query ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4564
					$author_query = $wpdb->prepare(' post_author = %d ', $post_author);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4565
				} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4566
					$author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4567
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4568
			}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4569
			if ( '' != $author_query ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4570
				$author_query = " AND ($author_query)";
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4571
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4572
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4573
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4574
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4575
	$join = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4576
	$where = "$exclusions $inclusions ";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4577
	if ( '' !== $meta_key || '' !== $meta_value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4578
		$join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4579
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4580
		// meta_key and meta_value might be slashed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4581
		$meta_key = wp_unslash($meta_key);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4582
		$meta_value = wp_unslash($meta_value);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4583
		if ( '' !== $meta_key ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4584
			$where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4585
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4586
		if ( '' !== $meta_value ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4587
			$where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4588
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4589
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4590
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4591
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4592
	if ( is_array( $parent ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4593
		$post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4594
		if ( ! empty( $post_parent__in ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4595
			$where .= " AND post_parent IN ($post_parent__in)";
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4596
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4597
	} elseif ( $parent >= 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4598
		$where .= $wpdb->prepare(' AND post_parent = %d ', $parent);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4599
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4600
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4601
	if ( 1 == count( $post_status ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4602
		$where_post_type = $wpdb->prepare( "post_type = %s AND post_status = %s", $r['post_type'], reset( $post_status ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4603
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4604
		$post_status = implode( "', '", $post_status );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4605
		$where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $r['post_type'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4606
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4607
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4608
	$orderby_array = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4609
	$allowed_keys = array( 'author', 'post_author', 'date', 'post_date', 'title', 'post_title', 'name', 'post_name', 'modified',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4610
		'post_modified', 'modified_gmt', 'post_modified_gmt', 'menu_order', 'parent', 'post_parent',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4611
		'ID', 'rand', 'comment_count' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4612
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4613
	foreach ( explode( ',', $r['sort_column'] ) as $orderby ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4614
		$orderby = trim( $orderby );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4615
		if ( ! in_array( $orderby, $allowed_keys ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4616
			continue;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4617
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4618
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4619
		switch ( $orderby ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4620
			case 'menu_order':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4621
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4622
			case 'ID':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4623
				$orderby = "$wpdb->posts.ID";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4624
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4625
			case 'rand':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4626
				$orderby = 'RAND()';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4627
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4628
			case 'comment_count':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4629
				$orderby = "$wpdb->posts.comment_count";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4630
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4631
			default:
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4632
				if ( 0 === strpos( $orderby, 'post_' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4633
					$orderby = "$wpdb->posts." . $orderby;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4634
				} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4635
					$orderby = "$wpdb->posts.post_" . $orderby;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4636
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4637
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4638
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4639
		$orderby_array[] = $orderby;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4640
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4641
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4642
	$sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4643
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4644
	$sort_order = strtoupper( $r['sort_order'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4645
	if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ) ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4646
		$sort_order = 'ASC';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4647
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4648
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4649
	$query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where ";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4650
	$query .= $author_query;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4651
	$query .= " ORDER BY " . $sort_column . " " . $sort_order ;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4652
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4653
	if ( ! empty( $number ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4654
		$query .= ' LIMIT ' . $offset . ',' . $number;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4655
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4656
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4657
	$pages = $wpdb->get_results($query);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4658
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4659
	if ( empty($pages) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4660
		/** This filter is documented in wp-includes/post.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4661
		$pages = apply_filters( 'get_pages', array(), $r );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4662
		return $pages;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4663
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4664
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4665
	// Sanitize before caching so it'll only get done once.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4666
	$num_pages = count($pages);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4667
	for ($i = 0; $i < $num_pages; $i++) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4668
		$pages[$i] = sanitize_post($pages[$i], 'raw');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4669
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4670
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4671
	// Update cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4672
	update_post_cache( $pages );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4673
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4674
	if ( $child_of || $hierarchical ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4675
		$pages = get_page_children($child_of, $pages);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4676
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4677
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4678
	if ( ! empty( $r['exclude_tree'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4679
		$exclude = wp_parse_id_list( $r['exclude_tree'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4680
		foreach( $exclude as $id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4681
			$children = get_page_children( $id, $pages );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4682
			foreach ( $children as $child ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4683
				$exclude[] = $child->ID;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4684
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4685
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4686
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4687
		$num_pages = count( $pages );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4688
		for ( $i = 0; $i < $num_pages; $i++ ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4689
			if ( in_array( $pages[$i]->ID, $exclude ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4690
				unset( $pages[$i] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4691
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4692
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4693
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4694
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4695
	$page_structure = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4696
	foreach ( $pages as $page ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4697
		$page_structure[] = $page->ID;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4698
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4699
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4700
	wp_cache_set( $cache_key, $page_structure, 'posts' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4701
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4702
	// Convert to WP_Post instances
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4703
	$pages = array_map( 'get_post', $pages );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4704
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4705
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4706
	 * Filter the retrieved list of pages.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4707
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4708
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4709
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4710
	 * @param array $pages List of pages to retrieve.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4711
	 * @param array $r     Array of get_pages() arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4712
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4713
	$pages = apply_filters( 'get_pages', $pages, $r );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4714
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4715
	return $pages;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4716
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4717
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4718
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4719
// Attachment functions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4720
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4721
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4722
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4723
 * Check if the attachment URI is local one and is really an attachment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4724
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4725
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4726
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4727
 * @param string $url URL to check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4728
 * @return bool True on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4729
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4730
function is_local_attachment($url) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4731
	if (strpos($url, home_url()) === false)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4732
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4733
	if (strpos($url, home_url('/?attachment_id=')) !== false)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4734
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4735
	if ( $id = url_to_postid($url) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4736
		$post = get_post($id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4737
		if ( 'attachment' == $post->post_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4738
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4739
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4740
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4741
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4742
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4743
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4744
 * Insert an attachment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4745
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4746
 * If you set the 'ID' in the $args parameter, it will mean that you are
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4747
 * updating and attempt to update the attachment. You can also set the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4748
 * attachment name or title by setting the key 'post_name' or 'post_title'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4749
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4750
 * You can set the dates for the attachment manually by setting the 'post_date'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4751
 * and 'post_date_gmt' keys' values.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4752
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4753
 * By default, the comments will use the default settings for whether the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4754
 * comments are allowed. You can close them manually or keep them open by
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4755
 * setting the value for the 'comment_status' key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4756
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4757
 * @since 2.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4758
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4759
 * @see wp_insert_post()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4760
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4761
 * @param string|array $args   Arguments for inserting an attachment.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4762
 * @param string       $file   Optional. Filename.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4763
 * @param int          $parent Optional. Parent post ID.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4764
 * @return int Attachment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4765
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4766
function wp_insert_attachment( $args, $file = false, $parent = 0 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4767
	$defaults = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4768
		'file'        => $file,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4769
		'post_parent' => 0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4770
	);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4771
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4772
	$data = wp_parse_args( $args, $defaults );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4773
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4774
	if ( ! empty( $parent ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4775
		$data['post_parent'] = $parent;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4776
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4777
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4778
	$data['post_type'] = 'attachment';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4779
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4780
	return wp_insert_post( $data );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4781
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4782
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4783
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4784
 * Trash or delete an attachment.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4785
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4786
 * When an attachment is permanently deleted, the file will also be removed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4787
 * Deletion removes all post meta fields, taxonomy, comments, etc. associated
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4788
 * with the attachment (except the main post).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4789
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4790
 * The attachment is moved to the trash instead of permanently deleted unless trash
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4791
 * for media is disabled, item is already in the trash, or $force_delete is true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4792
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4793
 * @since 2.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4794
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4795
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4796
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4797
 * @param int  $post_id      Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4798
 * @param bool $force_delete Optional. Whether to bypass trash and force deletion.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4799
 *                           Default false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4800
 * @return mixed False on failure. Post data on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4801
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4802
function wp_delete_attachment( $post_id, $force_delete = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4803
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4804
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4805
	if ( !$post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4806
		return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4807
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4808
	if ( 'attachment' != $post->post_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4809
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4810
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4811
	if ( !$force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' != $post->post_status )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4812
		return wp_trash_post( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4813
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4814
	delete_post_meta($post_id, '_wp_trash_meta_status');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4815
	delete_post_meta($post_id, '_wp_trash_meta_time');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4816
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4817
	$meta = wp_get_attachment_metadata( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4818
	$backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4819
	$file = get_attached_file( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4820
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4821
	if ( is_multisite() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4822
		delete_transient( 'dirsize_cache' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4823
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4824
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4825
	 * Fires before an attachment is deleted, at the start of wp_delete_attachment().
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4826
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4827
	 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4828
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4829
	 * @param int $post_id Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4830
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4831
	do_action( 'delete_attachment', $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4832
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4833
	wp_delete_object_term_relationships($post_id, array('category', 'post_tag'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4834
	wp_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4835
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4836
	// Delete all for any posts.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4837
	delete_metadata( 'post', null, '_thumbnail_id', $post_id, true );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4838
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4839
	$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4840
	foreach ( $comment_ids as $comment_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4841
		wp_delete_comment( $comment_id, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4842
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4843
	$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4844
	foreach ( $post_meta_ids as $mid )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4845
		delete_metadata_by_mid( 'post', $mid );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4846
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4847
	/** This action is documented in wp-includes/post.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4848
	do_action( 'delete_post', $post_id );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4849
	$result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4850
	if ( ! $result ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4851
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4852
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4853
	/** This action is documented in wp-includes/post.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4854
	do_action( 'deleted_post', $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4855
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4856
	$uploadpath = wp_upload_dir();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4857
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4858
	if ( ! empty($meta['thumb']) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4859
		// Don't delete the thumb if another attachment uses it.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4860
		if (! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id)) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4861
			$thumbfile = str_replace(basename($file), $meta['thumb'], $file);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4862
			/** This filter is documented in wp-includes/functions.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4863
			$thumbfile = apply_filters( 'wp_delete_file', $thumbfile );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4864
			@ unlink( path_join($uploadpath['basedir'], $thumbfile) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4865
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4866
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4867
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4868
	// Remove intermediate and backup images if there are any.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4869
	if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4870
		foreach ( $meta['sizes'] as $size => $sizeinfo ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4871
			$intermediate_file = str_replace( basename( $file ), $sizeinfo['file'], $file );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4872
			/** This filter is documented in wp-includes/functions.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4873
			$intermediate_file = apply_filters( 'wp_delete_file', $intermediate_file );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4874
			@ unlink( path_join( $uploadpath['basedir'], $intermediate_file ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4875
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4876
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4877
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4878
	if ( is_array($backup_sizes) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4879
		foreach ( $backup_sizes as $size ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4880
			$del_file = path_join( dirname($meta['file']), $size['file'] );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4881
			/** This filter is documented in wp-includes/functions.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4882
			$del_file = apply_filters( 'wp_delete_file', $del_file );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4883
			@ unlink( path_join($uploadpath['basedir'], $del_file) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4884
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4885
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4886
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4887
	wp_delete_file( $file );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4888
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4889
	clean_post_cache( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4890
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4891
	return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4892
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4893
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4894
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4895
 * Retrieve attachment meta field for attachment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4896
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4897
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4898
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4899
 * @param int  $post_id    Attachment ID. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4900
 * @param bool $unfiltered Optional. If true, filters are not run. Default false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4901
 * @return string|bool Attachment meta field. False on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4902
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4903
function wp_get_attachment_metadata( $post_id = 0, $unfiltered = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4904
	$post_id = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4905
	if ( !$post = get_post( $post_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4906
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4907
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4908
	$data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4909
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4910
	if ( $unfiltered )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4911
		return $data;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4912
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4913
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4914
	 * Filter the attachment meta data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4915
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4916
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4917
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4918
	 * @param array|bool $data    Array of meta data for the given attachment, or false
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4919
	 *                            if the object does not exist.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4920
	 * @param int        $post_id Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4921
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4922
	return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4923
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4924
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4925
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4926
 * Update metadata for an attachment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4927
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4928
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4929
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4930
 * @param int   $post_id Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4931
 * @param array $data    Attachment data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4932
 * @return int|bool False if $post is invalid.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4933
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4934
function wp_update_attachment_metadata( $post_id, $data ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4935
	$post_id = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4936
	if ( !$post = get_post( $post_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4937
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4938
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4939
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4940
	 * Filter the updated attachment meta data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4941
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4942
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4943
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4944
	 * @param array $data    Array of updated attachment meta data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4945
	 * @param int   $post_id Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4946
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4947
	if ( $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4948
		return update_post_meta( $post->ID, '_wp_attachment_metadata', $data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4949
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4950
		return delete_post_meta( $post->ID, '_wp_attachment_metadata' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4951
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4952
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4953
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4954
 * Retrieve the URL for an attachment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4955
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4956
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4957
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4958
 * @param int $post_id Optional. Attachment ID. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4959
 * @return string|bool Attachment URL, otherwise false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4960
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4961
function wp_get_attachment_url( $post_id = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4962
	$post_id = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4963
	if ( !$post = get_post( $post_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4964
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4965
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4966
	if ( 'attachment' != $post->post_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4967
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4968
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4969
	$url = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4970
	// Get attached file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4971
	if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4972
		// Get upload directory.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4973
		if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4974
			// Check that the upload base exists in the file location.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4975
			if ( 0 === strpos( $file, $uploads['basedir'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4976
				// Replace file location with url location.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4977
				$url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4978
			} elseif ( false !== strpos($file, 'wp-content/uploads') ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4979
				$url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4980
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4981
				// It's a newly-uploaded file, therefore $file is relative to the basedir.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4982
				$url = $uploads['baseurl'] . "/$file";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4983
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4984
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4985
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4986
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4987
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4988
	 * If any of the above options failed, Fallback on the GUID as used pre-2.7,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4989
	 * not recommended to rely upon this.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4990
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4991
	if ( empty($url) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4992
		$url = get_the_guid( $post->ID );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4993
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4994
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4995
	// On SSL front-end, URLs should be HTTPS.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4996
	if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4997
		$url = set_url_scheme( $url );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4998
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4999
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5000
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5001
	 * Filter the attachment URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5002
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5003
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5004
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5005
	 * @param string $url     URL for the given attachment.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5006
	 * @param int    $post_id Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5007
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5008
	$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5009
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5010
	if ( empty( $url ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5011
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5012
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5013
	return $url;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5014
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5015
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5016
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5017
 * Retrieve thumbnail for an attachment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5018
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5019
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5020
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5021
 * @param int $post_id Optional. Attachment ID. Default 0.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5022
 * @return mixed False on failure. Thumbnail file path on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5023
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5024
function wp_get_attachment_thumb_file( $post_id = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5025
	$post_id = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5026
	if ( !$post = get_post( $post_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5027
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5028
	if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5029
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5030
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5031
	$file = get_attached_file( $post->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5032
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5033
	if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5034
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5035
		 * Filter the attachment thumbnail file path.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5036
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5037
		 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5038
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5039
		 * @param string $thumbfile File path to the attachment thumbnail.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5040
		 * @param int    $post_id   Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5041
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5042
		return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5043
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5044
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5045
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5046
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5047
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5048
 * Retrieve URL for an attachment thumbnail.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5049
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5050
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5051
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5052
 * @param int $post_id Optional. Attachment ID. Default 0.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5053
 * @return string|bool False on failure. Thumbnail URL on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5054
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5055
function wp_get_attachment_thumb_url( $post_id = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5056
	$post_id = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5057
	if ( !$post = get_post( $post_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5058
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5059
	if ( !$url = wp_get_attachment_url( $post->ID ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5060
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5061
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5062
	$sized = image_downsize( $post_id, 'thumbnail' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5063
	if ( $sized )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5064
		return $sized[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5065
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5066
	if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5067
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5068
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5069
	$url = str_replace(basename($url), basename($thumb), $url);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5070
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5071
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5072
	 * Filter the attachment thumbnail URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5073
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5074
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5075
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5076
	 * @param string $url     URL for the attachment thumbnail.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5077
	 * @param int    $post_id Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5078
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5079
	return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5080
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5081
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5082
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5083
 * Verifies an attachment is of a given type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5084
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5085
 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5086
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5087
 * @param string      $type    Attachment type. Accepts 'image', 'audio', or 'video'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5088
 * @param int|WP_Post $post_id Optional. Attachment ID. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5089
 * @return bool True if one of the accepted types, false otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5090
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5091
function wp_attachment_is( $type, $post_id = 0 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5092
	if ( ! $post = get_post( $post_id ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5093
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5094
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5095
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5096
	if ( ! $file = get_attached_file( $post->ID ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5097
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5098
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5099
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5100
	if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5101
		return true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5102
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5103
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5104
	$check = wp_check_filetype( $file );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5105
	if ( empty( $check['ext'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5106
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5107
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5108
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5109
	$ext = $check['ext'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5110
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5111
	if ( 'import' !== $post->post_mime_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5112
		return $type === $ext;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5113
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5114
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5115
	switch ( $type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5116
	case 'image':
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5117
		$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5118
		return in_array( $ext, $image_exts );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5119
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5120
	case 'audio':
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5121
		return in_array( $ext, wp_get_audio_extensions() );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5122
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5123
	case 'video':
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5124
		return in_array( $ext, wp_get_video_extensions() );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5125
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5126
	default:
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5127
		return $type === $ext;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5128
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5129
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5130
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5131
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5132
 * Checks if the attachment is an image.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5133
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5134
 * @since 2.1.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5135
 * @since 4.2.0 Modified into wrapper for wp_attachment_is()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5136
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5137
 * @param int|WP_Post $post Optional. Attachment ID. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5138
 * @return bool Whether the attachment is an image.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5139
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5140
function wp_attachment_is_image( $post = 0 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5141
	return wp_attachment_is( 'image', $post );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5142
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5143
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5144
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5145
 * Retrieve the icon for a MIME type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5146
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5147
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5148
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5149
 * @param string|int $mime MIME type or attachment ID.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5150
 * @return string|bool Icon, false otherwise.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5151
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5152
function wp_mime_type_icon( $mime = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5153
	if ( !is_numeric($mime) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5154
		$icon = wp_cache_get("mime_type_icon_$mime");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5155
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5156
	$post_id = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5157
	if ( empty($icon) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5158
		$post_mimes = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5159
		if ( is_numeric($mime) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5160
			$mime = (int) $mime;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5161
			if ( $post = get_post( $mime ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5162
				$post_id = (int) $post->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5163
				$ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $post->guid);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5164
				if ( !empty($ext) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5165
					$post_mimes[] = $ext;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5166
					if ( $ext_type = wp_ext2type( $ext ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5167
						$post_mimes[] = $ext_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5168
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5169
				$mime = $post->post_mime_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5170
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5171
				$mime = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5172
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5173
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5174
			$post_mimes[] = $mime;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5175
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5176
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5177
		$icon_files = wp_cache_get('icon_files');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5178
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5179
		if ( !is_array($icon_files) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5180
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5181
			 * Filter the icon directory path.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5182
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5183
			 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5184
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5185
			 * @param string $path Icon directory absolute path.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5186
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5187
			$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5188
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5189
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5190
			 * Filter the icon directory URI.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5191
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5192
			 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5193
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5194
			 * @param string $uri Icon directory URI.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5195
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5196
			$icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5197
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5198
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5199
			 * Filter the list of icon directory URIs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5200
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5201
			 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5202
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5203
			 * @param array $uris List of icon directory URIs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5204
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5205
			$dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5206
			$icon_files = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5207
			while ( $dirs ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5208
				$keys = array_keys( $dirs );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5209
				$dir = array_shift( $keys );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5210
				$uri = array_shift($dirs);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5211
				if ( $dh = opendir($dir) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5212
					while ( false !== $file = readdir($dh) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5213
						$file = basename($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5214
						if ( substr($file, 0, 1) == '.' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5215
							continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5216
						if ( !in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg') ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5217
							if ( is_dir("$dir/$file") )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5218
								$dirs["$dir/$file"] = "$uri/$file";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5219
							continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5220
						}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5221
						$icon_files["$dir/$file"] = "$uri/$file";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5222
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5223
					closedir($dh);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5224
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5225
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5226
			wp_cache_add( 'icon_files', $icon_files, 'default', 600 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5227
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5228
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5229
		$types = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5230
		// Icon basename - extension = MIME wildcard.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5231
		foreach ( $icon_files as $file => $uri )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5232
			$types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5233
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5234
		if ( ! empty($mime) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5235
			$post_mimes[] = substr($mime, 0, strpos($mime, '/'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5236
			$post_mimes[] = substr($mime, strpos($mime, '/') + 1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5237
			$post_mimes[] = str_replace('/', '_', $mime);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5238
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5239
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5240
		$matches = wp_match_mime_types(array_keys($types), $post_mimes);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5241
		$matches['default'] = array('default');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5242
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5243
		foreach ( $matches as $match => $wilds ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5244
			if ( isset($types[$wilds[0]])) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5245
				$icon = $types[$wilds[0]];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5246
				if ( !is_numeric($mime) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5247
					wp_cache_add("mime_type_icon_$mime", $icon);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5248
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5249
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5250
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5251
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5252
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5253
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5254
	 * Filter the mime type icon.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5255
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5256
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5257
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5258
	 * @param string $icon    Path to the mime type icon.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5259
	 * @param string $mime    Mime type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5260
	 * @param int    $post_id Attachment ID. Will equal 0 if the function passed
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5261
	 *                        the mime type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5262
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5263
	return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5264
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5265
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5266
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5267
 * Check for changed slugs for published post objects and save the old slug.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5268
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5269
 * The function is used when a post object of any type is updated,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5270
 * by comparing the current and previous post objects.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5271
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5272
 * If the slug was changed and not already part of the old slugs then it will be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5273
 * added to the post meta field ('_wp_old_slug') for storing old slugs for that
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5274
 * post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5275
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5276
 * The most logically usage of this function is redirecting changed post objects, so
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5277
 * that those that linked to an changed post will be redirected to the new post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5278
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5279
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5280
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5281
 * @param int     $post_id     Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5282
 * @param WP_Post $post        The Post Object
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5283
 * @param WP_Post $post_before The Previous Post Object
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5284
 * @return int Same as $post_id
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5285
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5286
function wp_check_for_changed_slugs( $post_id, $post, $post_before ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5287
	// Don't bother if it hasnt changed.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5288
	if ( $post->post_name == $post_before->post_name )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5289
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5290
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5291
	// We're only concerned with published, non-hierarchical objects.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5292
	if ( $post->post_status != 'publish' || is_post_type_hierarchical( $post->post_type ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5293
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5294
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5295
	$old_slugs = (array) get_post_meta($post_id, '_wp_old_slug');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5296
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5297
	// If we haven't added this old slug before, add it now.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5298
	if ( !empty( $post_before->post_name ) && !in_array($post_before->post_name, $old_slugs) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5299
		add_post_meta($post_id, '_wp_old_slug', $post_before->post_name);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5300
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5301
	// If the new slug was used previously, delete it from the list.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5302
	if ( in_array($post->post_name, $old_slugs) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5303
		delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5304
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5305
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5306
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5307
 * Retrieve the private post SQL based on capability.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5308
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5309
 * This function provides a standardized way to appropriately select on the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5310
 * post_status of a post type. The function will return a piece of SQL code
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5311
 * that can be added to a WHERE clause; this SQL is constructed to allow all
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5312
 * published posts, and all private posts to which the user has access.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5313
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5314
 * @since 2.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5315
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5316
 * @param string $post_type Post type. Currently only supports 'post' or 'page'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5317
 * @return string SQL code that can be added to a where clause.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5318
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5319
function get_private_posts_cap_sql( $post_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5320
	return get_posts_by_author_sql( $post_type, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5321
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5322
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5323
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5324
 * Retrieve the post SQL based on capability, author, and type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5325
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5326
 * @since 3.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5327
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5328
 * @see get_private_posts_cap_sql()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5329
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5330
 * @param string $post_type   Post type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5331
 * @param bool   $full        Optional. Returns a full WHERE statement instead of just
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5332
 *                            an 'andalso' term. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5333
 * @param int    $post_author Optional. Query posts having a single author ID. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5334
 * @param bool   $public_only Optional. Only return public posts. Skips cap checks for
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5335
 *                            $current_user.  Default false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5336
 * @return string SQL WHERE code that can be added to a query.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5337
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5338
function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5339
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5340
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5341
	// Private posts.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5342
	$post_type_obj = get_post_type_object( $post_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5343
	if ( ! $post_type_obj )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5344
		return $full ? 'WHERE 1 = 0' : ' 1 = 0 ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5345
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5346
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5347
	 * Filter the capability to read private posts for a custom post type
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5348
	 * when generating SQL for getting posts by author.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5349
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5350
	 * @since 2.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5351
	 * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless".
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5352
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5353
	 * @param string $cap Capability.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5354
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5355
	if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5356
		$cap = $post_type_obj->cap->read_private_posts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5357
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5358
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5359
	$sql = $wpdb->prepare( 'post_type = %s', $post_type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5360
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5361
	if ( null !== $post_author ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5362
		$sql .= $wpdb->prepare( ' AND post_author = %d', $post_author );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5363
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5364
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5365
	// Only need to check the cap if $public_only is false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5366
	$post_status_sql = "post_status = 'publish'";
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5367
	if ( false === $public_only ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5368
		if ( current_user_can( $cap ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5369
			// Does the user have the capability to view private posts? Guess so.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5370
			$post_status_sql .= " OR post_status = 'private'";
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5371
		} elseif ( is_user_logged_in() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5372
			// Users can view their own private posts.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5373
			$id = get_current_user_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5374
			if ( null === $post_author || ! $full ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5375
				$post_status_sql .= " OR post_status = 'private' AND post_author = $id";
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5376
			} elseif ( $id == (int) $post_author ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5377
				$post_status_sql .= " OR post_status = 'private'";
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5378
			} // else none
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5379
		} // else none
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5380
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5381
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5382
	$sql .= " AND ($post_status_sql)";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5383
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5384
	if ( $full ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5385
		$sql = 'WHERE ' . $sql;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5386
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5387
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5388
	return $sql;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5389
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5390
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5391
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5392
 * Retrieve the date that the last post was published.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5393
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5394
 * The server timezone is the default and is the difference between GMT and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5395
 * server time. The 'blog' value is the date when the last post was posted. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5396
 * 'gmt' is when the last post was posted in GMT formatted date.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5397
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5398
 * @since 0.71
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5399
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5400
 * @param string $timezone The location to get the time. Accepts 'gmt', 'blog',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5401
 *                         or 'server'. Default 'server'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5402
 * @return string The date of the last post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5403
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5404
function get_lastpostdate( $timezone = 'server' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5405
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5406
	 * Filter the date the last post was published.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5407
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5408
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5409
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5410
	 * @param string $date     Date the last post was published. Likely values are 'gmt',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5411
	 *                         'blog', or 'server'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5412
	 * @param string $timezone Location to use for getting the post published date.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5413
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5414
	return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date' ), $timezone );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5415
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5416
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5417
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5418
 * Get the timestamp of the last time any post was modified.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5419
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5420
 * The server timezone is the default and is the difference between GMT and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5421
 * server time. The 'blog' value is just when the last post was modified. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5422
 * 'gmt' is when the last post was modified in GMT time.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5423
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5424
 * @since 1.2.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5425
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5426
 * @param string $timezone Optional. The timezone for the timestamp. Uses the server's internal timezone.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5427
 *                         Accepts 'server', 'blog', 'gmt'. or 'server'. 'server' uses the server's
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5428
 *                         internal timezone. 'blog' uses the `post_modified` field, which proxies
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5429
 *                         to the timezone set for the site. 'gmt' uses the `post_modified_gmt` field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5430
 *                         Default 'server'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5431
 * @return string The timestamp.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5432
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5433
function get_lastpostmodified( $timezone = 'server' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5434
	$lastpostmodified = _get_last_post_time( $timezone, 'modified' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5435
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5436
	$lastpostdate = get_lastpostdate($timezone);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5437
	if ( $lastpostdate > $lastpostmodified )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5438
		$lastpostmodified = $lastpostdate;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5439
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5440
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5441
	 * Filter the date the last post was modified.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5442
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5443
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5444
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5445
	 * @param string $lastpostmodified Date the last post was modified.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5446
	 * @param string $timezone         Location to use for getting the post modified date.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5447
	 *                                 See {@see get_lastpostmodified()} for accepted `$timezone` values.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5448
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5449
	return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5450
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5451
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5452
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5453
 * Get the timestamp of the last time any post was modified or published.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5454
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5455
 * @since 3.1.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5456
 * @access private
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5457
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5458
 * @param string $timezone The timezone for the timestamp. See {@see get_lastpostmodified()}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5459
 *                         for information on accepted values.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5460
 * @param string $field    Post field to check. Accepts 'date' or 'modified'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5461
 * @return string The timestamp.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5462
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5463
function _get_last_post_time( $timezone, $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5464
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5465
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5466
	if ( !in_array( $field, array( 'date', 'modified' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5467
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5468
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5469
	$timezone = strtolower( $timezone );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5470
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5471
	$key = "lastpost{$field}:$timezone";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5472
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5473
	$date = wp_cache_get( $key, 'timeinfo' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5474
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5475
	if ( !$date ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5476
		$add_seconds_server = date('Z');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5477
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5478
		$post_types = get_post_types( array( 'public' => true ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5479
		array_walk( $post_types, array( &$wpdb, 'escape_by_ref' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5480
		$post_types = "'" . implode( "', '", $post_types ) . "'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5481
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5482
		switch ( $timezone ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5483
			case 'gmt':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5484
				$date = $wpdb->get_var("SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5485
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5486
			case 'blog':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5487
				$date = $wpdb->get_var("SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5488
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5489
			case 'server':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5490
				$date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5491
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5492
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5493
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5494
		if ( $date )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5495
			wp_cache_set( $key, $date, 'timeinfo' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5496
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5497
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5498
	return $date;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5499
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5500
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5501
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5502
 * Updates posts in cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5503
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5504
 * @since 1.5.1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5505
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5506
 * @param array $posts Array of post objects, passed by reference.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5507
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5508
function update_post_cache( &$posts ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5509
	if ( ! $posts )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5510
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5511
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5512
	foreach ( $posts as $post )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5513
		wp_cache_add( $post->ID, $post, 'posts' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5514
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5515
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5516
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5517
 * Will clean the post in the cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5518
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5519
 * Cleaning means delete from the cache of the post. Will call to clean the term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5520
 * object cache associated with the post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5521
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5522
 * This function not run if $_wp_suspend_cache_invalidation is not empty. See
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5523
 * wp_suspend_cache_invalidation().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5524
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5525
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5526
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5527
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5528
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5529
 * @param int|WP_Post $post Post ID or post object to remove from the cache.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5530
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5531
function clean_post_cache( $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5532
	global $_wp_suspend_cache_invalidation, $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5533
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5534
	if ( ! empty( $_wp_suspend_cache_invalidation ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5535
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5536
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5537
	$post = get_post( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5538
	if ( empty( $post ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5539
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5540
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5541
	wp_cache_delete( $post->ID, 'posts' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5542
	wp_cache_delete( $post->ID, 'post_meta' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5543
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5544
	clean_object_term_cache( $post->ID, $post->post_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5545
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5546
	wp_cache_delete( 'wp_get_archives', 'general' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5547
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5548
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5549
	 * Fires immediately after the given post's cache is cleaned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5550
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5551
	 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5552
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5553
	 * @param int     $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5554
	 * @param WP_Post $post    Post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5555
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5556
	do_action( 'clean_post_cache', $post->ID, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5557
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5558
	if ( 'page' == $post->post_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5559
		wp_cache_delete( 'all_page_ids', 'posts' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5560
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5561
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5562
		 * Fires immediately after the given page's cache is cleaned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5563
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5564
		 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5565
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5566
		 * @param int $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5567
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5568
		do_action( 'clean_page_cache', $post->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5569
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5570
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5571
	wp_cache_set( 'last_changed', microtime(), 'posts' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5572
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5573
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5574
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5575
 * Call major cache updating functions for list of Post objects.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5576
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5577
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5578
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5579
 * @param array  $posts             Array of Post objects
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5580
 * @param string $post_type         Optional. Post type. Default 'post'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5581
 * @param bool   $update_term_cache Optional. Whether to update the term cache. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5582
 * @param bool   $update_meta_cache Optional. Whether to update the meta cache. Default true.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5583
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5584
function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5585
	// No point in doing all this work if we didn't match any posts.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5586
	if ( !$posts )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5587
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5588
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5589
	update_post_cache($posts);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5590
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5591
	$post_ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5592
	foreach ( $posts as $post )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5593
		$post_ids[] = $post->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5594
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5595
	if ( ! $post_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5596
		$post_type = 'any';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5597
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5598
	if ( $update_term_cache ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5599
		if ( is_array($post_type) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5600
			$ptypes = $post_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5601
		} elseif ( 'any' == $post_type ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5602
			$ptypes = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5603
			// Just use the post_types in the supplied posts.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5604
			foreach ( $posts as $post ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5605
				$ptypes[] = $post->post_type;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5606
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5607
			$ptypes = array_unique($ptypes);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5608
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5609
			$ptypes = array($post_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5610
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5611
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5612
		if ( ! empty($ptypes) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5613
			update_object_term_cache($post_ids, $ptypes);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5614
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5615
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5616
	if ( $update_meta_cache )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5617
		update_postmeta_cache($post_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5618
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5619
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5620
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5621
 * Updates metadata cache for list of post IDs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5622
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5623
 * Performs SQL query to retrieve the metadata for the post IDs and updates the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5624
 * metadata cache for the posts. Therefore, the functions, which call this
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5625
 * function, do not need to perform SQL queries on their own.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5626
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5627
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5628
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5629
 * @param array $post_ids List of post IDs.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5630
 * @return bool|array Returns false if there is nothing to update or an array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5631
 *                    of metadata.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5632
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5633
function update_postmeta_cache( $post_ids ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5634
	return update_meta_cache('post', $post_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5635
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5636
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5637
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5638
 * Will clean the attachment in the cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5639
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5640
 * Cleaning means delete from the cache. Optionally will clean the term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5641
 * object cache associated with the attachment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5642
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5643
 * This function will not run if $_wp_suspend_cache_invalidation is not empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5644
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5645
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5646
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5647
 * @see wp_suspend_cache_invalidation()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5648
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5649
 * @param int  $id          The attachment ID in the cache to clean.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5650
 * @param bool $clean_terms Optional. Whether to clean terms cache. Default false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5651
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5652
function clean_attachment_cache( $id, $clean_terms = false ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5653
	global $_wp_suspend_cache_invalidation;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5654
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5655
	if ( !empty($_wp_suspend_cache_invalidation) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5656
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5657
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5658
	$id = (int) $id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5659
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5660
	wp_cache_delete($id, 'posts');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5661
	wp_cache_delete($id, 'post_meta');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5662
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5663
	if ( $clean_terms )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5664
		clean_object_term_cache($id, 'attachment');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5665
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5666
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5667
	 * Fires after the given attachment's cache is cleaned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5668
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5669
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5670
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5671
	 * @param int $id Attachment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5672
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5673
	do_action( 'clean_attachment_cache', $id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5674
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5675
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5676
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5677
// Hooks
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5678
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5679
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5680
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5681
 * Hook for managing future post transitions to published.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5682
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5683
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5684
 * @access private
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5685
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5686
 * @see wp_clear_scheduled_hook()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5687
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5688
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5689
 * @param string  $new_status New post status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5690
 * @param string  $old_status Previous post status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5691
 * @param WP_Post $post       Post object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5692
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5693
function _transition_post_status( $new_status, $old_status, $post ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5694
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5695
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5696
	if ( $old_status != 'publish' && $new_status == 'publish' ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5697
		// Reset GUID if transitioning to publish and it is empty.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5698
		if ( '' == get_the_guid($post->ID) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5699
			$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5700
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5701
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5702
		 * Fires when a post's status is transitioned from private to published.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5703
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5704
		 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5705
		 * @deprecated 2.3.0 Use 'private_to_publish' instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5706
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5707
		 * @param int $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5708
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5709
		do_action('private_to_published', $post->ID);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5710
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5711
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5712
	// If published posts changed clear the lastpostmodified cache.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5713
	if ( 'publish' == $new_status || 'publish' == $old_status) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5714
		foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5715
			wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5716
			wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5717
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5718
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5719
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5720
	if ( $new_status !== $old_status ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5721
		wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5722
		wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5723
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5724
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5725
	// Always clears the hook in case the post status bounced from future to draft.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5726
	wp_clear_scheduled_hook('publish_future_post', array( $post->ID ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5727
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5728
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5729
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5730
 * Hook used to schedule publication for a post marked for the future.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5731
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5732
 * The $post properties used and must exist are 'ID' and 'post_date_gmt'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5733
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5734
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5735
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5736
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5737
 * @param int     $deprecated Not used. Can be set to null. Never implemented. Not marked
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5738
 *                            as deprecated with _deprecated_argument() as it conflicts with
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5739
 *                            wp_transition_post_status() and the default filter for
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5740
 *                            {@see _future_post_hook()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5741
 * @param WP_Post $post       Post object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5742
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5743
function _future_post_hook( $deprecated, $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5744
	wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5745
	wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT') , 'publish_future_post', array( $post->ID ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5746
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5747
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5748
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5749
 * Hook to schedule pings and enclosures when a post is published.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5750
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5751
 * Uses XMLRPC_REQUEST and WP_IMPORTING constants.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5752
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5753
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5754
 * @access private
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5755
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5756
 * @param int $post_id The ID in the database table of the post being published.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5757
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5758
function _publish_post_hook( $post_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5759
	if ( defined( 'XMLRPC_REQUEST' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5760
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5761
		 * Fires when _publish_post_hook() is called during an XML-RPC request.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5762
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5763
		 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5764
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5765
		 * @param int $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5766
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5767
		do_action( 'xmlrpc_publish_post', $post_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5768
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5769
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5770
	if ( defined('WP_IMPORTING') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5771
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5772
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5773
	if ( get_option('default_pingback_flag') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5774
		add_post_meta( $post_id, '_pingme', '1' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5775
	add_post_meta( $post_id, '_encloseme', '1' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5776
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5777
	wp_schedule_single_event(time(), 'do_pings');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5778
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5779
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5780
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5781
 * Return the post's parent's post_ID
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5782
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5783
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5784
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5785
 * @param int $post_ID
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5786
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5787
 * @return int|bool Post parent ID, otherwise false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5788
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5789
function wp_get_post_parent_id( $post_ID ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5790
	$post = get_post( $post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5791
	if ( !$post || is_wp_error( $post ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5792
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5793
	return (int) $post->post_parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5794
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5795
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5796
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5797
 * Check the given subset of the post hierarchy for hierarchy loops.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5798
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5799
 * Prevents loops from forming and breaks those that it finds. Attached
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5800
 * to the 'wp_insert_post_parent' filter.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5801
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5802
 * @since 3.1.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5803
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5804
 * @see wp_find_hierarchy_loop()
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5805
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5806
 * @param int $post_parent ID of the parent for the post we're checking.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5807
 * @param int $post_ID     ID of the post we're checking.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5808
 * @return int The new post_parent for the post, 0 otherwise.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5809
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5810
function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5811
	// Nothing fancy here - bail.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5812
	if ( !$post_parent )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5813
		return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5814
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5815
	// New post can't cause a loop.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5816
	if ( empty( $post_ID ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5817
		return $post_parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5818
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5819
	// Can't be its own parent.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5820
	if ( $post_parent == $post_ID )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5821
		return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5822
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5823
	// Now look for larger loops.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5824
	if ( !$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5825
		return $post_parent; // No loop
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5826
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5827
	// Setting $post_parent to the given value causes a loop.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5828
	if ( isset( $loop[$post_ID] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5829
		return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5830
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5831
	// There's a loop, but it doesn't contain $post_ID. Break the loop.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5832
	foreach ( array_keys( $loop ) as $loop_member )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5833
		wp_update_post( array( 'ID' => $loop_member, 'post_parent' => 0 ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5834
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5835
	return $post_parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5836
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5837
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5838
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5839
 * Set a post thumbnail.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5840
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5841
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5842
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5843
 * @param int|WP_Post $post         Post ID or post object where thumbnail should be attached.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5844
 * @param int         $thumbnail_id Thumbnail to attach.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5845
 * @return bool True on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5846
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5847
function set_post_thumbnail( $post, $thumbnail_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5848
	$post = get_post( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5849
	$thumbnail_id = absint( $thumbnail_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5850
	if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5851
		if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5852
			return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5853
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5854
			return delete_post_meta( $post->ID, '_thumbnail_id' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5855
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5856
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5857
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5858
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5859
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5860
 * Remove a post thumbnail.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5861
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5862
 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5863
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5864
 * @param int|WP_Post $post Post ID or post object where thumbnail should be removed from.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5865
 * @return bool True on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5866
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5867
function delete_post_thumbnail( $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5868
	$post = get_post( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5869
	if ( $post )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5870
		return delete_post_meta( $post->ID, '_thumbnail_id' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5871
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5872
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5873
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5874
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5875
 * Delete auto-drafts for new posts that are > 7 days old.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5876
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5877
 * @since 3.4.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5878
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5879
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5880
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5881
function wp_delete_auto_drafts() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5882
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5883
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5884
	// Cleanup old auto-drafts more than 7 days old.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5885
	$old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5886
	foreach ( (array) $old_posts as $delete ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5887
		// Force delete.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5888
		wp_delete_post( $delete, true );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5889
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5890
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5891
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5892
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5893
 * Update the custom taxonomies' term counts when a post's status is changed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5894
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5895
 * For example, default posts term counts (for custom taxonomies) don't include
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5896
 * private / draft posts.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5897
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5898
 * @since 3.3.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5899
 * @access private
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5900
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5901
 * @param string  $new_status New post status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5902
 * @param string  $old_status Old post status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5903
 * @param WP_Post $post       Post object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5904
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5905
function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5906
	// Update counts for the post's terms.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5907
	foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5908
		$tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5909
		wp_update_term_count( $tt_ids, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5910
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5911
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5912
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5913
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5914
 * Adds any posts from the given ids to the cache that do not already exist in cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5915
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5916
 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5917
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5918
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5919
 * @see update_post_caches()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5920
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5921
 * @param array $ids               ID list
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5922
 * @param bool  $update_term_cache Optional. Whether to update the term cache. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  5923
 * @param bool  $update_meta_cache Optional. Whether to update the meta cache. Default true.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5924
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5925
function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5926
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5927
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5928
	$non_cached_ids = _get_non_cached_ids( $ids, 'posts' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5929
	if ( !empty( $non_cached_ids ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5930
		$fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", join( ",", $non_cached_ids ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5931
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5932
		update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5933
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  5934
}