Code Duplication    Length = 81-81 lines in 2 locations

json-endpoints/class.wpcom-json-api-update-post-endpoint.php 1 location

@@ 41-121 (lines=81) @@
38
			add_action( 'rest_api_inserted_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ) );
39
		}
40
41
		if ( $new ) {
42
			$input = $this->input( true );
43
44
			if ( 'revision' === $input['type'] ) {
45
				if ( ! isset( $input['parent'] ) ) {
46
					return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
47
				}
48
				$input['status'] = 'inherit'; // force inherit for revision type
49
				$input['slug'] = $input['parent'] . '-autosave-v1';
50
			}
51
			elseif ( !isset( $input['title'] ) && !isset( $input['content'] ) && !isset( $input['excerpt'] ) ) {
52
				return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
53
			}
54
55
			// default to post
56
			if ( empty( $input['type'] ) )
57
				$input['type'] = 'post';
58
59
			$post_type = get_post_type_object( $input['type'] );
60
61
			if ( ! $this->is_post_type_allowed( $input['type'] ) ) {
62
				return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 );
63
			}
64
65
			if ( ! empty( $input['author'] ) ) {
66
				$author_id = $this->parse_and_set_author( $input['author'], $input['type'] );
67
				unset( $input['author'] );
68
				if ( is_wp_error( $author_id ) )
69
					return $author_id;
70
			}
71
72
			if ( 'publish' === $input['status'] ) {
73
				if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
74
					if ( current_user_can( $post_type->cap->edit_posts ) ) {
75
						$input['status'] = 'pending';
76
					} else {
77
						return new WP_Error( 'unauthorized', 'User cannot publish posts', 403 );
78
					}
79
				}
80
			} else {
81
				if ( !current_user_can( $post_type->cap->edit_posts ) ) {
82
					return new WP_Error( 'unauthorized', 'User cannot edit posts', 403 );
83
				}
84
			}
85
		} else {
86
			$input = $this->input( false );
87
88
			if ( !is_array( $input ) || !$input ) {
89
				return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
90
			}
91
92
			$post = get_post( $post_id );
93
			$_post_type = ( ! empty( $input['type'] ) ) ? $input['type'] : $post->post_type;
94
			$post_type = get_post_type_object( $_post_type );
95
			if ( !$post || is_wp_error( $post ) ) {
96
				return new WP_Error( 'unknown_post', 'Unknown post', 404 );
97
			}
98
99
			if ( !current_user_can( 'edit_post', $post->ID ) ) {
100
				return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
101
			}
102
103
			if ( ! empty( $input['author'] ) ) {
104
				$author_id = $this->parse_and_set_author( $input['author'], $_post_type );
105
				unset( $input['author'] );
106
				if ( is_wp_error( $author_id ) )
107
					return $author_id;
108
			}
109
110
			if ( ( isset( $input['status'] ) && 'publish' === $input['status'] ) && 'publish' !== $post->post_status && !current_user_can( 'publish_post', $post->ID ) ) {
111
				$input['status'] = 'pending';
112
			}
113
			$last_status = $post->post_status;
114
			$new_status = isset( $input['status'] ) ? $input['status'] : $last_status;
115
116
			// Make sure that drafts get the current date when transitioning to publish if not supplied in the post.
117
			$date_in_past = ( strtotime($post->post_date_gmt) < time() );
118
			if ( 'publish' === $new_status && 'draft' === $last_status && ! isset( $input['date_gmt'] ) && $date_in_past ) {
119
				$input['date_gmt'] = gmdate( 'Y-m-d H:i:s' );
120
			}
121
		}
122
123
		// If date is set, $this->input will set date_gmt, date still needs to be adjusted for the blog's offset
124
		if ( isset( $input['date_gmt'] ) ) {

json-endpoints/class.wpcom-json-api-update-post-v1-1-endpoint.php 1 location

@@ 46-126 (lines=81) @@
43
			$input['status'] = 'publish';
44
		}
45
46
		if ( $new ) {
47
			$input = $this->input( true );
48
49
			if ( 'revision' === $input['type'] ) {
50
				if ( ! isset( $input['parent'] ) ) {
51
					return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
52
				}
53
				$input['status'] = 'inherit'; // force inherit for revision type
54
				$input['slug'] = $input['parent'] . '-autosave-v1';
55
			}
56
			elseif ( !isset( $input['title'] ) && !isset( $input['content'] ) && !isset( $input['excerpt'] ) ) {
57
				return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
58
			}
59
60
			// default to post
61
			if ( empty( $input['type'] ) )
62
				$input['type'] = 'post';
63
64
			$post_type = get_post_type_object( $input['type'] );
65
66
			if ( ! $this->is_post_type_allowed( $input['type'] ) ) {
67
				return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 );
68
			}
69
70
			if ( ! empty( $input['author'] ) ) {
71
				$author_id = $this->parse_and_set_author( $input['author'], $input['type'] );
72
				unset( $input['author'] );
73
				if ( is_wp_error( $author_id ) )
74
					return $author_id;
75
			}
76
77
			if ( 'publish' === $input['status'] ) {
78
				if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
79
					if ( current_user_can( $post_type->cap->edit_posts ) ) {
80
						$input['status'] = 'pending';
81
					} else {
82
						return new WP_Error( 'unauthorized', 'User cannot publish posts', 403 );
83
					}
84
				}
85
			} else {
86
				if ( !current_user_can( $post_type->cap->edit_posts ) ) {
87
					return new WP_Error( 'unauthorized', 'User cannot edit posts', 403 );
88
				}
89
			}
90
		} else {
91
			$input = $this->input( false );
92
93
			if ( !is_array( $input ) || !$input ) {
94
				return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
95
			}
96
97
			$post = get_post( $post_id );
98
			$_post_type = ( ! empty( $input['type'] ) ) ? $input['type'] : $post->post_type;
99
			$post_type = get_post_type_object( $_post_type );
100
			if ( !$post || is_wp_error( $post ) ) {
101
				return new WP_Error( 'unknown_post', 'Unknown post', 404 );
102
			}
103
104
			if ( !current_user_can( 'edit_post', $post->ID ) ) {
105
				return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
106
			}
107
108
			if ( ! empty( $input['author'] ) ) {
109
				$author_id = $this->parse_and_set_author( $input['author'], $_post_type );
110
				unset( $input['author'] );
111
				if ( is_wp_error( $author_id ) )
112
					return $author_id;
113
			}
114
115
			if ( ( isset( $input['status'] ) && 'publish' === $input['status'] ) && 'publish' !== $post->post_status && !current_user_can( 'publish_post', $post->ID ) ) {
116
				$input['status'] = 'pending';
117
			}
118
			$last_status = $post->post_status;
119
			$new_status = isset( $input['status'] ) ? $input['status'] : $last_status;
120
121
			// Make sure that drafts get the current date when transitioning to publish if not supplied in the post.
122
			$date_in_past = ( strtotime($post->post_date_gmt) < time() );
123
			if ( 'publish' === $new_status && 'draft' === $last_status && ! isset( $input['date_gmt'] ) && $date_in_past ) {
124
				$input['date_gmt'] = gmdate( 'Y-m-d H:i:s' );
125
			}
126
		}
127
128
		// If date is set, $this->input will set date_gmt, date still needs to be adjusted for the blog's offset
129
		if ( isset( $input['date_gmt'] ) ) {