25 * @param int $link_id Optional. ID of the link to edit. |
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. |
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?' ) ); |
30 wp_die( __( 'Cheatin’ uh?' ), 403 ); |
31 |
31 |
32 $_POST['link_url'] = esc_html( $_POST['link_url'] ); |
32 $_POST['link_url'] = esc_html( $_POST['link_url'] ); |
33 $_POST['link_url'] = esc_url($_POST['link_url']); |
33 $_POST['link_url'] = esc_url($_POST['link_url']); |
34 $_POST['link_name'] = esc_html( $_POST['link_name'] ); |
34 $_POST['link_name'] = esc_html( $_POST['link_name'] ); |
35 $_POST['link_image'] = esc_html( $_POST['link_image'] ); |
35 $_POST['link_image'] = esc_html( $_POST['link_image'] ); |
144 function wp_insert_link( $linkdata, $wp_error = false ) { |
144 function wp_insert_link( $linkdata, $wp_error = false ) { |
145 global $wpdb; |
145 global $wpdb; |
146 |
146 |
147 $defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 ); |
147 $defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 ); |
148 |
148 |
149 $linkdata = wp_parse_args( $linkdata, $defaults ); |
149 $args = wp_parse_args( $linkdata, $defaults ); |
150 $linkdata = sanitize_bookmark( $linkdata, 'db' ); |
150 $r = wp_unslash( sanitize_bookmark( $args, 'db' ) ); |
151 |
151 |
152 extract( wp_unslash( $linkdata ), EXTR_SKIP ); |
152 $link_id = $r['link_id']; |
|
153 $link_name = $r['link_name']; |
|
154 $link_url = $r['link_url']; |
153 |
155 |
154 $update = false; |
156 $update = false; |
155 |
157 if ( ! empty( $link_id ) ) { |
156 if ( !empty( $link_id ) ) |
|
157 $update = true; |
158 $update = true; |
|
159 } |
158 |
160 |
159 if ( trim( $link_name ) == '' ) { |
161 if ( trim( $link_name ) == '' ) { |
160 if ( trim( $link_url ) != '' ) { |
162 if ( trim( $link_url ) != '' ) { |
161 $link_name = $link_url; |
163 $link_name = $link_url; |
162 } else { |
164 } else { |
163 return 0; |
165 return 0; |
164 } |
166 } |
165 } |
167 } |
166 |
168 |
167 if ( trim( $link_url ) == '' ) |
169 if ( trim( $link_url ) == '' ) { |
168 return 0; |
170 return 0; |
169 |
171 } |
170 if ( empty( $link_rating ) ) |
172 |
171 $link_rating = 0; |
173 $link_rating = ( ! empty( $r['link_rating'] ) ) ? $r['link_rating'] : 0; |
172 |
174 $link_image = ( ! empty( $r['link_image'] ) ) ? $r['link_image'] : ''; |
173 if ( empty( $link_image ) ) |
175 $link_target = ( ! empty( $r['link_target'] ) ) ? $r['link_target'] : ''; |
174 $link_image = ''; |
176 $link_visible = ( ! empty( $r['link_visible'] ) ) ? $r['link_visible'] : 'Y'; |
175 |
177 $link_owner = ( ! empty( $r['link_owner'] ) ) ? $r['link_owner'] : get_current_user_id(); |
176 if ( empty( $link_target ) ) |
178 $link_notes = ( ! empty( $r['link_notes'] ) ) ? $r['link_notes'] : ''; |
177 $link_target = ''; |
179 $link_description = ( ! empty( $r['link_description'] ) ) ? $r['link_description'] : ''; |
178 |
180 $link_rss = ( ! empty( $r['link_rss'] ) ) ? $r['link_rss'] : ''; |
179 if ( empty( $link_visible ) ) |
181 $link_rel = ( ! empty( $r['link_rel'] ) ) ? $r['link_rel'] : ''; |
180 $link_visible = 'Y'; |
182 $link_category = ( ! empty( $r['link_category'] ) ) ? $r['link_category'] : array(); |
181 |
|
182 if ( empty( $link_owner ) ) |
|
183 $link_owner = get_current_user_id(); |
|
184 |
|
185 if ( empty( $link_notes ) ) |
|
186 $link_notes = ''; |
|
187 |
|
188 if ( empty( $link_description ) ) |
|
189 $link_description = ''; |
|
190 |
|
191 if ( empty( $link_rss ) ) |
|
192 $link_rss = ''; |
|
193 |
|
194 if ( empty( $link_rel ) ) |
|
195 $link_rel = ''; |
|
196 |
183 |
197 // Make sure we set a valid category |
184 // Make sure we set a valid category |
198 if ( ! isset( $link_category ) || 0 == count( $link_category ) || !is_array( $link_category ) ) { |
185 if ( ! is_array( $link_category ) || 0 == count( $link_category ) ) { |
199 $link_category = array( get_option( 'default_link_category' ) ); |
186 $link_category = array( get_option( 'default_link_category' ) ); |
200 } |
187 } |
201 |
188 |
202 if ( $update ) { |
189 if ( $update ) { |
203 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') ) ) { |
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' ) ) ) { |
204 if ( $wp_error ) |
191 if ( $wp_error ) { |
205 return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error ); |
192 return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error ); |
206 else |
193 } else { |
207 return 0; |
194 return 0; |
|
195 } |
208 } |
196 } |
209 } else { |
197 } else { |
210 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 ( 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' ) ) ) { |
211 if ( $wp_error ) |
199 if ( $wp_error ) { |
212 return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error ); |
200 return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error ); |
213 else |
201 } else { |
214 return 0; |
202 return 0; |
|
203 } |
215 } |
204 } |
216 $link_id = (int) $wpdb->insert_id; |
205 $link_id = (int) $wpdb->insert_id; |
217 } |
206 } |
218 |
207 |
219 wp_set_link_cats( $link_id, $link_category ); |
208 wp_set_link_cats( $link_id, $link_category ); |