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