author | ymh <ymh.work@gmail.com> |
Tue, 15 Oct 2019 15:48:13 +0200 | |
changeset 13 | d255fe9cd479 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 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 |
/** |
|
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_html( $_POST['link_url'] ); |
38 |
$_POST['link_url'] = esc_url( $_POST['link_url'] ); |
|
39 |
$_POST['link_name'] = esc_html( $_POST['link_name'] ); |
|
0 | 40 |
$_POST['link_image'] = esc_html( $_POST['link_image'] ); |
9 | 41 |
$_POST['link_rss'] = esc_url( $_POST['link_rss'] ); |
42 |
if ( ! isset( $_POST['link_visible'] ) || 'N' != $_POST['link_visible'] ) { |
|
0 | 43 |
$_POST['link_visible'] = 'Y'; |
9 | 44 |
} |
0 | 45 |
|
9 | 46 |
if ( ! empty( $link_id ) ) { |
0 | 47 |
$_POST['link_id'] = $link_id; |
48 |
return wp_update_link( $_POST ); |
|
49 |
} else { |
|
50 |
return wp_insert_link( $_POST ); |
|
51 |
} |
|
52 |
} |
|
53 |
||
54 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
* Retrieves the default link for editing. |
0 | 56 |
* |
57 |
* @since 2.0.0 |
|
58 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
* @return stdClass Default link object. |
0 | 60 |
*/ |
61 |
function get_default_link_to_edit() { |
|
62 |
$link = new stdClass; |
|
9 | 63 |
if ( isset( $_GET['linkurl'] ) ) { |
0 | 64 |
$link->link_url = esc_url( wp_unslash( $_GET['linkurl'] ) ); |
9 | 65 |
} else { |
0 | 66 |
$link->link_url = ''; |
9 | 67 |
} |
0 | 68 |
|
9 | 69 |
if ( isset( $_GET['name'] ) ) { |
0 | 70 |
$link->link_name = esc_attr( wp_unslash( $_GET['name'] ) ); |
9 | 71 |
} else { |
0 | 72 |
$link->link_name = ''; |
9 | 73 |
} |
0 | 74 |
|
75 |
$link->link_visible = 'Y'; |
|
76 |
||
77 |
return $link; |
|
78 |
} |
|
79 |
||
80 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
* Deletes a specified link from the database. |
0 | 82 |
* |
83 |
* @since 2.0.0 |
|
84 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
* |
0 | 87 |
* @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
|
88 |
* @return true Always true. |
0 | 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 ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
|
0 | 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 categories associated with the link specified. |
|
121 |
* |
|
122 |
* @since 2.1.0 |
|
123 |
* |
|
124 |
* @param int $link_id Link ID to look up |
|
125 |
* @return array The requested link's categories |
|
126 |
*/ |
|
127 |
function wp_get_link_cats( $link_id = 0 ) { |
|
9 | 128 |
$cats = wp_get_object_terms( $link_id, 'link_category', array( 'fields' => 'ids' ) ); |
0 | 129 |
return array_unique( $cats ); |
130 |
} |
|
131 |
||
132 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
133 |
* Retrieves link data based on its ID. |
0 | 134 |
* |
135 |
* @since 2.0.0 |
|
136 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
* @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
|
138 |
* @return object Link object for editing. |
0 | 139 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
function get_link_to_edit( $link ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
return get_bookmark( $link, OBJECT, 'edit' ); |
0 | 142 |
} |
143 |
||
144 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
* Inserts/updates links into/in the database. |
0 | 146 |
* |
147 |
* @since 2.0.0 |
|
148 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
* |
0 | 151 |
* @param array $linkdata Elements that make up the link to insert. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
* @param bool $wp_error Optional. Whether to return a WP_Error object on failure. Default false. |
0 | 153 |
* @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success. |
154 |
*/ |
|
155 |
function wp_insert_link( $linkdata, $wp_error = false ) { |
|
156 |
global $wpdb; |
|
157 |
||
9 | 158 |
$defaults = array( |
159 |
'link_id' => 0, |
|
160 |
'link_name' => '', |
|
161 |
'link_url' => '', |
|
162 |
'link_rating' => 0, |
|
163 |
); |
|
0 | 164 |
|
5 | 165 |
$args = wp_parse_args( $linkdata, $defaults ); |
9 | 166 |
$r = wp_unslash( sanitize_bookmark( $args, 'db' ) ); |
0 | 167 |
|
5 | 168 |
$link_id = $r['link_id']; |
169 |
$link_name = $r['link_name']; |
|
170 |
$link_url = $r['link_url']; |
|
0 | 171 |
|
172 |
$update = false; |
|
5 | 173 |
if ( ! empty( $link_id ) ) { |
0 | 174 |
$update = true; |
5 | 175 |
} |
0 | 176 |
|
177 |
if ( trim( $link_name ) == '' ) { |
|
178 |
if ( trim( $link_url ) != '' ) { |
|
179 |
$link_name = $link_url; |
|
180 |
} else { |
|
181 |
return 0; |
|
182 |
} |
|
183 |
} |
|
184 |
||
5 | 185 |
if ( trim( $link_url ) == '' ) { |
0 | 186 |
return 0; |
5 | 187 |
} |
0 | 188 |
|
5 | 189 |
$link_rating = ( ! empty( $r['link_rating'] ) ) ? $r['link_rating'] : 0; |
190 |
$link_image = ( ! empty( $r['link_image'] ) ) ? $r['link_image'] : ''; |
|
191 |
$link_target = ( ! empty( $r['link_target'] ) ) ? $r['link_target'] : ''; |
|
192 |
$link_visible = ( ! empty( $r['link_visible'] ) ) ? $r['link_visible'] : 'Y'; |
|
193 |
$link_owner = ( ! empty( $r['link_owner'] ) ) ? $r['link_owner'] : get_current_user_id(); |
|
194 |
$link_notes = ( ! empty( $r['link_notes'] ) ) ? $r['link_notes'] : ''; |
|
195 |
$link_description = ( ! empty( $r['link_description'] ) ) ? $r['link_description'] : ''; |
|
196 |
$link_rss = ( ! empty( $r['link_rss'] ) ) ? $r['link_rss'] : ''; |
|
197 |
$link_rel = ( ! empty( $r['link_rel'] ) ) ? $r['link_rel'] : ''; |
|
198 |
$link_category = ( ! empty( $r['link_category'] ) ) ? $r['link_category'] : array(); |
|
0 | 199 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
// Make sure we set a valid category. |
5 | 201 |
if ( ! is_array( $link_category ) || 0 == count( $link_category ) ) { |
0 | 202 |
$link_category = array( get_option( 'default_link_category' ) ); |
203 |
} |
|
204 |
||
205 |
if ( $update ) { |
|
5 | 206 |
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' ) ) ) { |
207 |
if ( $wp_error ) { |
|
0 | 208 |
return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error ); |
5 | 209 |
} else { |
0 | 210 |
return 0; |
5 | 211 |
} |
0 | 212 |
} |
213 |
} else { |
|
5 | 214 |
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' ) ) ) { |
215 |
if ( $wp_error ) { |
|
0 | 216 |
return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error ); |
5 | 217 |
} else { |
0 | 218 |
return 0; |
5 | 219 |
} |
0 | 220 |
} |
221 |
$link_id = (int) $wpdb->insert_id; |
|
222 |
} |
|
223 |
||
224 |
wp_set_link_cats( $link_id, $link_category ); |
|
225 |
||
226 |
if ( $update ) { |
|
227 |
/** |
|
228 |
* Fires after a link was updated in the database. |
|
229 |
* |
|
230 |
* @since 2.0.0 |
|
231 |
* |
|
232 |
* @param int $link_id ID of the link that was updated. |
|
233 |
*/ |
|
234 |
do_action( 'edit_link', $link_id ); |
|
235 |
} else { |
|
236 |
/** |
|
237 |
* Fires after a link was added to the database. |
|
238 |
* |
|
239 |
* @since 2.0.0 |
|
240 |
* |
|
241 |
* @param int $link_id ID of the link that was added. |
|
242 |
*/ |
|
243 |
do_action( 'add_link', $link_id ); |
|
244 |
} |
|
245 |
clean_bookmark_cache( $link_id ); |
|
246 |
||
247 |
return $link_id; |
|
248 |
} |
|
249 |
||
250 |
/** |
|
251 |
* Update link with the specified link categories. |
|
252 |
* |
|
253 |
* @since 2.1.0 |
|
254 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
* @param int $link_id ID of the link to update. |
9 | 256 |
* @param int[] $link_categories Array of link category IDs to add the link to. |
0 | 257 |
*/ |
258 |
function wp_set_link_cats( $link_id = 0, $link_categories = array() ) { |
|
259 |
// If $link_categories isn't already an array, make it one: |
|
9 | 260 |
if ( ! is_array( $link_categories ) || 0 == count( $link_categories ) ) { |
0 | 261 |
$link_categories = array( get_option( 'default_link_category' ) ); |
9 | 262 |
} |
0 | 263 |
|
264 |
$link_categories = array_map( 'intval', $link_categories ); |
|
265 |
$link_categories = array_unique( $link_categories ); |
|
266 |
||
267 |
wp_set_object_terms( $link_id, $link_categories, 'link_category' ); |
|
268 |
||
269 |
clean_bookmark_cache( $link_id ); |
|
270 |
} |
|
271 |
||
272 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
273 |
* Updates a link in the database. |
0 | 274 |
* |
275 |
* @since 2.0.0 |
|
276 |
* |
|
277 |
* @param array $linkdata Link data to update. |
|
278 |
* @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success. |
|
279 |
*/ |
|
280 |
function wp_update_link( $linkdata ) { |
|
281 |
$link_id = (int) $linkdata['link_id']; |
|
282 |
||
283 |
$link = get_bookmark( $link_id, ARRAY_A ); |
|
284 |
||
285 |
// Escape data pulled from DB. |
|
286 |
$link = wp_slash( $link ); |
|
287 |
||
288 |
// Passed link category list overwrites existing category list if not empty. |
|
9 | 289 |
if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] ) && 0 != count( $linkdata['link_category'] ) ) { |
0 | 290 |
$link_cats = $linkdata['link_category']; |
9 | 291 |
} else { |
0 | 292 |
$link_cats = $link['link_category']; |
9 | 293 |
} |
0 | 294 |
|
295 |
// Merge old and new fields with new fields overwriting old ones. |
|
9 | 296 |
$linkdata = array_merge( $link, $linkdata ); |
0 | 297 |
$linkdata['link_category'] = $link_cats; |
298 |
||
299 |
return wp_insert_link( $linkdata ); |
|
300 |
} |
|
301 |
||
302 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
* Outputs the 'disabled' message for the WordPress Link Manager. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
304 |
* |
0 | 305 |
* @since 3.5.0 |
306 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
* @global string $pagenow |
0 | 309 |
*/ |
310 |
function wp_link_manager_disabled_message() { |
|
311 |
global $pagenow; |
|
9 | 312 |
if ( 'link-manager.php' != $pagenow && 'link-add.php' != $pagenow && 'link.php' != $pagenow ) { |
0 | 313 |
return; |
9 | 314 |
} |
0 | 315 |
|
316 |
add_filter( 'pre_option_link_manager_enabled', '__return_true', 100 ); |
|
317 |
$really_can_manage_links = current_user_can( 'manage_links' ); |
|
318 |
remove_filter( 'pre_option_link_manager_enabled', '__return_true', 100 ); |
|
319 |
||
320 |
if ( $really_can_manage_links && current_user_can( 'install_plugins' ) ) { |
|
321 |
$link = network_admin_url( 'plugin-install.php?tab=search&s=Link+Manager' ); |
|
9 | 322 |
/* translators: %s: URL of link manager plugin */ |
0 | 323 |
wp_die( sprintf( __( 'If you are looking to use the link manager, please install the <a href="%s">Link Manager</a> plugin.' ), $link ) ); |
324 |
} |
|
325 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
wp_die( __( 'Sorry, you are not allowed to edit the links for this site.' ) ); |
0 | 327 |
} |