Ticket #36120: 36120.patch
File 36120.patch, 12.5 KB (added by , 5 years ago) |
---|
-
src/wp-admin/includes/bookmark.php
diff --git src/wp-admin/includes/bookmark.php src/wp-admin/includes/bookmark.php index 0d85026..8b85ca5 100644
function get_default_link_to_edit() { 75 75 } 76 76 77 77 /** 78 * Deletes a specified link from the database.79 *80 * @since 2.0.081 *82 * @global wpdb $wpdb WordPress database abstraction object.83 *84 * @param int $link_id ID of the link to delete85 * @return true Always true.86 */87 function wp_delete_link( $link_id ) {88 global $wpdb;89 /**90 * Fires before a link is deleted.91 *92 * @since 2.0.093 *94 * @param int $link_id ID of the link to delete.95 */96 do_action( 'delete_link', $link_id );97 98 wp_delete_object_term_relationships( $link_id, 'link_category' );99 100 $wpdb->delete( $wpdb->links, array( 'link_id' => $link_id ) );101 102 /**103 * Fires after a link has been deleted.104 *105 * @since 2.2.0106 *107 * @param int $link_id ID of the deleted link.108 */109 do_action( 'deleted_link', $link_id );110 111 clean_bookmark_cache( $link_id );112 113 return true;114 }115 116 /**117 78 * Retrieves the link categories associated with the link specified. 118 79 * 119 80 * @since 2.1.0 … … function get_link_to_edit( $link_id ) { 139 100 } 140 101 141 102 /** 142 * Inserts/updates links into/in the database.143 *144 * @since 2.0.0145 *146 * @global wpdb $wpdb WordPress database abstraction object.147 *148 * @param array $linkdata Elements that make up the link to insert.149 * @param bool $wp_error Optional. Whether to return a WP_Error object on failure. Default false.150 * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.151 */152 function wp_insert_link( $linkdata, $wp_error = false ) {153 global $wpdb;154 155 $defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 );156 157 $args = wp_parse_args( $linkdata, $defaults );158 $r = wp_unslash( sanitize_bookmark( $args, 'db' ) );159 160 $link_id = $r['link_id'];161 $link_name = $r['link_name'];162 $link_url = $r['link_url'];163 164 $update = false;165 if ( ! empty( $link_id ) ) {166 $update = true;167 }168 169 if ( trim( $link_name ) == '' ) {170 if ( trim( $link_url ) != '' ) {171 $link_name = $link_url;172 } else {173 return 0;174 }175 }176 177 if ( trim( $link_url ) == '' ) {178 return 0;179 }180 181 $link_rating = ( ! empty( $r['link_rating'] ) ) ? $r['link_rating'] : 0;182 $link_image = ( ! empty( $r['link_image'] ) ) ? $r['link_image'] : '';183 $link_target = ( ! empty( $r['link_target'] ) ) ? $r['link_target'] : '';184 $link_visible = ( ! empty( $r['link_visible'] ) ) ? $r['link_visible'] : 'Y';185 $link_owner = ( ! empty( $r['link_owner'] ) ) ? $r['link_owner'] : get_current_user_id();186 $link_notes = ( ! empty( $r['link_notes'] ) ) ? $r['link_notes'] : '';187 $link_description = ( ! empty( $r['link_description'] ) ) ? $r['link_description'] : '';188 $link_rss = ( ! empty( $r['link_rss'] ) ) ? $r['link_rss'] : '';189 $link_rel = ( ! empty( $r['link_rel'] ) ) ? $r['link_rel'] : '';190 $link_category = ( ! empty( $r['link_category'] ) ) ? $r['link_category'] : array();191 192 // Make sure we set a valid category.193 if ( ! is_array( $link_category ) || 0 == count( $link_category ) ) {194 $link_category = array( get_option( 'default_link_category' ) );195 }196 197 if ( $update ) {198 if ( false === $wpdb->update( $wpdb->links, compact( 'link_url', 'link_name', 'link_image', 'link_target', 'link_description', 'link_visible', 'link_rating', 'link_rel', 'link_notes', 'link_rss' ), compact( 'link_id' ) ) ) {199 if ( $wp_error ) {200 return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error );201 } else {202 return 0;203 }204 }205 } else {206 if ( false === $wpdb->insert( $wpdb->links, compact( 'link_url', 'link_name', 'link_image', 'link_target', 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_rel', 'link_notes', 'link_rss' ) ) ) {207 if ( $wp_error ) {208 return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error );209 } else {210 return 0;211 }212 }213 $link_id = (int) $wpdb->insert_id;214 }215 216 wp_set_link_cats( $link_id, $link_category );217 218 if ( $update ) {219 /**220 * Fires after a link was updated in the database.221 *222 * @since 2.0.0223 *224 * @param int $link_id ID of the link that was updated.225 */226 do_action( 'edit_link', $link_id );227 } else {228 /**229 * Fires after a link was added to the database.230 *231 * @since 2.0.0232 *233 * @param int $link_id ID of the link that was added.234 */235 do_action( 'add_link', $link_id );236 }237 clean_bookmark_cache( $link_id );238 239 return $link_id;240 }241 242 /**243 * Update link with the specified link categories.244 *245 * @since 2.1.0246 *247 * @param int $link_id ID of the link to update.248 * @param array $link_categories Array of link categories to add the link to.249 */250 function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {251 // If $link_categories isn't already an array, make it one:252 if ( !is_array( $link_categories ) || 0 == count( $link_categories ) )253 $link_categories = array( get_option( 'default_link_category' ) );254 255 $link_categories = array_map( 'intval', $link_categories );256 $link_categories = array_unique( $link_categories );257 258 wp_set_object_terms( $link_id, $link_categories, 'link_category' );259 260 clean_bookmark_cache( $link_id );261 }262 263 /**264 * Updates a link in the database.265 *266 * @since 2.0.0267 *268 * @param array $linkdata Link data to update.269 * @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success.270 */271 function wp_update_link( $linkdata ) {272 $link_id = (int) $linkdata['link_id'];273 274 $link = get_bookmark( $link_id, ARRAY_A );275 276 // Escape data pulled from DB.277 $link = wp_slash( $link );278 279 // Passed link category list overwrites existing category list if not empty.280 if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] )281 && 0 != count( $linkdata['link_category'] ) )282 $link_cats = $linkdata['link_category'];283 else284 $link_cats = $link['link_category'];285 286 // Merge old and new fields with new fields overwriting old ones.287 $linkdata = array_merge( $link, $linkdata );288 $linkdata['link_category'] = $link_cats;289 290 return wp_insert_link( $linkdata );291 }292 293 /**294 103 * Outputs the 'disabled' message for the WordPress Link Manager. 295 104 * 296 105 * @since 3.5.0 -
src/wp-includes/bookmark.php
diff --git src/wp-includes/bookmark.php src/wp-includes/bookmark.php index 140f5fd..242bbc7 100644
function clean_bookmark_cache( $bookmark_id ) { 415 415 wp_cache_delete( 'get_bookmarks', 'bookmark' ); 416 416 clean_object_term_cache( $bookmark_id, 'link'); 417 417 } 418 419 /** 420 * Deletes a specified link from the database. 421 * 422 * @since 2.0.0 423 * 424 * @global wpdb $wpdb WordPress database abstraction object. 425 * 426 * @param int $link_id ID of the link to delete 427 * 428 * @return true Always true. 429 */ 430 function wp_delete_link( $link_id ) { 431 global $wpdb; 432 /** 433 * Fires before a link is deleted. 434 * 435 * @since 2.0.0 436 * 437 * @param int $link_id ID of the link to delete. 438 */ 439 do_action( 'delete_link', $link_id ); 440 441 wp_delete_object_term_relationships( $link_id, 'link_category' ); 442 443 $wpdb->delete( $wpdb->links, array( 'link_id' => $link_id ) ); 444 445 /** 446 * Fires after a link has been deleted. 447 * 448 * @since 2.2.0 449 * 450 * @param int $link_id ID of the deleted link. 451 */ 452 do_action( 'deleted_link', $link_id ); 453 454 clean_bookmark_cache( $link_id ); 455 456 return true; 457 } 458 459 /** 460 * Inserts/updates links into/in the database. 461 * 462 * @since 2.0.0 463 * 464 * @global wpdb $wpdb WordPress database abstraction object. 465 * 466 * @param array $linkdata Elements that make up the link to insert. 467 * @param bool $wp_error Optional. Whether to return a WP_Error object on failure. Default false. 468 * 469 * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success. 470 */ 471 function wp_insert_link( $linkdata, $wp_error = false ) { 472 global $wpdb; 473 474 $defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 ); 475 476 $args = wp_parse_args( $linkdata, $defaults ); 477 $r = wp_unslash( sanitize_bookmark( $args, 'db' ) ); 478 479 $link_id = $r['link_id']; 480 $link_name = $r['link_name']; 481 $link_url = $r['link_url']; 482 483 $update = false; 484 if ( ! empty( $link_id ) ) { 485 $update = true; 486 } 487 488 if ( trim( $link_name ) == '' ) { 489 if ( trim( $link_url ) != '' ) { 490 $link_name = $link_url; 491 } else { 492 return 0; 493 } 494 } 495 496 if ( trim( $link_url ) == '' ) { 497 return 0; 498 } 499 500 $link_rating = ( ! empty( $r['link_rating'] ) ) ? $r['link_rating'] : 0; 501 $link_image = ( ! empty( $r['link_image'] ) ) ? $r['link_image'] : ''; 502 $link_target = ( ! empty( $r['link_target'] ) ) ? $r['link_target'] : ''; 503 $link_visible = ( ! empty( $r['link_visible'] ) ) ? $r['link_visible'] : 'Y'; 504 $link_owner = ( ! empty( $r['link_owner'] ) ) ? $r['link_owner'] : get_current_user_id(); 505 $link_notes = ( ! empty( $r['link_notes'] ) ) ? $r['link_notes'] : ''; 506 $link_description = ( ! empty( $r['link_description'] ) ) ? $r['link_description'] : ''; 507 $link_rss = ( ! empty( $r['link_rss'] ) ) ? $r['link_rss'] : ''; 508 $link_rel = ( ! empty( $r['link_rel'] ) ) ? $r['link_rel'] : ''; 509 $link_category = ( ! empty( $r['link_category'] ) ) ? $r['link_category'] : array(); 510 511 // Make sure we set a valid category. 512 if ( ! is_array( $link_category ) || 0 == count( $link_category ) ) { 513 $link_category = array( get_option( 'default_link_category' ) ); 514 } 515 516 if ( $update ) { 517 if ( false === $wpdb->update( $wpdb->links, compact( 'link_url', 'link_name', 'link_image', 'link_target', 'link_description', 'link_visible', 'link_rating', 'link_rel', 'link_notes', 'link_rss' ), compact( 'link_id' ) ) ) { 518 if ( $wp_error ) { 519 return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error ); 520 } else { 521 return 0; 522 } 523 } 524 } else { 525 if ( false === $wpdb->insert( $wpdb->links, compact( 'link_url', 'link_name', 'link_image', 'link_target', 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_rel', 'link_notes', 'link_rss' ) ) ) { 526 if ( $wp_error ) { 527 return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error ); 528 } else { 529 return 0; 530 } 531 } 532 $link_id = (int) $wpdb->insert_id; 533 } 534 535 wp_set_link_cats( $link_id, $link_category ); 536 537 if ( $update ) { 538 /** 539 * Fires after a link was updated in the database. 540 * 541 * @since 2.0.0 542 * 543 * @param int $link_id ID of the link that was updated. 544 */ 545 do_action( 'edit_link', $link_id ); 546 } else { 547 /** 548 * Fires after a link was added to the database. 549 * 550 * @since 2.0.0 551 * 552 * @param int $link_id ID of the link that was added. 553 */ 554 do_action( 'add_link', $link_id ); 555 } 556 clean_bookmark_cache( $link_id ); 557 558 return $link_id; 559 } 560 561 /** 562 * Updates a link in the database. 563 * 564 * @since 2.0.0 565 * 566 * @param array $linkdata Link data to update. 567 * 568 * @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success. 569 */ 570 function wp_update_link( $linkdata ) { 571 $link_id = (int) $linkdata['link_id']; 572 573 $link = get_bookmark( $link_id, ARRAY_A ); 574 575 // Escape data pulled from DB. 576 $link = wp_slash( $link ); 577 578 // Passed link category list overwrites existing category list if not empty. 579 if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] ) 580 && 0 != count( $linkdata['link_category'] ) 581 ) { 582 $link_cats = $linkdata['link_category']; 583 } else { 584 $link_cats = $link['link_category']; 585 } 586 587 // Merge old and new fields with new fields overwriting old ones. 588 $linkdata = array_merge( $link, $linkdata ); 589 $linkdata['link_category'] = $link_cats; 590 591 return wp_insert_link( $linkdata ); 592 } 593 594 /** 595 * Update link with the specified link categories. 596 * 597 * @since 2.1.0 598 * 599 * @param int $link_id ID of the link to update. 600 * @param array $link_categories Array of link categories to add the link to. 601 */ 602 function wp_set_link_cats( $link_id = 0, $link_categories = array() ) { 603 // If $link_categories isn't already an array, make it one: 604 if ( ! is_array( $link_categories ) || 0 == count( $link_categories ) ) { 605 $link_categories = array( get_option( 'default_link_category' ) ); 606 } 607 608 $link_categories = array_map( 'intval', $link_categories ); 609 $link_categories = array_unique( $link_categories ); 610 611 wp_set_object_terms( $link_id, $link_categories, 'link_category' ); 612 613 clean_bookmark_cache( $link_id ); 614 }