Apple_Actions\Index\Export::format_byline PHP Method

format_byline() public method

Formats the byline
Since: 1.2.0
public format_byline ( WP_Post $post, string $author = '', string $date = '' ) : string
$post WP_Post
$author string
$date string
return string
    public function format_byline($post, $author = '', $date = '')
    {
        // Get the author
        if (empty($author)) {
            $author = ucfirst(get_the_author_meta('display_name', $post->post_author));
        }
        // Get the date
        if (empty($date) && !empty($post->post_date)) {
            $date = $post->post_date;
        }
        // Set the default date format
        $date_format = 'M j, Y | g:i A';
        // Check for a custom byline format
        $byline_format = $this->get_setting('byline_format');
        if (!empty($byline_format)) {
            // Find and replace the author format placeholder name with a temporary placeholder
            // This is because some bylines could contain hashtags!
            $temp_byline_placeholder = 'AUTHOR' . time();
            $byline = str_replace('#author#', $temp_byline_placeholder, $byline_format);
            // Attempt to parse the date format from the remaining string
            $matches = array();
            preg_match('/#(.*?)#/', $byline, $matches);
            if (!empty($matches[1]) && !empty($date)) {
                // Set the date using the custom format
                $byline = str_replace($matches[0], date($matches[1], strtotime($date)), $byline);
            }
            // Replace the temporary placeholder with the actual byline
            $byline = str_replace($temp_byline_placeholder, $author, $byline);
        } else {
            // Use the default format
            $byline = sprintf('by %1$s | %2$s', $author, date($date_format, strtotime($date)));
        }
        return $byline;
    }