mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress Bookmark Administration API
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Administration
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * {@internal Missing Short Description}}
mp-wp_genesis 11 *
mp-wp_genesis 12 * @since unknown
mp-wp_genesis 13 *
mp-wp_genesis 14 * @return unknown
mp-wp_genesis 15 */
mp-wp_genesis 16 function add_link() {
mp-wp_genesis 17 return edit_link();
mp-wp_genesis 18 }
mp-wp_genesis 19
mp-wp_genesis 20 /**
mp-wp_genesis 21 * {@internal Missing Short Description}}
mp-wp_genesis 22 *
mp-wp_genesis 23 * @since unknown
mp-wp_genesis 24 *
mp-wp_genesis 25 * @param unknown_type $link_id
mp-wp_genesis 26 * @return unknown
mp-wp_genesis 27 */
mp-wp_genesis 28 function edit_link( $link_id = '' ) {
mp-wp_genesis 29 if (!current_user_can( 'manage_links' ))
mp-wp_genesis 30 wp_die( __( 'Cheatin’ uh?' ));
mp-wp_genesis 31
mp-wp_genesis 32 $_POST['link_url'] = wp_specialchars( $_POST['link_url'] );
mp-wp_genesis 33 $_POST['link_url'] = clean_url($_POST['link_url']);
mp-wp_genesis 34 $_POST['link_name'] = wp_specialchars( $_POST['link_name'] );
mp-wp_genesis 35 $_POST['link_image'] = wp_specialchars( $_POST['link_image'] );
mp-wp_genesis 36 $_POST['link_rss'] = clean_url($_POST['link_rss']);
mp-wp_genesis 37 if ( !isset($_POST['link_visible']) || 'N' != $_POST['link_visible'] )
mp-wp_genesis 38 $_POST['link_visible'] = 'Y';
mp-wp_genesis 39
mp-wp_genesis 40 if ( !empty( $link_id ) ) {
mp-wp_genesis 41 $_POST['link_id'] = $link_id;
mp-wp_genesis 42 return wp_update_link( $_POST);
mp-wp_genesis 43 } else {
mp-wp_genesis 44 return wp_insert_link( $_POST);
mp-wp_genesis 45 }
mp-wp_genesis 46 }
mp-wp_genesis 47
mp-wp_genesis 48 /**
mp-wp_genesis 49 * {@internal Missing Short Description}}
mp-wp_genesis 50 *
mp-wp_genesis 51 * @since unknown
mp-wp_genesis 52 *
mp-wp_genesis 53 * @return unknown
mp-wp_genesis 54 */
mp-wp_genesis 55 function get_default_link_to_edit() {
mp-wp_genesis 56 if ( isset( $_GET['linkurl'] ) )
mp-wp_genesis 57 $link->link_url = clean_url( $_GET['linkurl']);
mp-wp_genesis 58 else
mp-wp_genesis 59 $link->link_url = '';
mp-wp_genesis 60
mp-wp_genesis 61 if ( isset( $_GET['name'] ) )
mp-wp_genesis 62 $link->link_name = attribute_escape( $_GET['name']);
mp-wp_genesis 63 else
mp-wp_genesis 64 $link->link_name = '';
mp-wp_genesis 65
mp-wp_genesis 66 $link->link_visible = 'Y';
mp-wp_genesis 67
mp-wp_genesis 68 return $link;
mp-wp_genesis 69 }
mp-wp_genesis 70
mp-wp_genesis 71 /**
mp-wp_genesis 72 * {@internal Missing Short Description}}
mp-wp_genesis 73 *
mp-wp_genesis 74 * @since unknown
mp-wp_genesis 75 *
mp-wp_genesis 76 * @param unknown_type $link_id
mp-wp_genesis 77 * @return unknown
mp-wp_genesis 78 */
mp-wp_genesis 79 function wp_delete_link( $link_id ) {
mp-wp_genesis 80 global $wpdb;
mp-wp_genesis 81
mp-wp_genesis 82 do_action( 'delete_link', $link_id );
mp-wp_genesis 83
mp-wp_genesis 84 wp_delete_object_term_relationships( $link_id, 'link_category' );
mp-wp_genesis 85
mp-wp_genesis 86 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->links WHERE link_id = %d", $link_id ) );
mp-wp_genesis 87
mp-wp_genesis 88 do_action( 'deleted_link', $link_id );
mp-wp_genesis 89
mp-wp_genesis 90 clean_bookmark_cache( $link_id );
mp-wp_genesis 91
mp-wp_genesis 92 return true;
mp-wp_genesis 93 }
mp-wp_genesis 94
mp-wp_genesis 95 /**
mp-wp_genesis 96 * {@internal Missing Short Description}}
mp-wp_genesis 97 *
mp-wp_genesis 98 * @since unknown
mp-wp_genesis 99 *
mp-wp_genesis 100 * @param unknown_type $link_id
mp-wp_genesis 101 * @return unknown
mp-wp_genesis 102 */
mp-wp_genesis 103 function wp_get_link_cats( $link_id = 0 ) {
mp-wp_genesis 104
mp-wp_genesis 105 $cats = wp_get_object_terms( $link_id, 'link_category', 'fields=ids' );
mp-wp_genesis 106
mp-wp_genesis 107 return array_unique( $cats );
mp-wp_genesis 108 }
mp-wp_genesis 109
mp-wp_genesis 110 /**
mp-wp_genesis 111 * {@internal Missing Short Description}}
mp-wp_genesis 112 *
mp-wp_genesis 113 * @since unknown
mp-wp_genesis 114 *
mp-wp_genesis 115 * @param unknown_type $link_id
mp-wp_genesis 116 * @return unknown
mp-wp_genesis 117 */
mp-wp_genesis 118 function get_link_to_edit( $link_id ) {
mp-wp_genesis 119 return get_bookmark( $link_id, OBJECT, 'edit' );
mp-wp_genesis 120 }
mp-wp_genesis 121
mp-wp_genesis 122 /**
mp-wp_genesis 123 * {@internal Missing Short Description}}
mp-wp_genesis 124 *
mp-wp_genesis 125 * @since unknown
mp-wp_genesis 126 *
mp-wp_genesis 127 * @param unknown_type $linkdata
mp-wp_genesis 128 * @return unknown
mp-wp_genesis 129 */
mp-wp_genesis 130 function wp_insert_link( $linkdata, $wp_error = false ) {
mp-wp_genesis 131 global $wpdb, $current_user;
mp-wp_genesis 132
mp-wp_genesis 133 $defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 );
mp-wp_genesis 134
mp-wp_genesis 135 $linkdata = wp_parse_args( $linkdata, $defaults );
mp-wp_genesis 136 $linkdata = sanitize_bookmark( $linkdata, 'db' );
mp-wp_genesis 137
mp-wp_genesis 138 extract( stripslashes_deep( $linkdata ), EXTR_SKIP );
mp-wp_genesis 139
mp-wp_genesis 140 $update = false;
mp-wp_genesis 141
mp-wp_genesis 142 if ( !empty( $link_id ) )
mp-wp_genesis 143 $update = true;
mp-wp_genesis 144
mp-wp_genesis 145 if ( trim( $link_name ) == '' )
mp-wp_genesis 146 return 0;
mp-wp_genesis 147
mp-wp_genesis 148 if ( trim( $link_url ) == '' )
mp-wp_genesis 149 return 0;
mp-wp_genesis 150
mp-wp_genesis 151 if ( empty( $link_rating ) )
mp-wp_genesis 152 $link_rating = 0;
mp-wp_genesis 153
mp-wp_genesis 154 if ( empty( $link_image ) )
mp-wp_genesis 155 $link_image = '';
mp-wp_genesis 156
mp-wp_genesis 157 if ( empty( $link_target ) )
mp-wp_genesis 158 $link_target = '';
mp-wp_genesis 159
mp-wp_genesis 160 if ( empty( $link_visible ) )
mp-wp_genesis 161 $link_visible = 'Y';
mp-wp_genesis 162
mp-wp_genesis 163 if ( empty( $link_owner ) )
mp-wp_genesis 164 $link_owner = $current_user->id;
mp-wp_genesis 165
mp-wp_genesis 166 if ( empty( $link_notes ) )
mp-wp_genesis 167 $link_notes = '';
mp-wp_genesis 168
mp-wp_genesis 169 if ( empty( $link_description ) )
mp-wp_genesis 170 $link_description = '';
mp-wp_genesis 171
mp-wp_genesis 172 if ( empty( $link_rss ) )
mp-wp_genesis 173 $link_rss = '';
mp-wp_genesis 174
mp-wp_genesis 175 if ( empty( $link_rel ) )
mp-wp_genesis 176 $link_rel = '';
mp-wp_genesis 177
mp-wp_genesis 178 // Make sure we set a valid category
mp-wp_genesis 179 if ( ! isset( $link_category ) ||0 == count( $link_category ) || !is_array( $link_category ) ) {
mp-wp_genesis 180 $link_category = array( get_option( 'default_link_category' ) );
mp-wp_genesis 181 }
mp-wp_genesis 182
mp-wp_genesis 183 if ( $update ) {
mp-wp_genesis 184 if ( false === $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->links SET link_url = %s,
mp-wp_genesis 185 link_name = %s, link_image = %s, link_target = %s,
mp-wp_genesis 186 link_visible = %s, link_description = %s, link_rating = %s,
mp-wp_genesis 187 link_rel = %s, link_notes = %s, link_rss = %s
mp-wp_genesis 188 WHERE link_id = %s", $link_url, $link_name, $link_image, $link_target, $link_visible, $link_description, $link_rating, $link_rel, $link_notes, $link_rss, $link_id ) ) ) {
mp-wp_genesis 189 if ( $wp_error )
mp-wp_genesis 190 return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error );
mp-wp_genesis 191 else
mp-wp_genesis 192 return 0;
mp-wp_genesis 193 }
mp-wp_genesis 194 } else {
mp-wp_genesis 195 if ( false === $wpdb->query( $wpdb->prepare( "INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
mp-wp_genesis 196 $link_url,$link_name, $link_image, $link_target, $link_description, $link_visible, $link_owner, $link_rating, $link_rel, $link_notes, $link_rss ) ) ) {
mp-wp_genesis 197 if ( $wp_error )
mp-wp_genesis 198 return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error );
mp-wp_genesis 199 else
mp-wp_genesis 200 return 0;
mp-wp_genesis 201 }
mp-wp_genesis 202 $link_id = (int) $wpdb->insert_id;
mp-wp_genesis 203 }
mp-wp_genesis 204
mp-wp_genesis 205 wp_set_link_cats( $link_id, $link_category );
mp-wp_genesis 206
mp-wp_genesis 207 if ( $update )
mp-wp_genesis 208 do_action( 'edit_link', $link_id );
mp-wp_genesis 209 else
mp-wp_genesis 210 do_action( 'add_link', $link_id );
mp-wp_genesis 211
mp-wp_genesis 212 clean_bookmark_cache( $link_id );
mp-wp_genesis 213
mp-wp_genesis 214 return $link_id;
mp-wp_genesis 215 }
mp-wp_genesis 216
mp-wp_genesis 217 /**
mp-wp_genesis 218 * {@internal Missing Short Description}}
mp-wp_genesis 219 *
mp-wp_genesis 220 * @since unknown
mp-wp_genesis 221 *
mp-wp_genesis 222 * @param unknown_type $link_id
mp-wp_genesis 223 * @param unknown_type $link_categories
mp-wp_genesis 224 */
mp-wp_genesis 225 function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
mp-wp_genesis 226 // If $link_categories isn't already an array, make it one:
mp-wp_genesis 227 if ( !is_array( $link_categories ) || 0 == count( $link_categories ) )
mp-wp_genesis 228 $link_categories = array( get_option( 'default_link_category' ) );
mp-wp_genesis 229
mp-wp_genesis 230 $link_categories = array_map( 'intval', $link_categories );
mp-wp_genesis 231 $link_categories = array_unique( $link_categories );
mp-wp_genesis 232
mp-wp_genesis 233 wp_set_object_terms( $link_id, $link_categories, 'link_category' );
mp-wp_genesis 234
mp-wp_genesis 235 clean_bookmark_cache( $link_id );
mp-wp_genesis 236 } // wp_set_link_cats()
mp-wp_genesis 237
mp-wp_genesis 238 /**
mp-wp_genesis 239 * {@internal Missing Short Description}}
mp-wp_genesis 240 *
mp-wp_genesis 241 * @since unknown
mp-wp_genesis 242 *
mp-wp_genesis 243 * @param unknown_type $linkdata
mp-wp_genesis 244 * @return unknown
mp-wp_genesis 245 */
mp-wp_genesis 246 function wp_update_link( $linkdata ) {
mp-wp_genesis 247 $link_id = (int) $linkdata['link_id'];
mp-wp_genesis 248
mp-wp_genesis 249 $link = get_link( $link_id, ARRAY_A );
mp-wp_genesis 250
mp-wp_genesis 251 // Escape data pulled from DB.
mp-wp_genesis 252 $link = add_magic_quotes( $link );
mp-wp_genesis 253
mp-wp_genesis 254 // Passed link category list overwrites existing category list if not empty.
mp-wp_genesis 255 if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] )
mp-wp_genesis 256 && 0 != count( $linkdata['link_category'] ) )
mp-wp_genesis 257 $link_cats = $linkdata['link_category'];
mp-wp_genesis 258 else
mp-wp_genesis 259 $link_cats = $link['link_category'];
mp-wp_genesis 260
mp-wp_genesis 261 // Merge old and new fields with new fields overwriting old ones.
mp-wp_genesis 262 $linkdata = array_merge( $link, $linkdata );
mp-wp_genesis 263 $linkdata['link_category'] = $link_cats;
mp-wp_genesis 264
mp-wp_genesis 265 return wp_insert_link( $linkdata );
mp-wp_genesis 266 }
mp-wp_genesis 267
mp-wp_genesis 268 ?>