[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> author-template.php (source)

   1  <?php
   2  /**
   3   * Author Template functions for use in themes.
   4   *
   5   * These functions must be used within the WordPress Loop.
   6   *
   7   * @link https://codex.wordpress.org/Author_Templates
   8   *
   9   * @package WordPress
  10   * @subpackage Template
  11   */
  12  
  13  /**
  14   * Retrieve the author of the current post.
  15   *
  16   * @since 1.5.0
  17   *
  18   * @global WP_User $authordata The current author's data.
  19   *
  20   * @param string $deprecated Deprecated.
  21   * @return string|null The author's display name.
  22   */
  23  function get_the_author( $deprecated = '' ) {
  24      global $authordata;
  25  
  26      if ( ! empty( $deprecated ) ) {
  27          _deprecated_argument( __FUNCTION__, '2.1.0' );
  28      }
  29  
  30      /**
  31       * Filters the display name of the current post's author.
  32       *
  33       * @since 2.9.0
  34       *
  35       * @param string|null $display_name The author's display name.
  36       */
  37      return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : null );
  38  }
  39  
  40  /**
  41   * Display the name of the author of the current post.
  42   *
  43   * The behavior of this function is based off of old functionality predating
  44   * get_the_author(). This function is not deprecated, but is designed to echo
  45   * the value from get_the_author() and as an result of any old theme that might
  46   * still use the old behavior will also pass the value from get_the_author().
  47   *
  48   * The normal, expected behavior of this function is to echo the author and not
  49   * return it. However, backward compatibility has to be maintained.
  50   *
  51   * @since 0.71
  52   *
  53   * @see get_the_author()
  54   * @link https://developer.wordpress.org/reference/functions/the_author/
  55   *
  56   * @param string $deprecated      Deprecated.
  57   * @param bool   $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
  58   * @return string|null The author's display name, from get_the_author().
  59   */
  60  function the_author( $deprecated = '', $deprecated_echo = true ) {
  61      if ( ! empty( $deprecated ) ) {
  62          _deprecated_argument( __FUNCTION__, '2.1.0' );
  63      }
  64  
  65      if ( true !== $deprecated_echo ) {
  66          _deprecated_argument(
  67              __FUNCTION__,
  68              '1.5.0',
  69              sprintf(
  70                  /* translators: %s: get_the_author() */
  71                  __( 'Use %s instead if you do not want the value echoed.' ),
  72                  '<code>get_the_author()</code>'
  73              )
  74          );
  75      }
  76  
  77      if ( $deprecated_echo ) {
  78          echo get_the_author();
  79      }
  80  
  81      return get_the_author();
  82  }
  83  
  84  /**
  85   * Retrieve the author who last edited the current post.
  86   *
  87   * @since 2.8.0
  88   *
  89   * @return string|void The author's display name.
  90   */
  91  function get_the_modified_author() {
  92      $last_id = get_post_meta( get_post()->ID, '_edit_last', true );
  93  
  94      if ( $last_id ) {
  95          $last_user = get_userdata( $last_id );
  96  
  97          /**
  98           * Filters the display name of the author who last edited the current post.
  99           *
 100           * @since 2.8.0
 101           *
 102           * @param string $display_name The author's display name.
 103           */
 104          return apply_filters( 'the_modified_author', $last_user->display_name );
 105      }
 106  }
 107  
 108  /**
 109   * Display the name of the author who last edited the current post,
 110   * if the author's ID is available.
 111   *
 112   * @since 2.8.0
 113   *
 114   * @see get_the_author()
 115   */
 116  function the_modified_author() {
 117      echo get_the_modified_author();
 118  }
 119  
 120  /**
 121   * Retrieves the requested data of the author of the current post.
 122   *
 123   * Valid values for the `$field` parameter include:
 124   *
 125   * - admin_color
 126   * - aim
 127   * - comment_shortcuts
 128   * - description
 129   * - display_name
 130   * - first_name
 131   * - ID
 132   * - jabber
 133   * - last_name
 134   * - nickname
 135   * - plugins_last_view
 136   * - plugins_per_page
 137   * - rich_editing
 138   * - syntax_highlighting
 139   * - user_activation_key
 140   * - user_description
 141   * - user_email
 142   * - user_firstname
 143   * - user_lastname
 144   * - user_level
 145   * - user_login
 146   * - user_nicename
 147   * - user_pass
 148   * - user_registered
 149   * - user_status
 150   * - user_url
 151   * - yim
 152   *
 153   * @since 2.8.0
 154   *
 155   * @global WP_User $authordata The current author's data.
 156   *
 157   * @param string    $field   Optional. The user field to retrieve. Default empty.
 158   * @param int|false $user_id Optional. User ID.
 159   * @return string The author's field from the current author's DB object, otherwise an empty string.
 160   */
 161  function get_the_author_meta( $field = '', $user_id = false ) {
 162      $original_user_id = $user_id;
 163  
 164      if ( ! $user_id ) {
 165          global $authordata;
 166          $user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
 167      } else {
 168          $authordata = get_userdata( $user_id );
 169      }
 170  
 171      if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ), true ) ) {
 172          $field = 'user_' . $field;
 173      }
 174  
 175      $value = isset( $authordata->$field ) ? $authordata->$field : '';
 176  
 177      /**
 178       * Filters the value of the requested user metadata.
 179       *
 180       * The filter name is dynamic and depends on the $field parameter of the function.
 181       *
 182       * @since 2.8.0
 183       * @since 4.3.0 The `$original_user_id` parameter was added.
 184       *
 185       * @param string    $value            The value of the metadata.
 186       * @param int       $user_id          The user ID for the value.
 187       * @param int|false $original_user_id The original user ID, as passed to the function.
 188       */
 189      return apply_filters( "get_the_author_{$field}", $value, $user_id, $original_user_id );
 190  }
 191  
 192  /**
 193   * Outputs the field from the user's DB object. Defaults to current post's author.
 194   *
 195   * @since 2.8.0
 196   *
 197   * @param string    $field   Selects the field of the users record. See get_the_author_meta()
 198   *                           for the list of possible fields.
 199   * @param int|false $user_id Optional. User ID.
 200   *
 201   * @see get_the_author_meta()
 202   */
 203  function the_author_meta( $field = '', $user_id = false ) {
 204      $author_meta = get_the_author_meta( $field, $user_id );
 205  
 206      /**
 207       * The value of the requested user metadata.
 208       *
 209       * The filter name is dynamic and depends on the $field parameter of the function.
 210       *
 211       * @since 2.8.0
 212       *
 213       * @param string    $author_meta The value of the metadata.
 214       * @param int|false $user_id     The user ID.
 215       */
 216      echo apply_filters( "the_author_{$field}", $author_meta, $user_id );
 217  }
 218  
 219  /**
 220   * Retrieve either author's link or author's name.
 221   *
 222   * If the author has a home page set, return an HTML link, otherwise just return the
 223   * author's name.
 224   *
 225   * @since 3.0.0
 226   *
 227   * @return string|null An HTML link if the author's url exist in user meta,
 228   *                     else the result of get_the_author().
 229   */
 230  function get_the_author_link() {
 231      if ( get_the_author_meta( 'url' ) ) {
 232          return sprintf(
 233              '<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
 234              esc_url( get_the_author_meta( 'url' ) ),
 235              /* translators: %s: Author's display name. */
 236              esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), get_the_author() ) ),
 237              get_the_author()
 238          );
 239      } else {
 240          return get_the_author();
 241      }
 242  }
 243  
 244  /**
 245   * Display either author's link or author's name.
 246   *
 247   * If the author has a home page set, echo an HTML link, otherwise just echo the
 248   * author's name.
 249   *
 250   * @link https://developer.wordpress.org/reference/functions/the_author_link/
 251   *
 252   * @since 2.1.0
 253   */
 254  function the_author_link() {
 255      echo get_the_author_link();
 256  }
 257  
 258  /**
 259   * Retrieve the number of posts by the author of the current post.
 260   *
 261   * @since 1.5.0
 262   *
 263   * @return int The number of posts by the author.
 264   */
 265  function get_the_author_posts() {
 266      $post = get_post();
 267      if ( ! $post ) {
 268          return 0;
 269      }
 270      return count_user_posts( $post->post_author, $post->post_type );
 271  }
 272  
 273  /**
 274   * Display the number of posts by the author of the current post.
 275   *
 276   * @link https://developer.wordpress.org/reference/functions/the_author_posts/
 277   * @since 0.71
 278   */
 279  function the_author_posts() {
 280      echo get_the_author_posts();
 281  }
 282  
 283  /**
 284   * Retrieves an HTML link to the author page of the current post's author.
 285   *
 286   * Returns an HTML-formatted link using get_author_posts_url().
 287   *
 288   * @since 4.4.0
 289   *
 290   * @global WP_User $authordata The current author's data.
 291   *
 292   * @return string An HTML link to the author page, or an empty string if $authordata isn't defined.
 293   */
 294  function get_the_author_posts_link() {
 295      global $authordata;
 296      if ( ! is_object( $authordata ) ) {
 297          return '';
 298      }
 299  
 300      $link = sprintf(
 301          '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
 302          esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ),
 303          /* translators: %s: Author's display name. */
 304          esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
 305          get_the_author()
 306      );
 307  
 308      /**
 309       * Filters the link to the author page of the author of the current post.
 310       *
 311       * @since 2.9.0
 312       *
 313       * @param string $link HTML link.
 314       */
 315      return apply_filters( 'the_author_posts_link', $link );
 316  }
 317  
 318  /**
 319   * Displays an HTML link to the author page of the current post's author.
 320   *
 321   * @since 1.2.0
 322   * @since 4.4.0 Converted into a wrapper for get_the_author_posts_link()
 323   *
 324   * @param string $deprecated Unused.
 325   */
 326  function the_author_posts_link( $deprecated = '' ) {
 327      if ( ! empty( $deprecated ) ) {
 328          _deprecated_argument( __FUNCTION__, '2.1.0' );
 329      }
 330      echo get_the_author_posts_link();
 331  }
 332  
 333  /**
 334   * Retrieve the URL to the author page for the user with the ID provided.
 335   *
 336   * @since 2.1.0
 337   *
 338   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 339   *
 340   * @param int    $author_id       Author ID.
 341   * @param string $author_nicename Optional. The author's nicename (slug). Default empty.
 342   * @return string The URL to the author's page.
 343   */
 344  function get_author_posts_url( $author_id, $author_nicename = '' ) {
 345      global $wp_rewrite;
 346      $auth_ID = (int) $author_id;
 347      $link    = $wp_rewrite->get_author_permastruct();
 348  
 349      if ( empty( $link ) ) {
 350          $file = home_url( '/' );
 351          $link = $file . '?author=' . $auth_ID;
 352      } else {
 353          if ( '' === $author_nicename ) {
 354              $user = get_userdata( $author_id );
 355              if ( ! empty( $user->user_nicename ) ) {
 356                  $author_nicename = $user->user_nicename;
 357              }
 358          }
 359          $link = str_replace( '%author%', $author_nicename, $link );
 360          $link = home_url( user_trailingslashit( $link ) );
 361      }
 362  
 363      /**
 364       * Filters the URL to the author's page.
 365       *
 366       * @since 2.1.0
 367       *
 368       * @param string $link            The URL to the author's page.
 369       * @param int    $author_id       The author's ID.
 370       * @param string $author_nicename The author's nice name.
 371       */
 372      $link = apply_filters( 'author_link', $link, $author_id, $author_nicename );
 373  
 374      return $link;
 375  }
 376  
 377  /**
 378   * List all the authors of the site, with several options available.
 379   *
 380   * @link https://developer.wordpress.org/reference/functions/wp_list_authors/
 381   *
 382   * @since 1.2.0
 383   *
 384   * @global wpdb $wpdb WordPress database abstraction object.
 385   *
 386   * @param string|array $args {
 387   *     Optional. Array or string of default arguments.
 388   *
 389   *     @type string       $orderby       How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered',
 390   *                                       'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
 391   *                                       'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
 392   *     @type string       $order         Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
 393   *     @type int          $number        Maximum authors to return or display. Default empty (all authors).
 394   *     @type bool         $optioncount   Show the count in parenthesis next to the author's name. Default false.
 395   *     @type bool         $exclude_admin Whether to exclude the 'admin' account, if it exists. Default true.
 396   *     @type bool         $show_fullname Whether to show the author's full name. Default false.
 397   *     @type bool         $hide_empty    Whether to hide any authors with no posts. Default true.
 398   *     @type string       $feed          If not empty, show a link to the author's feed and use this text as the alt
 399   *                                       parameter of the link. Default empty.
 400   *     @type string       $feed_image    If not empty, show a link to the author's feed and use this image URL as
 401   *                                       clickable anchor. Default empty.
 402   *     @type string       $feed_type     The feed type to link to. Possible values include 'rss2', 'atom'.
 403   *                                       Default is the value of get_default_feed().
 404   *     @type bool         $echo          Whether to output the result or instead return it. Default true.
 405   *     @type string       $style         If 'list', each author is wrapped in an `<li>` element, otherwise the authors
 406   *                                       will be separated by commas.
 407   *     @type bool         $html          Whether to list the items in HTML form or plaintext. Default true.
 408   *     @type array|string $exclude       Array or comma/space-separated list of author IDs to exclude. Default empty.
 409   *     @type array|string $include       Array or comma/space-separated list of author IDs to include. Default empty.
 410   * }
 411   * @return void|string Void if 'echo' argument is true, list of authors if 'echo' is false.
 412   */
 413  function wp_list_authors( $args = '' ) {
 414      global $wpdb;
 415  
 416      $defaults = array(
 417          'orderby'       => 'name',
 418          'order'         => 'ASC',
 419          'number'        => '',
 420          'optioncount'   => false,
 421          'exclude_admin' => true,
 422          'show_fullname' => false,
 423          'hide_empty'    => true,
 424          'feed'          => '',
 425          'feed_image'    => '',
 426          'feed_type'     => '',
 427          'echo'          => true,
 428          'style'         => 'list',
 429          'html'          => true,
 430          'exclude'       => '',
 431          'include'       => '',
 432      );
 433  
 434      $args = wp_parse_args( $args, $defaults );
 435  
 436      $return = '';
 437  
 438      $query_args           = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
 439      $query_args['fields'] = 'ids';
 440      $authors              = get_users( $query_args );
 441  
 442      $author_count = array();
 443      foreach ( (array) $wpdb->get_results( "SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE " . get_private_posts_cap_sql( 'post' ) . ' GROUP BY post_author' ) as $row ) {
 444          $author_count[ $row->post_author ] = $row->count;
 445      }
 446      foreach ( $authors as $author_id ) {
 447          $posts = isset( $author_count[ $author_id ] ) ? $author_count[ $author_id ] : 0;
 448  
 449          if ( ! $posts && $args['hide_empty'] ) {
 450              continue;
 451          }
 452  
 453          $author = get_userdata( $author_id );
 454  
 455          if ( $args['exclude_admin'] && 'admin' === $author->display_name ) {
 456              continue;
 457          }
 458  
 459          if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
 460              $name = "$author->first_name $author->last_name";
 461          } else {
 462              $name = $author->display_name;
 463          }
 464  
 465          if ( ! $args['html'] ) {
 466              $return .= $name . ', ';
 467  
 468              continue; // No need to go further to process HTML.
 469          }
 470  
 471          if ( 'list' === $args['style'] ) {
 472              $return .= '<li>';
 473          }
 474  
 475          $link = sprintf(
 476              '<a href="%1$s" title="%2$s">%3$s</a>',
 477              get_author_posts_url( $author->ID, $author->user_nicename ),
 478              /* translators: %s: Author's display name. */
 479              esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
 480              $name
 481          );
 482  
 483          if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
 484              $link .= ' ';
 485              if ( empty( $args['feed_image'] ) ) {
 486                  $link .= '(';
 487              }
 488  
 489              $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
 490  
 491              $alt = '';
 492              if ( ! empty( $args['feed'] ) ) {
 493                  $alt  = ' alt="' . esc_attr( $args['feed'] ) . '"';
 494                  $name = $args['feed'];
 495              }
 496  
 497              $link .= '>';
 498  
 499              if ( ! empty( $args['feed_image'] ) ) {
 500                  $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
 501              } else {
 502                  $link .= $name;
 503              }
 504  
 505              $link .= '</a>';
 506  
 507              if ( empty( $args['feed_image'] ) ) {
 508                  $link .= ')';
 509              }
 510          }
 511  
 512          if ( $args['optioncount'] ) {
 513              $link .= ' (' . $posts . ')';
 514          }
 515  
 516          $return .= $link;
 517          $return .= ( 'list' === $args['style'] ) ? '</li>' : ', ';
 518      }
 519  
 520      $return = rtrim( $return, ', ' );
 521  
 522      if ( $args['echo'] ) {
 523          echo $return;
 524      } else {
 525          return $return;
 526      }
 527  }
 528  
 529  /**
 530   * Determines whether this site has more than one author.
 531   *
 532   * Checks to see if more than one author has published posts.
 533   *
 534   * For more information on this and similar theme functions, check out
 535   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 536   * Conditional Tags} article in the Theme Developer Handbook.
 537   *
 538   * @since 3.2.0
 539   *
 540   * @global wpdb $wpdb WordPress database abstraction object.
 541   *
 542   * @return bool Whether or not we have more than one author
 543   */
 544  function is_multi_author() {
 545      global $wpdb;
 546  
 547      $is_multi_author = get_transient( 'is_multi_author' );
 548      if ( false === $is_multi_author ) {
 549          $rows            = (array) $wpdb->get_col( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2" );
 550          $is_multi_author = 1 < count( $rows ) ? 1 : 0;
 551          set_transient( 'is_multi_author', $is_multi_author );
 552      }
 553  
 554      /**
 555       * Filters whether the site has more than one author with published posts.
 556       *
 557       * @since 3.2.0
 558       *
 559       * @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
 560       */
 561      return apply_filters( 'is_multi_author', (bool) $is_multi_author );
 562  }
 563  
 564  /**
 565   * Helper function to clear the cache for number of authors.
 566   *
 567   * @since 3.2.0
 568   * @access private
 569   */
 570  function __clear_multi_author_cache() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore
 571      delete_transient( 'is_multi_author' );
 572  }


Generated: Mon Aug 10 01:00:03 2020 Cross-referenced by PHPXref 0.7.1