wp/wp-admin/includes/bookmark.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    37 	$_POST['link_url']   = esc_html( $_POST['link_url'] );
    37 	$_POST['link_url']   = esc_html( $_POST['link_url'] );
    38 	$_POST['link_url']   = esc_url( $_POST['link_url'] );
    38 	$_POST['link_url']   = esc_url( $_POST['link_url'] );
    39 	$_POST['link_name']  = esc_html( $_POST['link_name'] );
    39 	$_POST['link_name']  = esc_html( $_POST['link_name'] );
    40 	$_POST['link_image'] = esc_html( $_POST['link_image'] );
    40 	$_POST['link_image'] = esc_html( $_POST['link_image'] );
    41 	$_POST['link_rss']   = esc_url( $_POST['link_rss'] );
    41 	$_POST['link_rss']   = esc_url( $_POST['link_rss'] );
    42 	if ( ! isset( $_POST['link_visible'] ) || 'N' != $_POST['link_visible'] ) {
    42 	if ( ! isset( $_POST['link_visible'] ) || 'N' !== $_POST['link_visible'] ) {
    43 		$_POST['link_visible'] = 'Y';
    43 		$_POST['link_visible'] = 'Y';
    44 	}
    44 	}
    45 
    45 
    46 	if ( ! empty( $link_id ) ) {
    46 	if ( ! empty( $link_id ) ) {
    47 		$_POST['link_id'] = $link_id;
    47 		$_POST['link_id'] = $link_id;
   115 
   115 
   116 	return true;
   116 	return true;
   117 }
   117 }
   118 
   118 
   119 /**
   119 /**
   120  * Retrieves the link categories associated with the link specified.
   120  * Retrieves the link category IDs associated with the link specified.
   121  *
   121  *
   122  * @since 2.1.0
   122  * @since 2.1.0
   123  *
   123  *
   124  * @param int $link_id Link ID to look up
   124  * @param int $link_id Link ID to look up.
   125  * @return array The requested link's categories
   125  * @return int[] The IDs of the requested link's categories.
   126  */
   126  */
   127 function wp_get_link_cats( $link_id = 0 ) {
   127 function wp_get_link_cats( $link_id = 0 ) {
   128 	$cats = wp_get_object_terms( $link_id, 'link_category', array( 'fields' => 'ids' ) );
   128 	$cats = wp_get_object_terms( $link_id, 'link_category', array( 'fields' => 'ids' ) );
   129 	return array_unique( $cats );
   129 	return array_unique( $cats );
   130 }
   130 }
   160 		'link_name'   => '',
   160 		'link_name'   => '',
   161 		'link_url'    => '',
   161 		'link_url'    => '',
   162 		'link_rating' => 0,
   162 		'link_rating' => 0,
   163 	);
   163 	);
   164 
   164 
   165 	$args = wp_parse_args( $linkdata, $defaults );
   165 	$parsed_args = wp_parse_args( $linkdata, $defaults );
   166 	$r    = wp_unslash( sanitize_bookmark( $args, 'db' ) );
   166 	$parsed_args = wp_unslash( sanitize_bookmark( $parsed_args, 'db' ) );
   167 
   167 
   168 	$link_id   = $r['link_id'];
   168 	$link_id   = $parsed_args['link_id'];
   169 	$link_name = $r['link_name'];
   169 	$link_name = $parsed_args['link_name'];
   170 	$link_url  = $r['link_url'];
   170 	$link_url  = $parsed_args['link_url'];
   171 
   171 
   172 	$update = false;
   172 	$update = false;
   173 	if ( ! empty( $link_id ) ) {
   173 	if ( ! empty( $link_id ) ) {
   174 		$update = true;
   174 		$update = true;
   175 	}
   175 	}
   176 
   176 
   177 	if ( trim( $link_name ) == '' ) {
   177 	if ( '' === trim( $link_name ) ) {
   178 		if ( trim( $link_url ) != '' ) {
   178 		if ( '' !== trim( $link_url ) ) {
   179 			$link_name = $link_url;
   179 			$link_name = $link_url;
   180 		} else {
   180 		} else {
   181 			return 0;
   181 			return 0;
   182 		}
   182 		}
   183 	}
   183 	}
   184 
   184 
   185 	if ( trim( $link_url ) == '' ) {
   185 	if ( '' === trim( $link_url ) ) {
   186 		return 0;
   186 		return 0;
   187 	}
   187 	}
   188 
   188 
   189 	$link_rating      = ( ! empty( $r['link_rating'] ) ) ? $r['link_rating'] : 0;
   189 	$link_rating      = ( ! empty( $parsed_args['link_rating'] ) ) ? $parsed_args['link_rating'] : 0;
   190 	$link_image       = ( ! empty( $r['link_image'] ) ) ? $r['link_image'] : '';
   190 	$link_image       = ( ! empty( $parsed_args['link_image'] ) ) ? $parsed_args['link_image'] : '';
   191 	$link_target      = ( ! empty( $r['link_target'] ) ) ? $r['link_target'] : '';
   191 	$link_target      = ( ! empty( $parsed_args['link_target'] ) ) ? $parsed_args['link_target'] : '';
   192 	$link_visible     = ( ! empty( $r['link_visible'] ) ) ? $r['link_visible'] : 'Y';
   192 	$link_visible     = ( ! empty( $parsed_args['link_visible'] ) ) ? $parsed_args['link_visible'] : 'Y';
   193 	$link_owner       = ( ! empty( $r['link_owner'] ) ) ? $r['link_owner'] : get_current_user_id();
   193 	$link_owner       = ( ! empty( $parsed_args['link_owner'] ) ) ? $parsed_args['link_owner'] : get_current_user_id();
   194 	$link_notes       = ( ! empty( $r['link_notes'] ) ) ? $r['link_notes'] : '';
   194 	$link_notes       = ( ! empty( $parsed_args['link_notes'] ) ) ? $parsed_args['link_notes'] : '';
   195 	$link_description = ( ! empty( $r['link_description'] ) ) ? $r['link_description'] : '';
   195 	$link_description = ( ! empty( $parsed_args['link_description'] ) ) ? $parsed_args['link_description'] : '';
   196 	$link_rss         = ( ! empty( $r['link_rss'] ) ) ? $r['link_rss'] : '';
   196 	$link_rss         = ( ! empty( $parsed_args['link_rss'] ) ) ? $parsed_args['link_rss'] : '';
   197 	$link_rel         = ( ! empty( $r['link_rel'] ) ) ? $r['link_rel'] : '';
   197 	$link_rel         = ( ! empty( $parsed_args['link_rel'] ) ) ? $parsed_args['link_rel'] : '';
   198 	$link_category    = ( ! empty( $r['link_category'] ) ) ? $r['link_category'] : array();
   198 	$link_category    = ( ! empty( $parsed_args['link_category'] ) ) ? $parsed_args['link_category'] : array();
   199 
   199 
   200 	// Make sure we set a valid category.
   200 	// Make sure we set a valid category.
   201 	if ( ! is_array( $link_category ) || 0 == count( $link_category ) ) {
   201 	if ( ! is_array( $link_category ) || 0 === count( $link_category ) ) {
   202 		$link_category = array( get_option( 'default_link_category' ) );
   202 		$link_category = array( get_option( 'default_link_category' ) );
   203 	}
   203 	}
   204 
   204 
   205 	if ( $update ) {
   205 	if ( $update ) {
   206 		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' ) ) ) {
   206 		if ( false === $wpdb->update( $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' ), compact( 'link_id' ) ) ) {
   207 			if ( $wp_error ) {
   207 			if ( $wp_error ) {
   208 				return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error );
   208 				return new WP_Error( 'db_update_error', __( 'Could not update link in the database.' ), $wpdb->last_error );
   209 			} else {
   209 			} else {
   210 				return 0;
   210 				return 0;
   211 			}
   211 			}
   212 		}
   212 		}
   213 	} else {
   213 	} else {
   214 		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' ) ) ) {
   214 		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' ) ) ) {
   215 			if ( $wp_error ) {
   215 			if ( $wp_error ) {
   216 				return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error );
   216 				return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database.' ), $wpdb->last_error );
   217 			} else {
   217 			} else {
   218 				return 0;
   218 				return 0;
   219 			}
   219 			}
   220 		}
   220 		}
   221 		$link_id = (int) $wpdb->insert_id;
   221 		$link_id = (int) $wpdb->insert_id;
   255  * @param int   $link_id         ID of the link to update.
   255  * @param int   $link_id         ID of the link to update.
   256  * @param int[] $link_categories Array of link category IDs to add the link to.
   256  * @param int[] $link_categories Array of link category IDs to add the link to.
   257  */
   257  */
   258 function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
   258 function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
   259 	// If $link_categories isn't already an array, make it one:
   259 	// If $link_categories isn't already an array, make it one:
   260 	if ( ! is_array( $link_categories ) || 0 == count( $link_categories ) ) {
   260 	if ( ! is_array( $link_categories ) || 0 === count( $link_categories ) ) {
   261 		$link_categories = array( get_option( 'default_link_category' ) );
   261 		$link_categories = array( get_option( 'default_link_category' ) );
   262 	}
   262 	}
   263 
   263 
   264 	$link_categories = array_map( 'intval', $link_categories );
   264 	$link_categories = array_map( 'intval', $link_categories );
   265 	$link_categories = array_unique( $link_categories );
   265 	$link_categories = array_unique( $link_categories );
   284 
   284 
   285 	// Escape data pulled from DB.
   285 	// Escape data pulled from DB.
   286 	$link = wp_slash( $link );
   286 	$link = wp_slash( $link );
   287 
   287 
   288 	// Passed link category list overwrites existing category list if not empty.
   288 	// Passed link category list overwrites existing category list if not empty.
   289 	if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] ) && 0 != count( $linkdata['link_category'] ) ) {
   289 	if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] )
       
   290 		&& count( $linkdata['link_category'] ) > 0
       
   291 	) {
   290 		$link_cats = $linkdata['link_category'];
   292 		$link_cats = $linkdata['link_category'];
   291 	} else {
   293 	} else {
   292 		$link_cats = $link['link_category'];
   294 		$link_cats = $link['link_category'];
   293 	}
   295 	}
   294 
   296 
   307  *
   309  *
   308  * @global string $pagenow
   310  * @global string $pagenow
   309  */
   311  */
   310 function wp_link_manager_disabled_message() {
   312 function wp_link_manager_disabled_message() {
   311 	global $pagenow;
   313 	global $pagenow;
   312 	if ( 'link-manager.php' != $pagenow && 'link-add.php' != $pagenow && 'link.php' != $pagenow ) {
   314 
       
   315 	if ( ! in_array( $pagenow, array( 'link-manager.php', 'link-add.php', 'link.php' ), true ) ) {
   313 		return;
   316 		return;
   314 	}
   317 	}
   315 
   318 
   316 	add_filter( 'pre_option_link_manager_enabled', '__return_true', 100 );
   319 	add_filter( 'pre_option_link_manager_enabled', '__return_true', 100 );
   317 	$really_can_manage_links = current_user_can( 'manage_links' );
   320 	$really_can_manage_links = current_user_can( 'manage_links' );
   318 	remove_filter( 'pre_option_link_manager_enabled', '__return_true', 100 );
   321 	remove_filter( 'pre_option_link_manager_enabled', '__return_true', 100 );
   319 
   322 
   320 	if ( $really_can_manage_links && current_user_can( 'install_plugins' ) ) {
   323 	if ( $really_can_manage_links && current_user_can( 'install_plugins' ) ) {
   321 		$link = network_admin_url( 'plugin-install.php?tab=search&s=Link+Manager' );
   324 		$link = network_admin_url( 'plugin-install.php?tab=search&s=Link+Manager' );
   322 		/* translators: %s: URL of link manager plugin */
   325 		/* translators: %s: URL to install the Link Manager plugin. */
   323 		wp_die( sprintf( __( 'If you are looking to use the link manager, please install the <a href="%s">Link Manager</a> plugin.' ), $link ) );
   326 		wp_die( sprintf( __( 'If you are looking to use the link manager, please install the <a href="%s">Link Manager</a> plugin.' ), $link ) );
   324 	}
   327 	}
   325 
   328 
   326 	wp_die( __( 'Sorry, you are not allowed to edit the links for this site.' ) );
   329 	wp_die( __( 'Sorry, you are not allowed to edit the links for this site.' ) );
   327 }
   330 }