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


Generated : Mon Mar 18 08:20:01 2024 Cross-referenced by PHPXref