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


Generated : Sun Oct 4 08:20:33 2026 Cross-referenced by PHPXref