[ Index ]

PHP Cross Reference of WordPress

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


Generated: Tue Nov 19 01:00:03 2019 Cross-referenced by PHPXref 0.7.1