~canonical-sysadmins/wordpress/4.7.2

« back to all changes in this revision

Viewing changes to wp-admin/includes/bookmark.php

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

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