PageRenderTime 224ms CodeModel.GetById 131ms app.highlight 10ms RepoModel.GetById 80ms app.codeStats 0ms

/wp-admin/includes/bookmark.php

https://bitbucket.org/Wallynm/iptb
PHP | 269 lines | 134 code | 53 blank | 82 comment | 43 complexity | d74583b139fffa1ee61614cdf9a4cbf3 MD5 | raw file
  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 */
 16function add_link() {
 17	return edit_link();
 18}
 19
 20/**
 21 * Update or insert 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.
 26 * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
 27 */
 28function edit_link( $link_id = 0 ) {
 29	if ( !current_user_can( 'manage_links' ) )
 30		wp_die( __( 'Cheatin&#8217; uh?' ) );
 31
 32	$_POST['link_url'] = esc_html( $_POST['link_url'] );
 33	$_POST['link_url'] = esc_url($_POST['link_url']);
 34	$_POST['link_name'] = esc_html( $_POST['link_name'] );
 35	$_POST['link_image'] = esc_html( $_POST['link_image'] );
 36	$_POST['link_rss'] = esc_url($_POST['link_rss']);
 37	if ( !isset($_POST['link_visible']) || 'N' != $_POST['link_visible'] )
 38		$_POST['link_visible'] = 'Y';
 39
 40	if ( !empty( $link_id ) ) {
 41		$_POST['link_id'] = $link_id;
 42		return wp_update_link( $_POST );
 43	} else {
 44		return wp_insert_link( $_POST );
 45	}
 46}
 47
 48/**
 49 * Retrieve the default link for editing.
 50 *
 51 * @since 2.0.0
 52 *
 53 * @return object Default link
 54 */
 55function get_default_link_to_edit() {
 56	if ( isset( $_GET['linkurl'] ) )
 57		$link->link_url = esc_url( $_GET['linkurl'] );
 58	else
 59		$link->link_url = '';
 60
 61	if ( isset( $_GET['name'] ) )
 62		$link->link_name = esc_attr( $_GET['name'] );
 63	else
 64		$link->link_name = '';
 65
 66	$link->link_visible = 'Y';
 67
 68	return $link;
 69}
 70
 71/**
 72 * Delete link specified from database
 73 *
 74 * @since 2.0.0
 75 *
 76 * @param int $link_id ID of the link to delete
 77 * @return bool True
 78 */
 79function wp_delete_link( $link_id ) {
 80	global $wpdb;
 81
 82	do_action( 'delete_link', $link_id );
 83
 84	wp_delete_object_term_relationships( $link_id, 'link_category' );
 85
 86	$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->links WHERE link_id = %d", $link_id ) );
 87
 88	do_action( 'deleted_link', $link_id );
 89
 90	clean_bookmark_cache( $link_id );
 91
 92	return true;
 93}
 94
 95/**
 96 * Retrieves the link categories associated with the link specified.
 97 *
 98 * @since 2.1.0
 99 *
100 * @param int $link_id Link ID to look up
101 * @return array The requested link's categories
102 */
103function wp_get_link_cats( $link_id = 0 ) {
104
105	$cats = wp_get_object_terms( $link_id, 'link_category', array('fields' => 'ids') );
106
107	return array_unique( $cats );
108}
109
110/**
111 * Retrieve link data based on ID.
112 *
113 * @since 2.0.0
114 *
115 * @param int $link_id ID of link to retrieve
116 * @return object Link for editing
117 */
118function get_link_to_edit( $link_id ) {
119	return get_bookmark( $link_id, OBJECT, 'edit' );
120}
121
122/**
123 * This function inserts/updates links into/in the database.
124 *
125 * @since 2.0.0
126 *
127 * @param array $linkdata Elements that make up the link to insert.
128 * @param bool $wp_error Optional. If true return WP_Error object on failure.
129 * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
130 */
131function wp_insert_link( $linkdata, $wp_error = false ) {
132	global $wpdb;
133
134	$defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 );
135
136	$linkdata = wp_parse_args( $linkdata, $defaults );
137	$linkdata = sanitize_bookmark( $linkdata, 'db' );
138
139	extract( stripslashes_deep( $linkdata ), EXTR_SKIP );
140
141	$update = false;
142
143	if ( !empty( $link_id ) )
144		$update = true;
145
146	if ( trim( $link_name ) == '' ) {
147		if ( trim( $link_url ) != '' ) {
148			$link_name = $link_url;
149		} else {
150			return 0;
151		}
152	}
153
154	if ( trim( $link_url ) == '' )
155		return 0;
156
157	if ( empty( $link_rating ) )
158		$link_rating = 0;
159
160	if ( empty( $link_image ) )
161		$link_image = '';
162
163	if ( empty( $link_target ) )
164		$link_target = '';
165
166	if ( empty( $link_visible ) )
167		$link_visible = 'Y';
168
169	if ( empty( $link_owner ) )
170		$link_owner = get_current_user_id();
171
172	if ( empty( $link_notes ) )
173		$link_notes = '';
174
175	if ( empty( $link_description ) )
176		$link_description = '';
177
178	if ( empty( $link_rss ) )
179		$link_rss = '';
180
181	if ( empty( $link_rel ) )
182		$link_rel = '';
183
184	// Make sure we set a valid category
185	if ( ! isset( $link_category ) || 0 == count( $link_category ) || !is_array( $link_category ) ) {
186		$link_category = array( get_option( 'default_link_category' ) );
187	}
188
189	if ( $update ) {
190		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') ) ) {
191			if ( $wp_error )
192				return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error );
193			else
194				return 0;
195		}
196	} else {
197		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') ) ) {
198			if ( $wp_error )
199				return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error );
200			else
201				return 0;
202		}
203		$link_id = (int) $wpdb->insert_id;
204	}
205
206	wp_set_link_cats( $link_id, $link_category );
207
208	if ( $update )
209		do_action( 'edit_link', $link_id );
210	else
211		do_action( 'add_link', $link_id );
212
213	clean_bookmark_cache( $link_id );
214
215	return $link_id;
216}
217
218/**
219 * Update link with the specified link categories.
220 *
221 * @since 2.1.0
222 *
223 * @param int $link_id ID of link to update
224 * @param array $link_categories Array of categories to
225 */
226function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
227	// If $link_categories isn't already an array, make it one:
228	if ( !is_array( $link_categories ) || 0 == count( $link_categories ) )
229		$link_categories = array( get_option( 'default_link_category' ) );
230
231	$link_categories = array_map( 'intval', $link_categories );
232	$link_categories = array_unique( $link_categories );
233
234	wp_set_object_terms( $link_id, $link_categories, 'link_category' );
235
236	clean_bookmark_cache( $link_id );
237}
238
239/**
240 * Update a link in the database.
241 *
242 * @since 2.0.0
243 *
244 * @param array $linkdata Link data to update.
245 * @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success.
246 */
247function wp_update_link( $linkdata ) {
248	$link_id = (int) $linkdata['link_id'];
249
250	$link = get_bookmark( $link_id, ARRAY_A );
251
252	// Escape data pulled from DB.
253	$link = add_magic_quotes( $link );
254
255	// Passed link category list overwrites existing category list if not empty.
256	if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] )
257			 && 0 != count( $linkdata['link_category'] ) )
258		$link_cats = $linkdata['link_category'];
259	else
260		$link_cats = $link['link_category'];
261
262	// Merge old and new fields with new fields overwriting old ones.
263	$linkdata = array_merge( $link, $linkdata );
264	$linkdata['link_category'] = $link_cats;
265
266	return wp_insert_link( $linkdata );
267}
268
269?>