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_v1_2_Endpoint extends WPCOM_JSON_API_Update_Post_v1_1_Endpoint { |
||
| 3 | |||
| 4 | // /sites/%s/posts/new -> $blog_id |
||
| 5 | // /sites/%s/posts/%d -> $blog_id, $post_id |
||
| 6 | function write_post( $path, $blog_id, $post_id ) { |
||
| 7 | $new = $this->api->ends_with( $path, '/new' ); |
||
| 8 | $args = $this->query_args(); |
||
| 9 | |||
| 10 | // unhook publicize, it's hooked again later -- without this, skipping services is impossible |
||
| 11 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
| 12 | remove_action( 'save_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ), 100, 2 ); |
||
| 13 | add_action( 'rest_api_inserted_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ) ); |
||
| 14 | } |
||
| 15 | |||
| 16 | // 'future' is an alias for 'publish' for now |
||
| 17 | if ( isset( $input['status'] ) && 'future' === $input['status'] ) { |
||
| 18 | $input['status'] = 'publish'; |
||
| 19 | } |
||
| 20 | |||
| 21 | if ( $new ) { |
||
| 22 | $input = $this->input( true ); |
||
| 23 | |||
| 24 | if ( 'revision' === $input['type'] ) { |
||
| 25 | if ( ! isset( $input['parent'] ) ) { |
||
| 26 | return new WP_Error( 'invalid_input', 'Invalid request input', 400 ); |
||
| 27 | } |
||
| 28 | $input['status'] = 'inherit'; // force inherit for revision type |
||
| 29 | $input['slug'] = $input['parent'] . '-autosave-v1'; |
||
| 30 | } |
||
| 31 | elseif ( !isset( $input['title'] ) && !isset( $input['content'] ) && !isset( $input['excerpt'] ) ) { |
||
| 32 | return new WP_Error( 'invalid_input', 'Invalid request input', 400 ); |
||
| 33 | } |
||
| 34 | |||
| 35 | // default to post |
||
| 36 | if ( empty( $input['type'] ) ) |
||
| 37 | $input['type'] = 'post'; |
||
| 38 | |||
| 39 | $post_type = get_post_type_object( $input['type'] ); |
||
| 40 | |||
| 41 | if ( ! $this->is_post_type_allowed( $input['type'] ) ) { |
||
| 42 | return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 ); |
||
| 43 | } |
||
| 44 | |||
| 45 | if ( ! empty( $input['author'] ) ) { |
||
| 46 | $author_id = parent::parse_and_set_author( $input['author'], $input['type'] ); |
||
| 47 | unset( $input['author'] ); |
||
| 48 | if ( is_wp_error( $author_id ) ) |
||
| 49 | return $author_id; |
||
| 50 | } |
||
| 51 | |||
| 52 | if ( 'publish' === $input['status'] ) { |
||
| 53 | if ( ! current_user_can( $post_type->cap->publish_posts ) ) { |
||
| 54 | if ( current_user_can( $post_type->cap->edit_posts ) ) { |
||
| 55 | $input['status'] = 'pending'; |
||
| 56 | } else { |
||
| 57 | return new WP_Error( 'unauthorized', 'User cannot publish posts', 403 ); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | } else { |
||
| 61 | if ( !current_user_can( $post_type->cap->edit_posts ) ) { |
||
| 62 | return new WP_Error( 'unauthorized', 'User cannot edit posts', 403 ); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } else { |
||
| 66 | $input = $this->input( false ); |
||
| 67 | |||
| 68 | if ( !is_array( $input ) || !$input ) { |
||
| 69 | return new WP_Error( 'invalid_input', 'Invalid request input', 400 ); |
||
| 70 | } |
||
| 71 | |||
| 72 | $post = get_post( $post_id ); |
||
| 73 | $_post_type = ( ! empty( $input['type'] ) ) ? $input['type'] : $post->post_type; |
||
| 74 | $post_type = get_post_type_object( $_post_type ); |
||
| 75 | if ( !$post || is_wp_error( $post ) ) { |
||
| 76 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
||
| 77 | } |
||
| 78 | |||
| 79 | if ( !current_user_can( 'edit_post', $post->ID ) ) { |
||
| 80 | return new WP_Error( 'unauthorized', 'User cannot edit post', 403 ); |
||
| 81 | } |
||
| 82 | |||
| 83 | if ( ! empty( $input['author'] ) ) { |
||
| 84 | $author_id = parent::parse_and_set_author( $input['author'], $_post_type ); |
||
| 85 | unset( $input['author'] ); |
||
| 86 | if ( is_wp_error( $author_id ) ) |
||
| 87 | return $author_id; |
||
| 88 | } |
||
| 89 | |||
| 90 | if ( ( isset( $input['status'] ) && 'publish' === $input['status'] ) && 'publish' !== $post->post_status && !current_user_can( 'publish_post', $post->ID ) ) { |
||
| 91 | $input['status'] = 'pending'; |
||
| 92 | } |
||
| 93 | $last_status = $post->post_status; |
||
| 94 | $new_status = isset( $input['status'] ) ? $input['status'] : $last_status; |
||
| 95 | |||
| 96 | // Make sure that drafts get the current date when transitioning to publish if not supplied in the post. |
||
| 97 | $date_in_past = ( strtotime($post->post_date_gmt) < time() ); |
||
| 98 | if ( 'publish' === $new_status && 'draft' === $last_status && ! isset( $input['date_gmt'] ) && $date_in_past ) { |
||
| 99 | $input['date_gmt'] = gmdate( 'Y-m-d H:i:s' ); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | // If date is set, $this->input will set date_gmt, date still needs to be adjusted for the blog's offset |
||
| 104 | View Code Duplication | if ( isset( $input['date_gmt'] ) ) { |
|
| 105 | $gmt_offset = get_option( 'gmt_offset' ); |
||
| 106 | $time_with_offset = strtotime( $input['date_gmt'] ) + $gmt_offset * HOUR_IN_SECONDS; |
||
| 107 | $input['date'] = date( 'Y-m-d H:i:s', $time_with_offset ); |
||
| 108 | } |
||
| 109 | |||
| 110 | View Code Duplication | if ( ! empty( $author_id ) && get_current_user_id() != $author_id ) { |
|
| 111 | if ( ! current_user_can( $post_type->cap->edit_others_posts ) ) { |
||
| 112 | return new WP_Error( 'unauthorized', "User is not allowed to publish others' posts.", 403 ); |
||
| 113 | } elseif ( ! user_can( $author_id, $post_type->cap->edit_posts ) ) { |
||
| 114 | return new WP_Error( 'unauthorized', 'Assigned author cannot publish post.', 403 ); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | if ( !is_post_type_hierarchical( $post_type->name ) && 'revision' !== $post_type->name ) { |
||
| 119 | unset( $input['parent'] ); |
||
| 120 | } |
||
| 121 | |||
| 122 | /* add taxonomies by name */ |
||
| 123 | $tax_input = array(); |
||
| 124 | foreach ( array( 'categories' => 'category', 'tags' => 'post_tag' ) as $key => $taxonomy ) { |
||
| 125 | if ( ! isset( $input[ $key ] ) ) { |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | |||
| 129 | $tax_input[ $taxonomy ] = array(); |
||
| 130 | |||
| 131 | $is_hierarchical = is_taxonomy_hierarchical( $taxonomy ); |
||
| 132 | |||
| 133 | if ( is_array( $input[$key] ) ) { |
||
| 134 | $terms = $input[$key]; |
||
| 135 | } else { |
||
| 136 | $terms = explode( ',', $input[$key] ); |
||
| 137 | } |
||
| 138 | |||
| 139 | foreach ( $terms as $term ) { |
||
| 140 | /** |
||
| 141 | * We assume these are names, not IDs, even if they are numeric. |
||
| 142 | * Note: A category named "0" will not work right. |
||
| 143 | * https://core.trac.wordpress.org/ticket/9059 |
||
| 144 | */ |
||
| 145 | $term_info = get_term_by( 'name', $term, $taxonomy, ARRAY_A ); |
||
| 146 | |||
| 147 | if ( ! $term_info ) { |
||
| 148 | // only add a new tag/cat if the user has access to |
||
| 149 | $tax = get_taxonomy( $taxonomy ); |
||
| 150 | |||
| 151 | // see https://core.trac.wordpress.org/ticket/26409 |
||
| 152 | if ( 'category' === $taxonomy && ! current_user_can( $tax->cap->edit_terms ) ) { |
||
| 153 | continue; |
||
| 154 | } else if ( ! current_user_can( $tax->cap->assign_terms ) ) { |
||
| 155 | continue; |
||
| 156 | } |
||
| 157 | |||
| 158 | $term_info = wp_insert_term( $term, $taxonomy ); |
||
| 159 | } |
||
| 160 | |||
| 161 | View Code Duplication | if ( ! is_wp_error( $term_info ) ) { |
|
| 162 | if ( $is_hierarchical ) { |
||
| 163 | // Categories must be added by ID |
||
| 164 | $tax_input[$taxonomy][] = (int) $term_info['term_id']; |
||
| 165 | } else { |
||
| 166 | // Tags must be added by name |
||
| 167 | $tax_input[$taxonomy][] = $term; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | /* add taxonomies by ID */ |
||
| 174 | foreach ( array( 'categories_by_id' => 'category', 'tags_by_id' => 'post_tag' ) as $key => $taxonomy ) { |
||
| 175 | if ( ! isset( $input[ $key ] ) ) { |
||
| 176 | continue; |
||
| 177 | } |
||
| 178 | |||
| 179 | // combine with any previous selections |
||
| 180 | if ( ! isset( $tax_input[ $taxonomy ] ) || ! is_array( $tax_input[ $taxonomy ] ) ) { |
||
| 181 | $tax_input[ $taxonomy ] = array(); |
||
| 182 | } |
||
| 183 | |||
| 184 | $is_hierarchical = is_taxonomy_hierarchical( $taxonomy ); |
||
| 185 | |||
| 186 | if ( is_array( $input[$key] ) ) { |
||
| 187 | $terms = $input[$key]; |
||
| 188 | } else { |
||
| 189 | $terms = explode( ',', $input[$key] ); |
||
| 190 | } |
||
| 191 | |||
| 192 | foreach ( $terms as $term ) { |
||
| 193 | if ( ! ctype_digit( $term ) ) { |
||
| 194 | // skip anything that doesn't look like an ID |
||
| 195 | continue; |
||
| 196 | } |
||
| 197 | $term = (int) $term; |
||
| 198 | $term_info = get_term_by( 'id', $term, $taxonomy, ARRAY_A ); |
||
| 199 | |||
| 200 | View Code Duplication | if ( $term_info && ! is_wp_error( $term_info ) ) { |
|
| 201 | if ( $is_hierarchical ) { |
||
| 202 | // Categories must be added by ID |
||
| 203 | $tax_input[$taxonomy][] = $term; |
||
| 204 | } else { |
||
| 205 | // Tags must be added by name |
||
| 206 | $tax_input[$taxonomy][] = $term_info['name']; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | View Code Duplication | if ( ( isset( $input['categories'] ) || isset( $input['categories_by_id'] ) ) |
|
| 213 | && empty( $tax_input['category'] ) && 'revision' !== $post_type->name ) { |
||
| 214 | $tax_input['category'][] = get_option( 'default_category' ); |
||
| 215 | } |
||
| 216 | |||
| 217 | unset( $input['tags'], $input['categories'], $input['tags_by_id'], $input['categories_by_id'] ); |
||
| 218 | |||
| 219 | $insert = array(); |
||
| 220 | |||
| 221 | View Code Duplication | if ( !empty( $input['slug'] ) ) { |
|
| 222 | $insert['post_name'] = $input['slug']; |
||
| 223 | unset( $input['slug'] ); |
||
| 224 | } |
||
| 225 | |||
| 226 | View Code Duplication | if ( isset( $input['discussion'] ) ) { |
|
| 227 | $discussion = (array) $input['discussion']; |
||
| 228 | foreach ( array( 'comment', 'ping' ) as $discussion_type ) { |
||
| 229 | $discussion_open = sprintf( '%ss_open', $discussion_type ); |
||
| 230 | $discussion_status = sprintf( '%s_status', $discussion_type ); |
||
| 231 | |||
| 232 | if ( isset( $discussion[ $discussion_open ] ) ) { |
||
| 233 | $is_open = WPCOM_JSON_API::is_truthy( $discussion[ $discussion_open ] ); |
||
| 234 | $discussion[ $discussion_status ] = $is_open ? 'open' : 'closed'; |
||
| 235 | } |
||
| 236 | |||
| 237 | if ( in_array( $discussion[ $discussion_status ], array( 'open', 'closed' ) ) ) { |
||
| 238 | $insert[ $discussion_status ] = $discussion[ $discussion_status ]; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | unset( $input['discussion'] ); |
||
| 244 | |||
| 245 | View Code Duplication | if ( isset( $input['menu_order'] ) ) { |
|
| 246 | $insert['menu_order'] = $input['menu_order']; |
||
| 247 | unset( $input['menu_order'] ); |
||
| 248 | } |
||
| 249 | |||
| 250 | $publicize = isset( $input['publicize'] ) ? $input['publicize'] : null; |
||
| 251 | unset( $input['publicize'] ); |
||
| 252 | |||
| 253 | $publicize_custom_message = isset( $input['publicize_message'] ) ? $input['publicize_message'] : null; |
||
| 254 | unset( $input['publicize_message'] ); |
||
| 255 | |||
| 256 | View Code Duplication | if ( isset( $input['featured_image'] ) ) { |
|
| 257 | $featured_image = trim( $input['featured_image'] ); |
||
| 258 | $delete_featured_image = empty( $featured_image ); |
||
| 259 | unset( $input['featured_image'] ); |
||
| 260 | } |
||
| 261 | |||
| 262 | $metadata = isset( $input['metadata'] ) ? $input['metadata'] : null; |
||
| 263 | unset( $input['metadata'] ); |
||
| 264 | |||
| 265 | $likes = isset( $input['likes_enabled'] ) ? $input['likes_enabled'] : null; |
||
| 266 | unset( $input['likes_enabled'] ); |
||
| 267 | |||
| 268 | $sharing = isset( $input['sharing_enabled'] ) ? $input['sharing_enabled'] : null; |
||
| 269 | unset( $input['sharing_enabled'] ); |
||
| 270 | |||
| 271 | $sticky = isset( $input['sticky'] ) ? $input['sticky'] : null; |
||
| 272 | unset( $input['sticky'] ); |
||
| 273 | |||
| 274 | foreach ( $input as $key => $value ) { |
||
| 275 | $insert["post_$key"] = $value; |
||
| 276 | } |
||
| 277 | |||
| 278 | if ( ! empty( $author_id ) ) { |
||
| 279 | $insert['post_author'] = absint( $author_id ); |
||
| 280 | } |
||
| 281 | |||
| 282 | if ( ! empty( $tax_input ) ) { |
||
| 283 | $insert['tax_input'] = $tax_input; |
||
| 284 | } |
||
| 285 | |||
| 286 | $has_media = ! empty( $input['media'] ) ? count( $input['media'] ) : false; |
||
| 287 | $has_media_by_url = ! empty( $input['media_urls'] ) ? count( $input['media_urls'] ) : false; |
||
| 288 | |||
| 289 | View Code Duplication | if ( $new ) { |
|
| 290 | |||
| 291 | if ( isset( $input['content'] ) && ! has_shortcode( $input['content'], 'gallery' ) && ( $has_media || $has_media_by_url ) ) { |
||
| 292 | switch ( ( $has_media + $has_media_by_url ) ) { |
||
| 293 | case 0 : |
||
| 294 | // No images - do nothing. |
||
| 295 | break; |
||
| 296 | case 1 : |
||
| 297 | // 1 image - make it big |
||
| 298 | $insert['post_content'] = $input['content'] = "[gallery size=full columns=1]\n\n" . $input['content']; |
||
| 299 | break; |
||
| 300 | default : |
||
| 301 | // Several images - 3 column gallery |
||
| 302 | $insert['post_content'] = $input['content'] = "[gallery]\n\n" . $input['content']; |
||
| 303 | break; |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | $post_id = wp_insert_post( add_magic_quotes( $insert ), true ); |
||
| 308 | } else { |
||
| 309 | $insert['ID'] = $post->ID; |
||
| 310 | |||
| 311 | // wp_update_post ignores date unless edit_date is set |
||
| 312 | // See: http://codex.wordpress.org/Function_Reference/wp_update_post#Scheduling_posts |
||
| 313 | // See: https://core.trac.wordpress.org/browser/tags/3.9.2/src/wp-includes/post.php#L3302 |
||
| 314 | if ( isset( $input['date_gmt'] ) || isset( $input['date'] ) ) { |
||
| 315 | $insert['edit_date'] = true; |
||
| 316 | } |
||
| 317 | |||
| 318 | $post_id = wp_update_post( (object) $insert ); |
||
| 319 | } |
||
| 320 | |||
| 321 | |||
| 322 | if ( !$post_id || is_wp_error( $post_id ) ) { |
||
| 323 | return $post_id; |
||
| 324 | } |
||
| 325 | |||
| 326 | // make sure this post actually exists and is not an error of some kind (ie, trying to load media in the posts endpoint) |
||
| 327 | $post_check = $this->get_post_by( 'ID', $post_id, $args['context'] ); |
||
| 328 | if ( is_wp_error( $post_check ) ) { |
||
| 329 | return $post_check; |
||
| 330 | } |
||
| 331 | |||
| 332 | View Code Duplication | if ( $has_media || $has_media_by_url ) { |
|
| 333 | $media_files = ! empty( $input['media'] ) ? $input['media'] : array(); |
||
| 334 | $media_urls = ! empty( $input['media_urls'] ) ? $input['media_urls'] : array(); |
||
| 335 | $media_attrs = ! empty( $input['media_attrs'] ) ? $input['media_attrs'] : array(); |
||
| 336 | $force_parent_id = $post_id; |
||
| 337 | $media_results = $this->handle_media_creation_v1_1( $media_files, $media_urls, $media_attrs, $force_parent_id ); |
||
| 338 | } |
||
| 339 | |||
| 340 | // set page template for this post.. |
||
| 341 | View Code Duplication | if ( isset( $input['page_template'] ) && 'page' == $post_type->name ) { |
|
| 342 | $page_template = $input['page_template']; |
||
| 343 | $page_templates = wp_get_theme()->get_page_templates( get_post( $post_id ) ); |
||
| 344 | if ( empty( $page_template ) || 'default' == $page_template || isset( $page_templates[ $page_template ] ) ) { |
||
| 345 | update_post_meta( $post_id, '_wp_page_template', $page_template ); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | // Set like status for the post |
||
| 350 | /** This filter is documented in modules/likes.php */ |
||
| 351 | $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) ); |
||
| 352 | View Code Duplication | if ( $new ) { |
|
| 353 | if ( $sitewide_likes_enabled ) { |
||
| 354 | if ( false === $likes ) { |
||
| 355 | update_post_meta( $post_id, 'switch_like_status', 1 ); |
||
| 356 | } else { |
||
| 357 | delete_post_meta( $post_id, 'switch_like_status' ); |
||
| 358 | } |
||
| 359 | } else { |
||
| 360 | if ( $likes ) { |
||
| 361 | update_post_meta( $post_id, 'switch_like_status', 1 ); |
||
| 362 | } else { |
||
| 363 | delete_post_meta( $post_id, 'switch_like_status' ); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | } else { |
||
| 367 | if ( isset( $likes ) ) { |
||
| 368 | if ( $sitewide_likes_enabled ) { |
||
| 369 | if ( false === $likes ) { |
||
| 370 | update_post_meta( $post_id, 'switch_like_status', 1 ); |
||
| 371 | } else { |
||
| 372 | delete_post_meta( $post_id, 'switch_like_status' ); |
||
| 373 | } |
||
| 374 | } else { |
||
| 375 | if ( true === $likes ) { |
||
| 376 | update_post_meta( $post_id, 'switch_like_status', 1 ); |
||
| 377 | } else { |
||
| 378 | delete_post_meta( $post_id, 'switch_like_status' ); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | // Set sharing status of the post |
||
| 385 | View Code Duplication | if ( $new ) { |
|
| 386 | $sharing_enabled = isset( $sharing ) ? (bool) $sharing : true; |
||
| 387 | if ( false === $sharing_enabled ) { |
||
| 388 | update_post_meta( $post_id, 'sharing_disabled', 1 ); |
||
| 389 | } |
||
| 390 | } |
||
| 391 | else { |
||
| 392 | if ( isset( $sharing ) && true === $sharing ) { |
||
| 393 | delete_post_meta( $post_id, 'sharing_disabled' ); |
||
| 394 | } else if ( isset( $sharing ) && false == $sharing ) { |
||
| 395 | update_post_meta( $post_id, 'sharing_disabled', 1 ); |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | if ( isset( $sticky ) ) { |
||
| 400 | if ( true === $sticky ) { |
||
| 401 | stick_post( $post_id ); |
||
| 402 | } else { |
||
| 403 | unstick_post( $post_id ); |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | // WPCOM Specific (Jetpack's will get bumped elsewhere |
||
| 408 | // Tracks how many posts are published and sets meta |
||
| 409 | // so we can track some other cool stats (like likes & comments on posts published) |
||
| 410 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
| 411 | if ( |
||
| 412 | ( $new && 'publish' == $input['status'] ) |
||
| 413 | || ( |
||
| 414 | !$new && isset( $last_status ) |
||
| 415 | && 'publish' != $last_status |
||
| 416 | && isset( $new_status ) |
||
| 417 | && 'publish' == $new_status |
||
| 418 | ) |
||
| 419 | ) { |
||
| 420 | /** This action is documented in modules/widgets/social-media-icons.php */ |
||
| 421 | do_action( 'jetpack_bump_stats_extras', 'api-insights-posts', $this->api->token_details['client_id'] ); |
||
| 422 | update_post_meta( $post_id, '_rest_api_published', 1 ); |
||
| 423 | update_post_meta( $post_id, '_rest_api_client_id', $this->api->token_details['client_id'] ); |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | |||
| 428 | // We ask the user/dev to pass Publicize services he/she wants activated for the post, but Publicize expects us |
||
| 429 | // to instead flag the ones we don't want to be skipped. proceed with said logic. |
||
| 430 | // any posts coming from Path (client ID 25952) should also not publicize |
||
| 431 | View Code Duplication | if ( $publicize === false || ( isset( $this->api->token_details['client_id'] ) && 25952 == $this->api->token_details['client_id'] ) ) { |
|
| 432 | // No publicize at all, skip all by ID |
||
| 433 | foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) { |
||
| 434 | delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name ); |
||
| 435 | $service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name ); |
||
| 436 | if ( ! $service_connections ) { |
||
| 437 | continue; |
||
| 438 | } |
||
| 439 | foreach ( $service_connections as $service_connection ) { |
||
| 440 | update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 ); |
||
| 441 | } |
||
| 442 | } |
||
| 443 | } else if ( is_array( $publicize ) && ( count ( $publicize ) > 0 ) ) { |
||
| 444 | foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) { |
||
| 445 | /* |
||
| 446 | * We support both indexed and associative arrays: |
||
| 447 | * * indexed are to pass entire services |
||
| 448 | * * associative are to pass specific connections per service |
||
| 449 | * |
||
| 450 | * We do support mixed arrays: mixed integer and string keys (see 3rd example below). |
||
| 451 | * |
||
| 452 | * EG: array( 'twitter', 'facebook') will only publicize to those, ignoring the other available services |
||
| 453 | * Form data: publicize[]=twitter&publicize[]=facebook |
||
| 454 | * 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. |
||
| 455 | * Form data: publicize[twitter]=$pub_conn_id_0,$pub_conn_id_3&publicize[facebook]=$pub_conn_id_7 |
||
| 456 | * 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 |
||
| 457 | * Form data: publicize[]=twitter&publicize[facebook]=$pub_conn_id_0,$pub_conn_id_3 |
||
| 458 | */ |
||
| 459 | |||
| 460 | // Delete any stale SKIP value for the service by name. We'll add it back by ID. |
||
| 461 | delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name ); |
||
| 462 | |||
| 463 | // Get the user's connections |
||
| 464 | $service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name ); |
||
| 465 | |||
| 466 | // if the user doesn't have any connections for this service, move on |
||
| 467 | if ( ! $service_connections ) { |
||
| 468 | continue; |
||
| 469 | } |
||
| 470 | |||
| 471 | if ( !in_array( $name, $publicize ) && !array_key_exists( $name, $publicize ) ) { |
||
| 472 | // Skip the whole service by adding each connection ID |
||
| 473 | foreach ( $service_connections as $service_connection ) { |
||
| 474 | update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 ); |
||
| 475 | } |
||
| 476 | } else if ( !empty( $publicize[ $name ] ) ) { |
||
| 477 | // Seems we're being asked to only push to [a] specific connection[s]. |
||
| 478 | // Explode the list on commas, which will also support a single passed ID |
||
| 479 | $requested_connections = explode( ',', ( preg_replace( '/[\s]*/', '', $publicize[ $name ] ) ) ); |
||
| 480 | |||
| 481 | // Flag the connections we can't match with the requested list to be skipped. |
||
| 482 | foreach ( $service_connections as $service_connection ) { |
||
| 483 | if ( !in_array( $service_connection->meta['connection_data']->id, $requested_connections ) ) { |
||
| 484 | update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 ); |
||
| 485 | } else { |
||
| 486 | delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id ); |
||
| 487 | } |
||
| 488 | } |
||
| 489 | } else { |
||
| 490 | // delete all SKIP values; it's okay to publish to all connected IDs for this service |
||
| 491 | foreach ( $service_connections as $service_connection ) { |
||
| 492 | delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id ); |
||
| 493 | } |
||
| 494 | } |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | View Code Duplication | if ( ! is_null( $publicize_custom_message ) ) { |
|
| 499 | if ( empty( $publicize_custom_message ) ) { |
||
| 500 | delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_MESS ); |
||
| 501 | } else { |
||
| 502 | update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_MESS, trim( $publicize_custom_message ) ); |
||
| 503 | } |
||
| 504 | } |
||
| 505 | |||
| 506 | View Code Duplication | if ( ! empty( $insert['post_format'] ) ) { |
|
| 507 | if ( 'default' !== strtolower( $insert['post_format'] ) ) { |
||
| 508 | set_post_format( $post_id, $insert['post_format'] ); |
||
| 509 | } |
||
| 510 | else { |
||
| 511 | set_post_format( $post_id, get_option( 'default_post_format' ) ); |
||
| 512 | } |
||
| 513 | } |
||
| 514 | |||
| 515 | if ( isset( $featured_image ) ) { |
||
| 516 | parent::parse_and_set_featured_image( $post_id, $delete_featured_image, $featured_image ); |
||
| 517 | } |
||
| 518 | |||
| 519 | View Code Duplication | if ( ! empty( $metadata ) ) { |
|
| 520 | foreach ( (array) $metadata as $meta ) { |
||
| 521 | |||
| 522 | $meta = (object) $meta; |
||
| 523 | |||
| 524 | $existing_meta_item = new stdClass; |
||
| 525 | |||
| 526 | if ( empty( $meta->operation ) ) |
||
| 527 | $meta->operation = 'update'; |
||
| 528 | |||
| 529 | if ( ! empty( $meta->value ) ) { |
||
| 530 | if ( 'true' == $meta->value ) |
||
| 531 | $meta->value = true; |
||
| 532 | if ( 'false' == $meta->value ) |
||
| 533 | $meta->value = false; |
||
| 534 | } |
||
| 535 | |||
| 536 | if ( ! empty( $meta->id ) ) { |
||
| 537 | $meta->id = absint( $meta->id ); |
||
| 538 | $existing_meta_item = get_metadata_by_mid( 'post', $meta->id ); |
||
| 539 | } |
||
| 540 | |||
| 541 | $unslashed_meta_key = wp_unslash( $meta->key ); // should match what the final key will be |
||
| 542 | $meta->key = wp_slash( $meta->key ); |
||
| 543 | $unslashed_existing_meta_key = wp_unslash( $existing_meta_item->meta_key ); |
||
| 544 | $existing_meta_item->meta_key = wp_slash( $existing_meta_item->meta_key ); |
||
| 545 | |||
| 546 | // make sure that the meta id passed matches the existing meta key |
||
| 547 | if ( ! empty( $meta->id ) && ! empty( $meta->key ) ) { |
||
| 548 | $meta_by_id = get_metadata_by_mid( 'post', $meta->id ); |
||
| 549 | if ( $meta_by_id->meta_key !== $meta->key ) { |
||
| 550 | continue; // skip this meta |
||
| 551 | } |
||
| 552 | } |
||
| 553 | |||
| 554 | switch ( $meta->operation ) { |
||
| 555 | case 'delete': |
||
|
0 ignored issues
–
show
|
|||
| 556 | |||
| 557 | if ( ! empty( $meta->id ) && ! empty( $existing_meta_item->meta_key ) && current_user_can( 'delete_post_meta', $post_id, $unslashed_existing_meta_key ) ) { |
||
| 558 | delete_metadata_by_mid( 'post', $meta->id ); |
||
| 559 | } elseif ( ! empty( $meta->key ) && ! empty( $meta->previous_value ) && current_user_can( 'delete_post_meta', $post_id, $unslashed_meta_key ) ) { |
||
| 560 | delete_post_meta( $post_id, $meta->key, $meta->previous_value ); |
||
| 561 | } elseif ( ! empty( $meta->key ) && current_user_can( 'delete_post_meta', $post_id, $unslashed_meta_key ) ) { |
||
| 562 | delete_post_meta( $post_id, $meta->key ); |
||
| 563 | } |
||
| 564 | |||
| 565 | break; |
||
| 566 | 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...
|
|||
| 567 | |||
| 568 | if ( ! empty( $meta->id ) || ! empty( $meta->previous_value ) ) { |
||
| 569 | continue; |
||
| 570 | } elseif ( ! empty( $meta->key ) && ! empty( $meta->value ) && ( current_user_can( 'add_post_meta', $post_id, $unslashed_meta_key ) ) || $this->is_metadata_public( $meta->key ) ) { |
||
| 571 | add_post_meta( $post_id, $meta->key, $meta->value ); |
||
| 572 | } |
||
| 573 | |||
| 574 | break; |
||
| 575 | 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...
|
|||
| 576 | |||
| 577 | if ( ! isset( $meta->value ) ) { |
||
| 578 | continue; |
||
| 579 | } 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 ) ) ) { |
||
| 580 | update_metadata_by_mid( 'post', $meta->id, $meta->value ); |
||
| 581 | } 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 ) ) ) { |
||
| 582 | update_post_meta( $post_id, $meta->key,$meta->value, $meta->previous_value ); |
||
| 583 | } elseif ( ! empty( $meta->key ) && ( current_user_can( 'edit_post_meta', $post_id, $unslashed_meta_key ) || $this->is_metadata_public( $meta->key ) ) ) { |
||
| 584 | update_post_meta( $post_id, $meta->key, $meta->value ); |
||
| 585 | } |
||
| 586 | |||
| 587 | break; |
||
| 588 | } |
||
| 589 | |||
| 590 | } |
||
| 591 | } |
||
| 592 | |||
| 593 | /** This action is documented in json-endpoints/class.wpcom-json-api-update-post-endpoint.php */ |
||
| 594 | do_action( 'rest_api_inserted_post', $post_id, $insert, $new ); |
||
| 595 | |||
| 596 | $return = $this->get_post_by( 'ID', $post_id, $args['context'] ); |
||
| 597 | if ( !$return || is_wp_error( $return ) ) { |
||
| 598 | return $return; |
||
| 599 | } |
||
| 600 | |||
| 601 | View Code Duplication | if ( isset( $input['type'] ) && 'revision' === $input['type'] ) { |
|
| 602 | $return['preview_nonce'] = wp_create_nonce( 'post_preview_' . $input['parent'] ); |
||
| 603 | } |
||
| 604 | |||
| 605 | if ( isset( $sticky ) ) { |
||
| 606 | // workaround for sticky test occasionally failing, maybe a race condition with stick_post() above |
||
| 607 | $return['sticky'] = ( true === $sticky ); |
||
| 608 | } |
||
| 609 | |||
| 610 | if ( ! empty( $media_results['errors'] ) ) |
||
| 611 | $return['media_errors'] = $media_results['errors']; |
||
| 612 | |||
| 613 | View Code Duplication | if ( 'publish' !== $post->post_status && isset( $input['title'] )) { |
|
| 614 | $return['other_URLs'] = (object) $this->get_post_permalink_suggestions( $post_id, $input['title'] ); |
||
| 615 | } |
||
| 616 | |||
| 617 | /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ |
||
| 618 | do_action( 'wpcom_json_api_objects', 'posts' ); |
||
| 619 | |||
| 620 | return $return; |
||
| 621 | } |
||
| 622 | } |
||
| 623 |
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.