[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7