wp/wp-admin/includes/bookmark.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
    16 function add_link() {
    16 function add_link() {
    17 	return edit_link();
    17 	return edit_link();
    18 }
    18 }
    19 
    19 
    20 /**
    20 /**
    21  * Update or insert a link using values provided in $_POST.
    21  * Updates or inserts a link using values provided in $_POST.
    22  *
    22  *
    23  * @since 2.0.0
    23  * @since 2.0.0
    24  *
    24  *
    25  * @param int $link_id Optional. ID of the link to edit.
    25  * @param int $link_id Optional. ID of the link to edit. Default 0.
    26  * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
    26  * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
    27  */
    27  */
    28 function edit_link( $link_id = 0 ) {
    28 function edit_link( $link_id = 0 ) {
    29 	if ( !current_user_can( 'manage_links' ) )
    29 	if ( ! current_user_can( 'manage_links' ) ) {
    30 		wp_die( __( 'Cheatin’ uh?' ), 403 );
    30 		wp_die(
       
    31 			'<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' .
       
    32 			'<p>' . __( 'Sorry, you are not allowed to edit the links for this site.' ) . '</p>',
       
    33 			403
       
    34 		);
       
    35 	}
    31 
    36 
    32 	$_POST['link_url'] = esc_html( $_POST['link_url'] );
    37 	$_POST['link_url'] = esc_html( $_POST['link_url'] );
    33 	$_POST['link_url'] = esc_url($_POST['link_url']);
    38 	$_POST['link_url'] = esc_url($_POST['link_url']);
    34 	$_POST['link_name'] = esc_html( $_POST['link_name'] );
    39 	$_POST['link_name'] = esc_html( $_POST['link_name'] );
    35 	$_POST['link_image'] = esc_html( $_POST['link_image'] );
    40 	$_POST['link_image'] = esc_html( $_POST['link_image'] );
    44 		return wp_insert_link( $_POST );
    49 		return wp_insert_link( $_POST );
    45 	}
    50 	}
    46 }
    51 }
    47 
    52 
    48 /**
    53 /**
    49  * Retrieve the default link for editing.
    54  * Retrieves the default link for editing.
    50  *
    55  *
    51  * @since 2.0.0
    56  * @since 2.0.0
    52  *
    57  *
    53  * @return stdClass Default link
    58  * @return stdClass Default link object.
    54  */
    59  */
    55 function get_default_link_to_edit() {
    60 function get_default_link_to_edit() {
    56 	$link = new stdClass;
    61 	$link = new stdClass;
    57 	if ( isset( $_GET['linkurl'] ) )
    62 	if ( isset( $_GET['linkurl'] ) )
    58 		$link->link_url = esc_url( wp_unslash( $_GET['linkurl'] ) );
    63 		$link->link_url = esc_url( wp_unslash( $_GET['linkurl'] ) );
    68 
    73 
    69 	return $link;
    74 	return $link;
    70 }
    75 }
    71 
    76 
    72 /**
    77 /**
    73  * Delete link specified from database.
    78  * Deletes a specified link from the database.
    74  *
    79  *
    75  * @since 2.0.0
    80  * @since 2.0.0
       
    81  *
       
    82  * @global wpdb $wpdb WordPress database abstraction object.
    76  *
    83  *
    77  * @param int $link_id ID of the link to delete
    84  * @param int $link_id ID of the link to delete
    78  * @return bool True
    85  * @return true Always true.
    79  */
    86  */
    80 function wp_delete_link( $link_id ) {
    87 function wp_delete_link( $link_id ) {
    81 	global $wpdb;
    88 	global $wpdb;
    82 	/**
    89 	/**
    83 	 * Fires before a link is deleted.
    90 	 * Fires before a link is deleted.
    89 	do_action( 'delete_link', $link_id );
    96 	do_action( 'delete_link', $link_id );
    90 
    97 
    91 	wp_delete_object_term_relationships( $link_id, 'link_category' );
    98 	wp_delete_object_term_relationships( $link_id, 'link_category' );
    92 
    99 
    93 	$wpdb->delete( $wpdb->links, array( 'link_id' => $link_id ) );
   100 	$wpdb->delete( $wpdb->links, array( 'link_id' => $link_id ) );
       
   101 
    94 	/**
   102 	/**
    95 	 * Fires after a link has been deleted.
   103 	 * Fires after a link has been deleted.
    96 	 *
   104 	 *
    97 	 * @since 2.2.0
   105 	 * @since 2.2.0
    98 	 *
   106 	 *
   112  *
   120  *
   113  * @param int $link_id Link ID to look up
   121  * @param int $link_id Link ID to look up
   114  * @return array The requested link's categories
   122  * @return array The requested link's categories
   115  */
   123  */
   116 function wp_get_link_cats( $link_id = 0 ) {
   124 function wp_get_link_cats( $link_id = 0 ) {
   117 
       
   118 	$cats = wp_get_object_terms( $link_id, 'link_category', array('fields' => 'ids') );
   125 	$cats = wp_get_object_terms( $link_id, 'link_category', array('fields' => 'ids') );
   119 
       
   120 	return array_unique( $cats );
   126 	return array_unique( $cats );
   121 }
   127 }
   122 
   128 
   123 /**
   129 /**
   124  * Retrieve link data based on ID.
   130  * Retrieves link data based on its ID.
   125  *
   131  *
   126  * @since 2.0.0
   132  * @since 2.0.0
   127  *
   133  *
   128  * @param int $link_id ID of link to retrieve
   134  * @param int|stdClass $link Link ID or object to retrieve.
   129  * @return object Link for editing
   135  * @return object Link object for editing.
   130  */
   136  */
   131 function get_link_to_edit( $link_id ) {
   137 function get_link_to_edit( $link ) {
   132 	return get_bookmark( $link_id, OBJECT, 'edit' );
   138 	return get_bookmark( $link, OBJECT, 'edit' );
   133 }
   139 }
   134 
   140 
   135 /**
   141 /**
   136  * This function inserts/updates links into/in the database.
   142  * Inserts/updates links into/in the database.
   137  *
   143  *
   138  * @since 2.0.0
   144  * @since 2.0.0
       
   145  *
       
   146  * @global wpdb $wpdb WordPress database abstraction object.
   139  *
   147  *
   140  * @param array $linkdata Elements that make up the link to insert.
   148  * @param array $linkdata Elements that make up the link to insert.
   141  * @param bool $wp_error Optional. If true return WP_Error object on failure.
   149  * @param bool  $wp_error Optional. Whether to return a WP_Error object on failure. Default false.
   142  * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
   150  * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
   143  */
   151  */
   144 function wp_insert_link( $linkdata, $wp_error = false ) {
   152 function wp_insert_link( $linkdata, $wp_error = false ) {
   145 	global $wpdb;
   153 	global $wpdb;
   146 
   154 
   179 	$link_description = ( ! empty( $r['link_description'] ) ) ? $r['link_description'] : '';
   187 	$link_description = ( ! empty( $r['link_description'] ) ) ? $r['link_description'] : '';
   180 	$link_rss         = ( ! empty( $r['link_rss'] ) ) ? $r['link_rss'] : '';
   188 	$link_rss         = ( ! empty( $r['link_rss'] ) ) ? $r['link_rss'] : '';
   181 	$link_rel         = ( ! empty( $r['link_rel'] ) ) ? $r['link_rel'] : '';
   189 	$link_rel         = ( ! empty( $r['link_rel'] ) ) ? $r['link_rel'] : '';
   182 	$link_category    = ( ! empty( $r['link_category'] ) ) ? $r['link_category'] : array();
   190 	$link_category    = ( ! empty( $r['link_category'] ) ) ? $r['link_category'] : array();
   183 
   191 
   184 	// Make sure we set a valid category
   192 	// Make sure we set a valid category.
   185 	if ( ! is_array( $link_category ) || 0 == count( $link_category ) ) {
   193 	if ( ! is_array( $link_category ) || 0 == count( $link_category ) ) {
   186 		$link_category = array( get_option( 'default_link_category' ) );
   194 		$link_category = array( get_option( 'default_link_category' ) );
   187 	}
   195 	}
   188 
   196 
   189 	if ( $update ) {
   197 	if ( $update ) {
   234 /**
   242 /**
   235  * Update link with the specified link categories.
   243  * Update link with the specified link categories.
   236  *
   244  *
   237  * @since 2.1.0
   245  * @since 2.1.0
   238  *
   246  *
   239  * @param int $link_id ID of link to update
   247  * @param int   $link_id         ID of the link to update.
   240  * @param array $link_categories Array of categories to
   248  * @param array $link_categories Array of link categories to add the link to.
   241  */
   249  */
   242 function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
   250 function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
   243 	// If $link_categories isn't already an array, make it one:
   251 	// If $link_categories isn't already an array, make it one:
   244 	if ( !is_array( $link_categories ) || 0 == count( $link_categories ) )
   252 	if ( !is_array( $link_categories ) || 0 == count( $link_categories ) )
   245 		$link_categories = array( get_option( 'default_link_category' ) );
   253 		$link_categories = array( get_option( 'default_link_category' ) );
   251 
   259 
   252 	clean_bookmark_cache( $link_id );
   260 	clean_bookmark_cache( $link_id );
   253 }
   261 }
   254 
   262 
   255 /**
   263 /**
   256  * Update a link in the database.
   264  * Updates a link in the database.
   257  *
   265  *
   258  * @since 2.0.0
   266  * @since 2.0.0
   259  *
   267  *
   260  * @param array $linkdata Link data to update.
   268  * @param array $linkdata Link data to update.
   261  * @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success.
   269  * @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success.
   281 
   289 
   282 	return wp_insert_link( $linkdata );
   290 	return wp_insert_link( $linkdata );
   283 }
   291 }
   284 
   292 
   285 /**
   293 /**
       
   294  * Outputs the 'disabled' message for the WordPress Link Manager.
       
   295  *
   286  * @since 3.5.0
   296  * @since 3.5.0
   287  * @access private
   297  * @access private
       
   298  *
       
   299  * @global string $pagenow
   288  */
   300  */
   289 function wp_link_manager_disabled_message() {
   301 function wp_link_manager_disabled_message() {
   290 	global $pagenow;
   302 	global $pagenow;
   291 	if ( 'link-manager.php' != $pagenow && 'link-add.php' != $pagenow && 'link.php' != $pagenow )
   303 	if ( 'link-manager.php' != $pagenow && 'link-add.php' != $pagenow && 'link.php' != $pagenow )
   292 		return;
   304 		return;
   298 	if ( $really_can_manage_links && current_user_can( 'install_plugins' ) ) {
   310 	if ( $really_can_manage_links && current_user_can( 'install_plugins' ) ) {
   299 		$link = network_admin_url( 'plugin-install.php?tab=search&amp;s=Link+Manager' );
   311 		$link = network_admin_url( 'plugin-install.php?tab=search&amp;s=Link+Manager' );
   300 		wp_die( sprintf( __( 'If you are looking to use the link manager, please install the <a href="%s">Link Manager</a> plugin.' ), $link ) );
   312 		wp_die( sprintf( __( 'If you are looking to use the link manager, please install the <a href="%s">Link Manager</a> plugin.' ), $link ) );
   301 	}
   313 	}
   302 
   314 
   303 	wp_die( __( 'You do not have sufficient permissions to edit the links for this site.' ) );
   315 	wp_die( __( 'Sorry, you are not allowed to edit the links for this site.' ) );
   304 }
   316 }
   305 add_action( 'admin_page_access_denied', 'wp_link_manager_disabled_message' );