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