Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | class WPCOM_JSON_API_Update_Post_Endpoint extends WPCOM_JSON_API_Post_Endpoint { |
||
| 3 | function __construct( $args ) { |
||
| 4 | parent::__construct( $args ); |
||
| 5 | if ( $this->api->ends_with( $this->path, '/delete' ) ) { |
||
| 6 | $this->post_object_format['status']['deleted'] = 'The post has been deleted permanently.'; |
||
| 7 | } |
||
| 8 | } |
||
| 9 | |||
| 10 | // /sites/%s/posts/new -> $blog_id |
||
| 11 | // /sites/%s/posts/%d -> $blog_id, $post_id |
||
| 12 | // /sites/%s/posts/%d/delete -> $blog_id, $post_id |
||
| 13 | // /sites/%s/posts/%d/restore -> $blog_id, $post_id |
||
| 14 | View Code Duplication | function callback( $path = '', $blog_id = 0, $post_id = 0 ) { |
|
| 15 | $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
||
| 16 | if ( is_wp_error( $blog_id ) ) { |
||
| 17 | return $blog_id; |
||
| 18 | } |
||
| 19 | |||
| 20 | if ( $this->api->ends_with( $path, '/delete' ) ) { |
||
| 21 | return $this->delete_post( $path, $blog_id, $post_id ); |
||
| 22 | } elseif ( $this->api->ends_with( $path, '/restore' ) ) { |
||
| 23 | return $this->restore_post( $path, $blog_id, $post_id ); |
||
| 24 | } else { |
||
| 25 | return $this->write_post( $path, $blog_id, $post_id ); |
||
| 26 | } |
||
| 27 | } |
||
| 28 | |||
| 29 | // /sites/%s/posts/new -> $blog_id |
||
| 30 | // /sites/%s/posts/%d -> $blog_id, $post_id |
||
| 31 | function write_post( $path, $blog_id, $post_id ) { |
||
| 32 | $new = $this->api->ends_with( $path, '/new' ); |
||
| 33 | $args = $this->query_args(); |
||
| 34 | |||
| 35 | // unhook publicize, it's hooked again later -- without this, skipping services is impossible |
||
| 36 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
| 37 | remove_action( 'save_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ), 100, 2 ); |
||
| 38 | add_action( 'rest_api_inserted_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ) ); |
||
| 39 | } |
||
| 40 | |||
| 41 | View Code Duplication | 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 | View Code Duplication | if ( isset( $input['date_gmt'] ) ) { |
|
| 125 | $gmt_offset = get_option( 'gmt_offset' ); |
||
| 126 | $time_with_offset = strtotime( $input['date_gmt'] ) + $gmt_offset * HOUR_IN_SECONDS; |
||
| 127 | $input['date'] = date( 'Y-m-d H:i:s', $time_with_offset ); |
||
| 128 | } |
||
| 129 | |||
| 130 | View Code Duplication | if ( ! empty( $author_id ) && get_current_user_id() != $author_id ) { |
|
| 131 | if ( ! current_user_can( $post_type->cap->edit_others_posts ) ) { |
||
| 132 | return new WP_Error( 'unauthorized', "User is not allowed to publish others' posts.", 403 ); |
||
| 133 | } elseif ( ! user_can( $author_id, $post_type->cap->edit_posts ) ) { |
||
| 134 | return new WP_Error( 'unauthorized', 'Assigned author cannot publish post.', 403 ); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | if ( !is_post_type_hierarchical( $post_type->name ) && 'revision' !== $post_type->name ) { |
||
| 139 | unset( $input['parent'] ); |
||
| 140 | } |
||
| 141 | |||
| 142 | $tax_input = array(); |
||
| 143 | |||
| 144 | View Code Duplication | foreach ( array( 'categories' => 'category', 'tags' => 'post_tag' ) as $key => $taxonomy ) { |
|
| 145 | if ( ! isset( $input[ $key ] ) ) { |
||
| 146 | continue; |
||
| 147 | } |
||
| 148 | |||
| 149 | $tax_input[ $taxonomy ] = array(); |
||
| 150 | |||
| 151 | $is_hierarchical = is_taxonomy_hierarchical( $taxonomy ); |
||
| 152 | |||
| 153 | if ( is_array( $input[$key] ) ) { |
||
| 154 | $terms = $input[$key]; |
||
| 155 | } else { |
||
| 156 | $terms = explode( ',', $input[$key] ); |
||
| 157 | } |
||
| 158 | |||
| 159 | foreach ( $terms as $term ) { |
||
| 160 | /** |
||
| 161 | * `curl --data 'category[]=123'` should be interpreted as a category ID, |
||
| 162 | * not a category whose name is '123'. |
||
| 163 | * |
||
| 164 | * Consequence: To add a category/tag whose name is '123', the client must |
||
| 165 | * first look up its ID. |
||
| 166 | */ |
||
| 167 | if ( ctype_digit( $term ) ) { |
||
| 168 | $term = (int) $term; |
||
| 169 | } |
||
| 170 | |||
| 171 | $term_info = term_exists( $term, $taxonomy ); |
||
| 172 | |||
| 173 | if ( ! $term_info ) { |
||
| 174 | // A term ID that doesn't already exist. Ignore it: we don't know what name to give it. |
||
| 175 | if ( is_int( $term ) ){ |
||
| 176 | continue; |
||
| 177 | } |
||
| 178 | // only add a new tag/cat if the user has access to |
||
| 179 | $tax = get_taxonomy( $taxonomy ); |
||
| 180 | |||
| 181 | // see https://core.trac.wordpress.org/ticket/26409 |
||
| 182 | if ( 'category' === $taxonomy && ! current_user_can( $tax->cap->edit_terms ) ) { |
||
| 183 | continue; |
||
| 184 | } else if ( ! current_user_can( $tax->cap->assign_terms ) ) { |
||
| 185 | continue; |
||
| 186 | } |
||
| 187 | |||
| 188 | $term_info = wp_insert_term( $term, $taxonomy ); |
||
| 189 | } |
||
| 190 | |||
| 191 | if ( ! is_wp_error( $term_info ) ) { |
||
| 192 | if ( $is_hierarchical ) { |
||
| 193 | // Categories must be added by ID |
||
| 194 | $tax_input[$taxonomy][] = (int) $term_info['term_id']; |
||
| 195 | } else { |
||
| 196 | // Tags must be added by name |
||
| 197 | if ( is_int( $term ) ) { |
||
| 198 | $term = get_term( $term, $taxonomy ); |
||
| 199 | $tax_input[$taxonomy][] = $term->name; |
||
| 200 | } else { |
||
| 201 | $tax_input[$taxonomy][] = $term; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | View Code Duplication | if ( isset( $input['categories'] ) && empty( $tax_input['category'] ) && 'revision' !== $post_type->name ) { |
|
| 209 | $tax_input['category'][] = get_option( 'default_category' ); |
||
| 210 | } |
||
| 211 | |||
| 212 | unset( $input['tags'], $input['categories'] ); |
||
| 213 | |||
| 214 | $insert = array(); |
||
| 215 | |||
| 216 | View Code Duplication | if ( !empty( $input['slug'] ) ) { |
|
| 217 | $insert['post_name'] = $input['slug']; |
||
| 218 | unset( $input['slug'] ); |
||
| 219 | } |
||
| 220 | |||
| 221 | if ( isset( $input['comments_open'] ) ) { |
||
| 222 | $insert['comment_status'] = ( true === $input['comments_open'] ) ? 'open' : 'closed'; |
||
| 223 | } |
||
| 224 | |||
| 225 | if ( isset( $input['pings_open'] ) ) { |
||
| 226 | $insert['ping_status'] = ( true === $input['pings_open'] ) ? 'open' : 'closed'; |
||
| 227 | } |
||
| 228 | |||
| 229 | unset( $input['comments_open'], $input['pings_open'] ); |
||
| 230 | |||
| 231 | View Code Duplication | if ( isset( $input['menu_order'] ) ) { |
|
| 232 | $insert['menu_order'] = $input['menu_order']; |
||
| 233 | unset( $input['menu_order'] ); |
||
| 234 | } |
||
| 235 | |||
| 236 | $publicize = isset( $input['publicize'] ) ? $input['publicize'] : null; |
||
| 237 | unset( $input['publicize'] ); |
||
| 238 | |||
| 239 | $publicize_custom_message = isset( $input['publicize_message'] ) ? $input['publicize_message'] : null; |
||
| 240 | unset( $input['publicize_message'] ); |
||
| 241 | |||
| 242 | View Code Duplication | if ( isset( $input['featured_image'] ) ) { |
|
| 243 | $featured_image = trim( $input['featured_image'] ); |
||
| 244 | $delete_featured_image = empty( $featured_image ); |
||
| 245 | unset( $input['featured_image'] ); |
||
| 246 | } |
||
| 247 | |||
| 248 | $metadata = isset( $input['metadata'] ) ? $input['metadata'] : null; |
||
| 249 | unset( $input['metadata'] ); |
||
| 250 | |||
| 251 | $likes = isset( $input['likes_enabled'] ) ? $input['likes_enabled'] : null; |
||
| 252 | unset( $input['likes_enabled'] ); |
||
| 253 | |||
| 254 | $sharing = isset( $input['sharing_enabled'] ) ? $input['sharing_enabled'] : null; |
||
| 255 | unset( $input['sharing_enabled'] ); |
||
| 256 | |||
| 257 | $sticky = isset( $input['sticky'] ) ? $input['sticky'] : null; |
||
| 258 | unset( $input['sticky'] ); |
||
| 259 | |||
| 260 | foreach ( $input as $key => $value ) { |
||
| 261 | $insert["post_$key"] = $value; |
||
| 262 | } |
||
| 263 | |||
| 264 | if ( ! empty( $author_id ) ) { |
||
| 265 | $insert['post_author'] = absint( $author_id ); |
||
| 266 | } |
||
| 267 | |||
| 268 | if ( ! empty( $tax_input ) ) { |
||
| 269 | $insert['tax_input'] = $tax_input; |
||
| 270 | } |
||
| 271 | |||
| 272 | $has_media = isset( $input['media'] ) && $input['media'] ? count( $input['media'] ) : false; |
||
| 273 | $has_media_by_url = isset( $input['media_urls'] ) && $input['media_urls'] ? count( $input['media_urls'] ) : false; |
||
| 274 | |||
| 275 | View Code Duplication | if ( $new ) { |
|
| 276 | |||
| 277 | if ( isset( $input['content'] ) && ! has_shortcode( $input['content'], 'gallery' ) && ( $has_media || $has_media_by_url ) ) { |
||
| 278 | switch ( ( $has_media + $has_media_by_url ) ) { |
||
| 279 | case 0 : |
||
| 280 | // No images - do nothing. |
||
| 281 | break; |
||
| 282 | case 1 : |
||
| 283 | // 1 image - make it big |
||
| 284 | $insert['post_content'] = $input['content'] = "[gallery size=full columns=1]\n\n" . $input['content']; |
||
| 285 | break; |
||
| 286 | default : |
||
| 287 | // Several images - 3 column gallery |
||
| 288 | $insert['post_content'] = $input['content'] = "[gallery]\n\n" . $input['content']; |
||
| 289 | break; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | $post_id = wp_insert_post( add_magic_quotes( $insert ), true ); |
||
| 294 | } else { |
||
| 295 | $insert['ID'] = $post->ID; |
||
| 296 | |||
| 297 | // wp_update_post ignores date unless edit_date is set |
||
| 298 | // See: http://codex.wordpress.org/Function_Reference/wp_update_post#Scheduling_posts |
||
| 299 | // See: https://core.trac.wordpress.org/browser/tags/3.9.2/src/wp-includes/post.php#L3302 |
||
| 300 | if ( isset( $input['date_gmt'] ) || isset( $input['date'] ) ) { |
||
| 301 | $insert['edit_date'] = true; |
||
| 302 | } |
||
| 303 | |||
| 304 | $post_id = wp_update_post( (object) $insert ); |
||
| 305 | |||
| 306 | } |
||
| 307 | |||
| 308 | if ( !$post_id || is_wp_error( $post_id ) ) { |
||
| 309 | return $post_id; |
||
| 310 | } |
||
| 311 | |||
| 312 | // make sure this post actually exists and is not an error of some kind (ie, trying to load media in the posts endpoint) |
||
| 313 | $post_check = $this->get_post_by( 'ID', $post_id, $args['context'] ); |
||
| 314 | if ( is_wp_error( $post_check ) ) { |
||
| 315 | return $post_check; |
||
| 316 | } |
||
| 317 | |||
| 318 | if ( $has_media ) { |
||
| 319 | $this->api->trap_wp_die( 'upload_error' ); |
||
| 320 | foreach ( $input['media'] as $media_item ) { |
||
| 321 | $_FILES['.api.media.item.'] = $media_item; |
||
| 322 | // check for WP_Error if we ever actually need $media_id |
||
| 323 | $media_id = media_handle_upload( '.api.media.item.', $post_id ); |
||
| 324 | } |
||
| 325 | $this->api->trap_wp_die( null ); |
||
| 326 | |||
| 327 | unset( $_FILES['.api.media.item.'] ); |
||
| 328 | } |
||
| 329 | |||
| 330 | if ( $has_media_by_url ) { |
||
| 331 | foreach ( $input['media_urls'] as $url ) { |
||
| 332 | $this->handle_media_sideload( $url, $post_id ); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | // Set like status for the post |
||
| 337 | /** This filter is documented in modules/likes.php */ |
||
| 338 | $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) ); |
||
| 339 | View Code Duplication | if ( $new ) { |
|
| 340 | if ( $sitewide_likes_enabled ) { |
||
| 341 | if ( false === $likes ) { |
||
| 342 | update_post_meta( $post_id, 'switch_like_status', 1 ); |
||
| 343 | } else { |
||
| 344 | delete_post_meta( $post_id, 'switch_like_status' ); |
||
| 345 | } |
||
| 346 | } else { |
||
| 347 | if ( $likes ) { |
||
| 348 | update_post_meta( $post_id, 'switch_like_status', 1 ); |
||
| 349 | } else { |
||
| 350 | delete_post_meta( $post_id, 'switch_like_status' ); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } else { |
||
| 354 | if ( isset( $likes ) ) { |
||
| 355 | if ( $sitewide_likes_enabled ) { |
||
| 356 | if ( false === $likes ) { |
||
| 357 | update_post_meta( $post_id, 'switch_like_status', 1 ); |
||
| 358 | } else { |
||
| 359 | delete_post_meta( $post_id, 'switch_like_status' ); |
||
| 360 | } |
||
| 361 | } else { |
||
| 362 | if ( true === $likes ) { |
||
| 363 | update_post_meta( $post_id, 'switch_like_status', 1 ); |
||
| 364 | } else { |
||
| 365 | delete_post_meta( $post_id, 'switch_like_status' ); |
||
| 366 | } |
||
| 367 | } |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | // Set sharing status of the post |
||
| 372 | View Code Duplication | if ( $new ) { |
|
| 373 | $sharing_enabled = isset( $sharing ) ? (bool) $sharing : true; |
||
| 374 | if ( false === $sharing_enabled ) { |
||
| 375 | update_post_meta( $post_id, 'sharing_disabled', 1 ); |
||
| 376 | } |
||
| 377 | } |
||
| 378 | else { |
||
| 379 | if ( isset( $sharing ) && true === $sharing ) { |
||
| 380 | delete_post_meta( $post_id, 'sharing_disabled' ); |
||
| 381 | } else if ( isset( $sharing ) && false == $sharing ) { |
||
| 382 | update_post_meta( $post_id, 'sharing_disabled', 1 ); |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | if ( isset( $sticky ) ) { |
||
| 387 | if ( true === $sticky ) { |
||
| 388 | stick_post( $post_id ); |
||
| 389 | } else { |
||
| 390 | unstick_post( $post_id ); |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | // WPCOM Specific (Jetpack's will get bumped elsewhere |
||
| 395 | // Tracks how many posts are published and sets meta |
||
| 396 | // so we can track some other cool stats (like likes & comments on posts published) |
||
| 397 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
| 398 | if ( |
||
| 399 | ( $new && 'publish' == $input['status'] ) |
||
| 400 | || ( |
||
| 401 | ! $new && isset( $last_status ) |
||
| 402 | && 'publish' != $last_status |
||
| 403 | && isset( $new_status ) |
||
| 404 | && 'publish' == $new_status |
||
| 405 | ) |
||
| 406 | ) { |
||
| 407 | /** This action is documented in modules/widgets/social-media-icons.php */ |
||
| 408 | do_action( 'jetpack_bump_stats_extras', 'api-insights-posts', $this->api->token_details['client_id'] ); |
||
| 409 | update_post_meta( $post_id, '_rest_api_published', 1 ); |
||
| 410 | update_post_meta( $post_id, '_rest_api_client_id', $this->api->token_details['client_id'] ); |
||
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | |||
| 415 | // We ask the user/dev to pass Publicize services he/she wants activated for the post, but Publicize expects us |
||
| 416 | // to instead flag the ones we don't want to be skipped. proceed with said logic. |
||
| 417 | // any posts coming from Path (client ID 25952) should also not publicize |
||
| 418 | View Code Duplication | if ( $publicize === false || ( isset( $this->api->token_details['client_id'] ) && 25952 == $this->api->token_details['client_id'] ) ) { |
|
| 419 | // No publicize at all, skip all by ID |
||
| 420 | foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) { |
||
| 421 | delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name ); |
||
| 422 | $service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name ); |
||
| 423 | if ( ! $service_connections ) { |
||
| 424 | continue; |
||
| 425 | } |
||
| 426 | foreach ( $service_connections as $service_connection ) { |
||
| 427 | update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 ); |
||
| 428 | } |
||
| 429 | } |
||
| 430 | } else if ( is_array( $publicize ) && ( count ( $publicize ) > 0 ) ) { |
||
| 431 | foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) { |
||
| 432 | /* |
||
| 433 | * We support both indexed and associative arrays: |
||
| 434 | * * indexed are to pass entire services |
||
| 435 | * * associative are to pass specific connections per service |
||
| 436 | * |
||
| 437 | * We do support mixed arrays: mixed integer and string keys (see 3rd example below). |
||
| 438 | * |
||
| 439 | * EG: array( 'twitter', 'facebook') will only publicize to those, ignoring the other available services |
||
| 440 | * Form data: publicize[]=twitter&publicize[]=facebook |
||
| 441 | * EG: array( 'twitter' => '(int) $pub_conn_id_0, (int) $pub_conn_id_3', 'facebook' => (int) $pub_conn_id_7 ) will publicize to two Twitter accounts, and one Facebook connection, of potentially many. |
||
| 442 | * Form data: publicize[twitter]=$pub_conn_id_0,$pub_conn_id_3&publicize[facebook]=$pub_conn_id_7 |
||
| 443 | * EG: array( 'twitter', 'facebook' => '(int) $pub_conn_id_0, (int) $pub_conn_id_3' ) will publicize to all available Twitter accounts, but only 2 of potentially many Facebook connections |
||
| 444 | * Form data: publicize[]=twitter&publicize[facebook]=$pub_conn_id_0,$pub_conn_id_3 |
||
| 445 | */ |
||
| 446 | |||
| 447 | // Delete any stale SKIP value for the service by name. We'll add it back by ID. |
||
| 448 | delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name ); |
||
| 449 | |||
| 450 | // Get the user's connections |
||
| 451 | $service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name ); |
||
| 452 | |||
| 453 | // if the user doesn't have any connections for this service, move on |
||
| 454 | if ( ! $service_connections ) { |
||
| 455 | continue; |
||
| 456 | } |
||
| 457 | |||
| 458 | if ( !in_array( $name, $publicize ) && !array_key_exists( $name, $publicize ) ) { |
||
| 459 | // Skip the whole service by adding each connection ID |
||
| 460 | foreach ( $service_connections as $service_connection ) { |
||
| 461 | update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 ); |
||
| 462 | } |
||
| 463 | } else if ( !empty( $publicize[ $name ] ) ) { |
||
| 464 | // Seems we're being asked to only push to [a] specific connection[s]. |
||
| 465 | // Explode the list on commas, which will also support a single passed ID |
||
| 466 | $requested_connections = explode( ',', ( preg_replace( '/[\s]*/', '', $publicize[ $name ] ) ) ); |
||
| 467 | // Flag the connections we can't match with the requested list to be skipped. |
||
| 468 | foreach ( $service_connections as $service_connection ) { |
||
| 469 | if ( !in_array( $service_connection->meta['connection_data']->id, $requested_connections ) ) { |
||
| 470 | update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 ); |
||
| 471 | } else { |
||
| 472 | delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id ); |
||
| 473 | } |
||
| 474 | } |
||
| 475 | } else { |
||
| 476 | // delete all SKIP values; it's okay to publish to all connected IDs for this service |
||
| 477 | foreach ( $service_connections as $service_connection ) { |
||
| 478 | delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id ); |
||
| 479 | } |
||
| 480 | } |
||
| 481 | } |
||
| 482 | } |
||
| 483 | |||
| 484 | View Code Duplication | if ( ! is_null( $publicize_custom_message ) ) { |
|
| 485 | if ( empty( $publicize_custom_message ) ) { |
||
| 486 | delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_MESS ); |
||
| 487 | } else { |
||
| 488 | update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_MESS, trim( $publicize_custom_message ) ); |
||
| 489 | } |
||
| 490 | } |
||
| 491 | |||
| 492 | View Code Duplication | if ( ! empty( $insert['post_format'] ) ) { |
|
| 493 | if ( 'default' !== strtolower( $insert['post_format'] ) ) { |
||
| 494 | set_post_format( $post_id, $insert['post_format'] ); |
||
| 495 | } |
||
| 496 | else { |
||
| 497 | set_post_format( $post_id, get_option( 'default_post_format' ) ); |
||
| 498 | } |
||
| 499 | } |
||
| 500 | |||
| 501 | if ( isset( $featured_image ) ) { |
||
| 502 | $this->parse_and_set_featured_image( $post_id, $delete_featured_image, $featured_image ); |
||
| 503 | } |
||
| 504 | |||
| 505 | View Code Duplication | if ( ! empty( $metadata ) ) { |
|
| 506 | foreach ( (array) $metadata as $meta ) { |
||
| 507 | |||
| 508 | $meta = (object) $meta; |
||
| 509 | |||
| 510 | $existing_meta_item = new stdClass; |
||
| 511 | |||
| 512 | if ( empty( $meta->operation ) ) |
||
| 513 | $meta->operation = 'update'; |
||
| 514 | |||
| 515 | if ( ! empty( $meta->value ) ) { |
||
| 516 | if ( 'true' == $meta->value ) |
||
| 517 | $meta->value = true; |
||
| 518 | if ( 'false' == $meta->value ) |
||
| 519 | $meta->value = false; |
||
| 520 | } |
||
| 521 | |||
| 522 | if ( ! empty( $meta->id ) ) { |
||
| 523 | $meta->id = absint( $meta->id ); |
||
| 524 | $existing_meta_item = get_metadata_by_mid( 'post', $meta->id ); |
||
| 525 | } |
||
| 526 | |||
| 527 | $unslashed_meta_key = wp_unslash( $meta->key ); // should match what the final key will be |
||
| 528 | $meta->key = wp_slash( $meta->key ); |
||
| 529 | $unslashed_existing_meta_key = wp_unslash( $existing_meta_item->meta_key ); |
||
| 530 | $existing_meta_item->meta_key = wp_slash( $existing_meta_item->meta_key ); |
||
| 531 | |||
| 532 | // make sure that the meta id passed matches the existing meta key |
||
| 533 | if ( ! empty( $meta->id ) && ! empty( $meta->key ) ) { |
||
| 534 | $meta_by_id = get_metadata_by_mid( 'post', $meta->id ); |
||
| 535 | if ( $meta_by_id->meta_key !== $meta->key ) { |
||
| 536 | continue; // skip this meta |
||
| 537 | } |
||
| 538 | } |
||
| 539 | |||
| 540 | switch ( $meta->operation ) { |
||
| 541 | case 'delete': |
||
|
0 ignored issues
–
show
|
|||
| 542 | |||
| 543 | if ( ! empty( $meta->id ) && ! empty( $existing_meta_item->meta_key ) && current_user_can( 'delete_post_meta', $post_id, $unslashed_existing_meta_key ) ) { |
||
| 544 | delete_metadata_by_mid( 'post', $meta->id ); |
||
| 545 | } elseif ( ! empty( $meta->key ) && ! empty( $meta->previous_value ) && current_user_can( 'delete_post_meta', $post_id, $unslashed_meta_key ) ) { |
||
| 546 | delete_post_meta( $post_id, $meta->key, $meta->previous_value ); |
||
| 547 | } elseif ( ! empty( $meta->key ) && current_user_can( 'delete_post_meta', $post_id, $unslashed_meta_key ) ) { |
||
| 548 | delete_post_meta( $post_id, $meta->key ); |
||
| 549 | } |
||
| 550 | |||
| 551 | break; |
||
| 552 | case 'add': |
||
|
0 ignored issues
–
show
The case body in a switch statement must start on the line following the statement.
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement. switch ($expr) {
case "A":
doSomething(); //right
break;
case "B":
doSomethingElse(); //wrong
break;
} To learn more about the PSR-2 coding standard, please refer to the PHP-Fig. Loading history...
|
|||
| 553 | |||
| 554 | if ( ! empty( $meta->id ) || ! empty( $meta->previous_value ) ) { |
||
| 555 | continue; |
||
| 556 | } elseif ( ! empty( $meta->key ) && ! empty( $meta->value ) && ( current_user_can( 'add_post_meta', $post_id, $unslashed_meta_key ) ) || $this->is_metadata_public( $meta->key ) ) { |
||
| 557 | add_post_meta( $post_id, $meta->key, $meta->value ); |
||
| 558 | } |
||
| 559 | |||
| 560 | break; |
||
| 561 | case 'update': |
||
|
0 ignored issues
–
show
The case body in a switch statement must start on the line following the statement.
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement. switch ($expr) {
case "A":
doSomething(); //right
break;
case "B":
doSomethingElse(); //wrong
break;
} To learn more about the PSR-2 coding standard, please refer to the PHP-Fig. Loading history...
|
|||
| 562 | |||
| 563 | if ( ! isset( $meta->value ) ) { |
||
| 564 | continue; |
||
| 565 | } elseif ( ! empty( $meta->id ) && ! empty( $existing_meta_item->meta_key ) && ( current_user_can( 'edit_post_meta', $post_id, $unslashed_existing_meta_key ) || $this->is_metadata_public( $meta->key ) ) ) { |
||
| 566 | update_metadata_by_mid( 'post', $meta->id, $meta->value ); |
||
| 567 | } elseif ( ! empty( $meta->key ) && ! empty( $meta->previous_value ) && ( current_user_can( 'edit_post_meta', $post_id, $unslashed_meta_key ) || $this->is_metadata_public( $meta->key ) ) ) { |
||
| 568 | update_post_meta( $post_id, $meta->key,$meta->value, $meta->previous_value ); |
||
| 569 | } elseif ( ! empty( $meta->key ) && ( current_user_can( 'edit_post_meta', $post_id, $unslashed_meta_key ) || $this->is_metadata_public( $meta->key ) ) ) { |
||
| 570 | update_post_meta( $post_id, $meta->key, $meta->value ); |
||
| 571 | } |
||
| 572 | |||
| 573 | break; |
||
| 574 | } |
||
| 575 | |||
| 576 | } |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Fires when a post is created via the REST API. |
||
| 581 | * |
||
| 582 | * @module json-api |
||
| 583 | * |
||
| 584 | * @since 2.3.0 |
||
| 585 | * |
||
| 586 | * @param int $post_id Post ID. |
||
| 587 | * @param array $insert Data used to build the post. |
||
| 588 | * @param string $new New post URL suffix. |
||
| 589 | */ |
||
| 590 | do_action( 'rest_api_inserted_post', $post_id, $insert, $new ); |
||
| 591 | |||
| 592 | $return = $this->get_post_by( 'ID', $post_id, $args['context'] ); |
||
| 593 | if ( !$return || is_wp_error( $return ) ) { |
||
| 594 | return $return; |
||
| 595 | } |
||
| 596 | |||
| 597 | View Code Duplication | if ( isset( $input['type'] ) && 'revision' === $input['type'] ) { |
|
| 598 | $return['preview_nonce'] = wp_create_nonce( 'post_preview_' . $input['parent'] ); |
||
| 599 | } |
||
| 600 | |||
| 601 | if ( isset( $sticky ) ) { |
||
| 602 | // workaround for sticky test occasionally failing, maybe a race condition with stick_post() above |
||
| 603 | $return['sticky'] = ( true === $sticky ); |
||
| 604 | } |
||
| 605 | |||
| 606 | /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ |
||
| 607 | do_action( 'wpcom_json_api_objects', 'posts' ); |
||
| 608 | |||
| 609 | return $return; |
||
| 610 | } |
||
| 611 | |||
| 612 | // /sites/%s/posts/%d/delete -> $blog_id, $post_id |
||
| 613 | View Code Duplication | function delete_post( $path, $blog_id, $post_id ) { |
|
| 614 | $post = get_post( $post_id ); |
||
| 615 | if ( !$post || is_wp_error( $post ) ) { |
||
| 616 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
||
| 617 | } |
||
| 618 | |||
| 619 | if ( ! $this->is_post_type_allowed( $post->post_type ) ) { |
||
| 620 | return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 ); |
||
| 621 | } |
||
| 622 | |||
| 623 | if ( !current_user_can( 'delete_post', $post->ID ) ) { |
||
| 624 | return new WP_Error( 'unauthorized', 'User cannot delete posts', 403 ); |
||
| 625 | } |
||
| 626 | |||
| 627 | $args = $this->query_args(); |
||
| 628 | $return = $this->get_post_by( 'ID', $post->ID, $args['context'] ); |
||
| 629 | if ( !$return || is_wp_error( $return ) ) { |
||
| 630 | return $return; |
||
| 631 | } |
||
| 632 | |||
| 633 | /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ |
||
| 634 | do_action( 'wpcom_json_api_objects', 'posts' ); |
||
| 635 | |||
| 636 | wp_delete_post( $post->ID ); |
||
| 637 | |||
| 638 | $status = get_post_status( $post->ID ); |
||
| 639 | if ( false === $status ) { |
||
| 640 | $return['status'] = 'deleted'; |
||
| 641 | return $return; |
||
| 642 | } |
||
| 643 | |||
| 644 | return $this->get_post_by( 'ID', $post->ID, $args['context'] ); |
||
| 645 | } |
||
| 646 | |||
| 647 | // /sites/%s/posts/%d/restore -> $blog_id, $post_id |
||
| 648 | View Code Duplication | function restore_post( $path, $blog_id, $post_id ) { |
|
| 649 | $args = $this->query_args(); |
||
| 650 | $post = get_post( $post_id ); |
||
| 651 | |||
| 652 | if ( !$post || is_wp_error( $post ) ) { |
||
| 653 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
||
| 654 | } |
||
| 655 | |||
| 656 | if ( !current_user_can( 'delete_post', $post->ID ) ) { |
||
| 657 | return new WP_Error( 'unauthorized', 'User cannot restore trashed posts', 403 ); |
||
| 658 | } |
||
| 659 | |||
| 660 | /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ |
||
| 661 | do_action( 'wpcom_json_api_objects', 'posts' ); |
||
| 662 | |||
| 663 | wp_untrash_post( $post->ID ); |
||
| 664 | |||
| 665 | return $this->get_post_by( 'ID', $post->ID, $args['context'] ); |
||
| 666 | } |
||
| 667 | |||
| 668 | View Code Duplication | private function parse_and_set_featured_image( $post_id, $delete_featured_image, $featured_image ) { |
|
| 669 | if ( $delete_featured_image ) { |
||
| 670 | delete_post_thumbnail( $post_id ); |
||
| 671 | return; |
||
| 672 | } |
||
| 673 | |||
| 674 | $featured_image = (string) $featured_image; |
||
| 675 | |||
| 676 | // if we got a post ID, we can just set it as the thumbnail |
||
| 677 | if ( ctype_digit( $featured_image ) && 'attachment' == get_post_type( $featured_image ) ) { |
||
| 678 | set_post_thumbnail( $post_id, $featured_image ); |
||
| 679 | return $featured_image; |
||
| 680 | } |
||
| 681 | |||
| 682 | $featured_image_id = $this->handle_media_sideload( $featured_image, $post_id ); |
||
| 683 | |||
| 684 | if ( empty( $featured_image_id ) || ! is_int( $featured_image_id ) ) |
||
| 685 | return false; |
||
| 686 | |||
| 687 | set_post_thumbnail( $post_id, $featured_image_id ); |
||
| 688 | return $featured_image_id; |
||
| 689 | } |
||
| 690 | |||
| 691 | View Code Duplication | private function parse_and_set_author( $author = null, $post_type = 'post' ) { |
|
| 692 | if ( empty( $author ) || ! post_type_supports( $post_type, 'author' ) ) |
||
| 693 | return get_current_user_id(); |
||
| 694 | |||
| 695 | if ( ctype_digit( $author ) ) { |
||
| 696 | $_user = get_user_by( 'id', $author ); |
||
| 697 | if ( ! $_user || is_wp_error( $_user ) ) |
||
| 698 | return new WP_Error( 'invalid_author', 'Invalid author provided' ); |
||
| 699 | |||
| 700 | return $_user->ID; |
||
| 701 | } |
||
| 702 | |||
| 703 | $_user = get_user_by( 'login', $author ); |
||
| 704 | if ( ! $_user || is_wp_error( $_user ) ) |
||
| 705 | return new WP_Error( 'invalid_author', 'Invalid author provided' ); |
||
| 706 | |||
| 707 | return $_user->ID; |
||
| 708 | } |
||
| 709 | } |
||
| 710 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.