balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

bookmark.php (11713B)


      1 <?php
      2 /**
      3  * WordPress Bookmark Administration API
      4  *
      5  * @package WordPress
      6  * @subpackage Administration
      7  */
      8 
      9 /**
     10  * Add a link to using values provided in $_POST.
     11  *
     12  * @since 2.0.0
     13  *
     14  * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
     15  */
     16 function add_link() {
     17 	return edit_link();
     18 }
     19 
     20 /**
     21  * Updates or inserts a link using values provided in $_POST.
     22  *
     23  * @since 2.0.0
     24  *
     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.
     27  */
     28 function edit_link( $link_id = 0 ) {
     29 	if ( ! current_user_can( 'manage_links' ) ) {
     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 	}
     36 
     37 	$_POST['link_url']   = esc_html( $_POST['link_url'] );
     38 	$_POST['link_url']   = esc_url( $_POST['link_url'] );
     39 	$_POST['link_name']  = esc_html( $_POST['link_name'] );
     40 	$_POST['link_image'] = esc_html( $_POST['link_image'] );
     41 	$_POST['link_rss']   = esc_url( $_POST['link_rss'] );
     42 	if ( ! isset( $_POST['link_visible'] ) || 'N' !== $_POST['link_visible'] ) {
     43 		$_POST['link_visible'] = 'Y';
     44 	}
     45 
     46 	if ( ! empty( $link_id ) ) {
     47 		$_POST['link_id'] = $link_id;
     48 		return wp_update_link( $_POST );
     49 	} else {
     50 		return wp_insert_link( $_POST );
     51 	}
     52 }
     53 
     54 /**
     55  * Retrieves the default link for editing.
     56  *
     57  * @since 2.0.0
     58  *
     59  * @return stdClass Default link object.
     60  */
     61 function get_default_link_to_edit() {
     62 	$link = new stdClass;
     63 	if ( isset( $_GET['linkurl'] ) ) {
     64 		$link->link_url = esc_url( wp_unslash( $_GET['linkurl'] ) );
     65 	} else {
     66 		$link->link_url = '';
     67 	}
     68 
     69 	if ( isset( $_GET['name'] ) ) {
     70 		$link->link_name = esc_attr( wp_unslash( $_GET['name'] ) );
     71 	} else {
     72 		$link->link_name = '';
     73 	}
     74 
     75 	$link->link_visible = 'Y';
     76 
     77 	return $link;
     78 }
     79 
     80 /**
     81  * Deletes a specified link from the database.
     82  *
     83  * @since 2.0.0
     84  *
     85  * @global wpdb $wpdb WordPress database abstraction object.
     86  *
     87  * @param int $link_id ID of the link to delete
     88  * @return true Always true.
     89  */
     90 function wp_delete_link( $link_id ) {
     91 	global $wpdb;
     92 	/**
     93 	 * Fires before a link is deleted.
     94 	 *
     95 	 * @since 2.0.0
     96 	 *
     97 	 * @param int $link_id ID of the link to delete.
     98 	 */
     99 	do_action( 'delete_link', $link_id );
    100 
    101 	wp_delete_object_term_relationships( $link_id, 'link_category' );
    102 
    103 	$wpdb->delete( $wpdb->links, array( 'link_id' => $link_id ) );
    104 
    105 	/**
    106 	 * Fires after a link has been deleted.
    107 	 *
    108 	 * @since 2.2.0
    109 	 *
    110 	 * @param int $link_id ID of the deleted link.
    111 	 */
    112 	do_action( 'deleted_link', $link_id );
    113 
    114 	clean_bookmark_cache( $link_id );
    115 
    116 	return true;
    117 }
    118 
    119 /**
    120  * Retrieves the link category IDs associated with the link specified.
    121  *
    122  * @since 2.1.0
    123  *
    124  * @param int $link_id Link ID to look up.
    125  * @return int[] The IDs of the requested link's categories.
    126  */
    127 function wp_get_link_cats( $link_id = 0 ) {
    128 	$cats = wp_get_object_terms( $link_id, 'link_category', array( 'fields' => 'ids' ) );
    129 	return array_unique( $cats );
    130 }
    131 
    132 /**
    133  * Retrieves link data based on its ID.
    134  *
    135  * @since 2.0.0
    136  *
    137  * @param int|stdClass $link Link ID or object to retrieve.
    138  * @return object Link object for editing.
    139  */
    140 function get_link_to_edit( $link ) {
    141 	return get_bookmark( $link, OBJECT, 'edit' );
    142 }
    143 
    144 /**
    145  * Inserts a link into the database, or updates an existing link.
    146  *
    147  * Runs all the necessary sanitizing, provides default values if arguments are missing,
    148  * and finally saves the link.
    149  *
    150  * @since 2.0.0
    151  *
    152  * @global wpdb $wpdb WordPress database abstraction object.
    153  *
    154  * @param array $linkdata {
    155  *     Elements that make up the link to insert.
    156  *
    157  *     @type int    $link_id          Optional. The ID of the existing link if updating.
    158  *     @type string $link_url         The URL the link points to.
    159  *     @type string $link_name        The title of the link.
    160  *     @type string $link_image       Optional. A URL of an image.
    161  *     @type string $link_target      Optional. The target element for the anchor tag.
    162  *     @type string $link_description Optional. A short description of the link.
    163  *     @type string $link_visible     Optional. 'Y' means visible, anything else means not.
    164  *     @type int    $link_owner       Optional. A user ID.
    165  *     @type int    $link_rating      Optional. A rating for the link.
    166  *     @type string $link_updated     Optional. When the link was last updated.
    167  *     @type string $link_rel         Optional. A relationship of the link to you.
    168  *     @type string $link_notes       Optional. An extended description of or notes on the link.
    169  *     @type string $link_rss         Optional. A URL of an associated RSS feed.
    170  *     @type int    $link_category    Optional. The term ID of the link category.
    171  *                                    If empty, uses default link category.
    172  * }
    173  * @param bool  $wp_error Optional. Whether to return a WP_Error object on failure. Default false.
    174  * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
    175  */
    176 function wp_insert_link( $linkdata, $wp_error = false ) {
    177 	global $wpdb;
    178 
    179 	$defaults = array(
    180 		'link_id'     => 0,
    181 		'link_name'   => '',
    182 		'link_url'    => '',
    183 		'link_rating' => 0,
    184 	);
    185 
    186 	$parsed_args = wp_parse_args( $linkdata, $defaults );
    187 	$parsed_args = wp_unslash( sanitize_bookmark( $parsed_args, 'db' ) );
    188 
    189 	$link_id   = $parsed_args['link_id'];
    190 	$link_name = $parsed_args['link_name'];
    191 	$link_url  = $parsed_args['link_url'];
    192 
    193 	$update = false;
    194 	if ( ! empty( $link_id ) ) {
    195 		$update = true;
    196 	}
    197 
    198 	if ( '' === trim( $link_name ) ) {
    199 		if ( '' !== trim( $link_url ) ) {
    200 			$link_name = $link_url;
    201 		} else {
    202 			return 0;
    203 		}
    204 	}
    205 
    206 	if ( '' === trim( $link_url ) ) {
    207 		return 0;
    208 	}
    209 
    210 	$link_rating      = ( ! empty( $parsed_args['link_rating'] ) ) ? $parsed_args['link_rating'] : 0;
    211 	$link_image       = ( ! empty( $parsed_args['link_image'] ) ) ? $parsed_args['link_image'] : '';
    212 	$link_target      = ( ! empty( $parsed_args['link_target'] ) ) ? $parsed_args['link_target'] : '';
    213 	$link_visible     = ( ! empty( $parsed_args['link_visible'] ) ) ? $parsed_args['link_visible'] : 'Y';
    214 	$link_owner       = ( ! empty( $parsed_args['link_owner'] ) ) ? $parsed_args['link_owner'] : get_current_user_id();
    215 	$link_notes       = ( ! empty( $parsed_args['link_notes'] ) ) ? $parsed_args['link_notes'] : '';
    216 	$link_description = ( ! empty( $parsed_args['link_description'] ) ) ? $parsed_args['link_description'] : '';
    217 	$link_rss         = ( ! empty( $parsed_args['link_rss'] ) ) ? $parsed_args['link_rss'] : '';
    218 	$link_rel         = ( ! empty( $parsed_args['link_rel'] ) ) ? $parsed_args['link_rel'] : '';
    219 	$link_category    = ( ! empty( $parsed_args['link_category'] ) ) ? $parsed_args['link_category'] : array();
    220 
    221 	// Make sure we set a valid category.
    222 	if ( ! is_array( $link_category ) || 0 === count( $link_category ) ) {
    223 		$link_category = array( get_option( 'default_link_category' ) );
    224 	}
    225 
    226 	if ( $update ) {
    227 		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' ) ) ) {
    228 			if ( $wp_error ) {
    229 				return new WP_Error( 'db_update_error', __( 'Could not update link in the database.' ), $wpdb->last_error );
    230 			} else {
    231 				return 0;
    232 			}
    233 		}
    234 	} else {
    235 		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' ) ) ) {
    236 			if ( $wp_error ) {
    237 				return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database.' ), $wpdb->last_error );
    238 			} else {
    239 				return 0;
    240 			}
    241 		}
    242 		$link_id = (int) $wpdb->insert_id;
    243 	}
    244 
    245 	wp_set_link_cats( $link_id, $link_category );
    246 
    247 	if ( $update ) {
    248 		/**
    249 		 * Fires after a link was updated in the database.
    250 		 *
    251 		 * @since 2.0.0
    252 		 *
    253 		 * @param int $link_id ID of the link that was updated.
    254 		 */
    255 		do_action( 'edit_link', $link_id );
    256 	} else {
    257 		/**
    258 		 * Fires after a link was added to the database.
    259 		 *
    260 		 * @since 2.0.0
    261 		 *
    262 		 * @param int $link_id ID of the link that was added.
    263 		 */
    264 		do_action( 'add_link', $link_id );
    265 	}
    266 	clean_bookmark_cache( $link_id );
    267 
    268 	return $link_id;
    269 }
    270 
    271 /**
    272  * Update link with the specified link categories.
    273  *
    274  * @since 2.1.0
    275  *
    276  * @param int   $link_id         ID of the link to update.
    277  * @param int[] $link_categories Array of link category IDs to add the link to.
    278  */
    279 function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
    280 	// If $link_categories isn't already an array, make it one:
    281 	if ( ! is_array( $link_categories ) || 0 === count( $link_categories ) ) {
    282 		$link_categories = array( get_option( 'default_link_category' ) );
    283 	}
    284 
    285 	$link_categories = array_map( 'intval', $link_categories );
    286 	$link_categories = array_unique( $link_categories );
    287 
    288 	wp_set_object_terms( $link_id, $link_categories, 'link_category' );
    289 
    290 	clean_bookmark_cache( $link_id );
    291 }
    292 
    293 /**
    294  * Updates a link in the database.
    295  *
    296  * @since 2.0.0
    297  *
    298  * @param array $linkdata Link data to update. See wp_insert_link() for accepted arguments.
    299  * @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success.
    300  */
    301 function wp_update_link( $linkdata ) {
    302 	$link_id = (int) $linkdata['link_id'];
    303 
    304 	$link = get_bookmark( $link_id, ARRAY_A );
    305 
    306 	// Escape data pulled from DB.
    307 	$link = wp_slash( $link );
    308 
    309 	// Passed link category list overwrites existing category list if not empty.
    310 	if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] )
    311 		&& count( $linkdata['link_category'] ) > 0
    312 	) {
    313 		$link_cats = $linkdata['link_category'];
    314 	} else {
    315 		$link_cats = $link['link_category'];
    316 	}
    317 
    318 	// Merge old and new fields with new fields overwriting old ones.
    319 	$linkdata                  = array_merge( $link, $linkdata );
    320 	$linkdata['link_category'] = $link_cats;
    321 
    322 	return wp_insert_link( $linkdata );
    323 }
    324 
    325 /**
    326  * Outputs the 'disabled' message for the WordPress Link Manager.
    327  *
    328  * @since 3.5.0
    329  * @access private
    330  *
    331  * @global string $pagenow
    332  */
    333 function wp_link_manager_disabled_message() {
    334 	global $pagenow;
    335 
    336 	if ( ! in_array( $pagenow, array( 'link-manager.php', 'link-add.php', 'link.php' ), true ) ) {
    337 		return;
    338 	}
    339 
    340 	add_filter( 'pre_option_link_manager_enabled', '__return_true', 100 );
    341 	$really_can_manage_links = current_user_can( 'manage_links' );
    342 	remove_filter( 'pre_option_link_manager_enabled', '__return_true', 100 );
    343 
    344 	if ( $really_can_manage_links ) {
    345 		$plugins = get_plugins();
    346 
    347 		if ( empty( $plugins['link-manager/link-manager.php'] ) ) {
    348 			if ( current_user_can( 'install_plugins' ) ) {
    349 				$install_url = wp_nonce_url(
    350 					self_admin_url( 'update.php?action=install-plugin&plugin=link-manager' ),
    351 					'install-plugin_link-manager'
    352 				);
    353 
    354 				wp_die(
    355 					sprintf(
    356 						/* translators: %s: A link to install the Link Manager plugin. */
    357 						__( 'If you are looking to use the link manager, please install the <a href="%s">Link Manager plugin</a>.' ),
    358 						esc_url( $install_url )
    359 					)
    360 				);
    361 			}
    362 		} elseif ( is_plugin_inactive( 'link-manager/link-manager.php' ) ) {
    363 			if ( current_user_can( 'activate_plugins' ) ) {
    364 				$activate_url = wp_nonce_url(
    365 					self_admin_url( 'plugins.php?action=activate&plugin=link-manager/link-manager.php' ),
    366 					'activate-plugin_link-manager/link-manager.php'
    367 				);
    368 
    369 				wp_die(
    370 					sprintf(
    371 						/* translators: %s: A link to activate the Link Manager plugin. */
    372 						__( 'Please activate the <a href="%s">Link Manager plugin</a> to use the link manager.' ),
    373 						esc_url( $activate_url )
    374 					)
    375 				);
    376 			}
    377 		}
    378 	}
    379 
    380 	wp_die( __( 'Sorry, you are not allowed to edit the links for this site.' ) );
    381 }