home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / author-template.php < prev    next >
Encoding:
PHP Script  |  2017-09-12  |  15.7 KB  |  544 lines

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