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


Generated: Mon Jun 27 01:00:02 2022 Cross-referenced by PHPXref 0.7.1