[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> bookmark.php (source)

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


Generated : Thu Dec 3 08:20:02 2026 Cross-referenced by PHPXref