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