SimpleLogger::getLogRowHeaderOutput PHP Method

getLogRowHeaderOutput() public method

Returns header output for a log row Format should be common for all log rows and should be like: Username (user role) · Date
public getLogRowHeaderOutput ( $row ) : string
return string HTML
    function getLogRowHeaderOutput($row)
    {
        // HTML for initiator
        $initiator_html = "";
        $initiator = $row->initiator;
        $context = $row->context;
        switch ($initiator) {
            case "wp":
                $initiator_html .= '<strong class="SimpleHistoryLogitem__inlineDivided">WordPress</strong> ';
                break;
            case "wp_cli":
                $initiator_html .= '<strong class="SimpleHistoryLogitem__inlineDivided">WP-CLI</strong> ';
                break;
                // wp_user = wordpress uses, but user may have been deleted since log entry was added
            // wp_user = wordpress uses, but user may have been deleted since log entry was added
            case "wp_user":
                $user_id = isset($row->context["_user_id"]) ? $row->context["_user_id"] : null;
                if ($user_id > 0 && ($user = get_user_by("id", $user_id))) {
                    // Sender is user and user still exists
                    $is_current_user = $user_id == get_current_user_id() ? true : false;
                    // get user role, as done in user-edit.php
                    $wp_roles = $GLOBALS["wp_roles"];
                    $all_roles = (array) $wp_roles->roles;
                    $user_roles = array_intersect(array_values((array) $user->roles), array_keys((array) $wp_roles->roles));
                    $user_role = array_shift($user_roles);
                    $user_display_name = $user->display_name;
                    /*
                     * If user who logged this is the currently logged in user
                     * skip name and email and use just "You"
                     *
                     * @param bool If you should be used
                     * @since 2.1
                     */
                    $use_you = apply_filters("simple_history/header_initiator_use_you", true);
                    if ($use_you && $is_current_user) {
                        $tmpl_initiator_html = '
							<a href="%6$s" class="SimpleHistoryLogitem__headerUserProfileLink">
								<strong class="SimpleHistoryLogitem__inlineDivided">%5$s</strong>
							</a>
						';
                    } else {
                        $tmpl_initiator_html = '
							<a href="%6$s" class="SimpleHistoryLogitem__headerUserProfileLink">
								<strong class="SimpleHistoryLogitem__inlineDivided">%3$s</strong>
								<span class="SimpleHistoryLogitem__inlineDivided SimpleHistoryLogitem__headerEmail">%2$s</span>
							</a>
						';
                    }
                    /**
                     * Filter the format for the user output
                     *
                     * @since 2.0
                     *
                     * @param string $format.
                     */
                    $tmpl_initiator_html = apply_filters("simple_history/header_initiator_html_existing_user", $tmpl_initiator_html);
                    $initiator_html .= sprintf($tmpl_initiator_html, esc_html($user->user_login), esc_html($user->user_email), esc_html($user_display_name), $user_role, _x("You", "header output when initiator is the currently logged in user", "simple-history"), get_edit_user_link($user_id));
                } else {
                    if ($user_id > 0) {
                        // Sender was a user, but user is deleted now
                        // output all info we have
                        // _user_id
                        // _username
                        // _user_login
                        // _user_email
                        $initiator_html .= sprintf('<strong class="SimpleHistoryLogitem__inlineDivided">' . __('Deleted user (had id %1$s, email %2$s, login %3$s)', "simple-history") . '</strong>', esc_html($context["_user_id"]), esc_html($context["_user_email"]), esc_html($context["_user_login"]));
                    }
                }
                break;
            case "web_user":
                /*
                Note: server_remote_addr may not show visiting/attacking ip, if server is behind...stuff..
                Can be behind varnish cashe, or browser can for example use compression in chrome mobile
                then the real ip is behind _server_http_x_forwarded_for_0 or similar
                _server_remote_addr	66.249.81.222
                _server_http_x_forwarded_for_0	5.35.187.212
                */
                // Check if additional IP addresses are stored, from http_x_forwarded_for and so on
                $arr_found_additional_ip_headers = $this->get_event_ip_number_headers($row);
                if (empty($context["_server_remote_addr"])) {
                    $initiator_html .= "<strong class='SimpleHistoryLogitem__inlineDivided'>" . __("Anonymous web user", "simple-history") . "</strong> ";
                } else {
                    $initiator_html .= "<strong class='SimpleHistoryLogitem__inlineDivided SimpleHistoryLogitem__anonUserWithIp'>";
                    #if ( sizeof( $arr_found_additional_ip_headers ) ) {
                    #$iplookup_link = sprintf('https://ipinfo.io/%1$s', esc_attr($context["_server_remote_addr"]));
                    #$ip_numbers_joined = wp_sprintf_l('%l', array("_server_remote_addr" => $context["_server_remote_addr"]) + $arr_found_additional_ip_headers);
                    /*$initiator_html .= sprintf(
                    			__('Anonymous user with multiple IP addresses detected: %1$s', "simple-history"),
                    			"<a target='_blank' href={$iplookup_link} class='SimpleHistoryLogitem__anonUserWithIp__theIp'>" . esc_html( $ip_numbers_joined ) . "</a>"
                    		);*/
                    /*
                    print_r($arr_found_additional_ip_headers);
                    Array
                    (
                        [_server_http_x_forwarded_for_0] => 5.35.187.212
                        [_server_http_x_forwarded_for_1] => 83.251.97.21
                    )
                    */
                    #} else {
                    // single ip address
                    $iplookup_link = sprintf('https://ipinfo.io/%1$s', esc_attr($context["_server_remote_addr"]));
                    $initiator_html .= sprintf(__('Anonymous user from %1$s', "simple-history"), "<a target='_blank' href={$iplookup_link} class='SimpleHistoryLogitem__anonUserWithIp__theIp'>" . esc_html($context["_server_remote_addr"]) . "</a>");
                    #} // multiple ip
                    $initiator_html .= "</strong> ";
                    // $initiator_html .= "<strong>" . __("<br><br>Unknown user from {$context["_server_remote_addr"]}") . "</strong>";
                    // $initiator_html .= "<strong>" . __("<br><br>{$context["_server_remote_addr"]}") . "</strong>";
                    // $initiator_html .= "<strong>" . __("<br><br>User from IP {$context["_server_remote_addr"]}") . "</strong>";
                    // $initiator_html .= "<strong>" . __("<br><br>Non-logged in user from IP  {$context["_server_remote_addr"]}") . "</strong>";
                }
                break;
            case "other":
                $initiator_html .= "<strong class='SimpleHistoryLogitem__inlineDivided'>" . _x("Other", "Event header output, when initiator is unknown", "simple-history") . "</strong>";
                break;
                // no initiator
            // no initiator
            case null:
                // $initiator_html .= "<strong class='SimpleHistoryLogitem__inlineDivided'>Null</strong>";
                break;
            default:
                $initiator_html .= "<strong class='SimpleHistoryLogitem__inlineDivided'>" . esc_html($initiator) . "</strong>";
        }
        /**
         * Filter generated html for the initiator row header html
         *
         * @since 2.0
         *
         * @param string $initiator_html
         * @param object $row Log row
         */
        $initiator_html = apply_filters("simple_history/row_header_initiator_output", $initiator_html, $row);
        // HTML for date
        // Date (should...) always exist
        // http://developers.whatwg.org/text-level-semantics.html#the-time-element
        $date_html = "";
        $str_when = "";
        // $row->date is in GMT
        $date_datetime = new DateTime($row->date, new DateTimeZone('GMT'));
        // Current datetime in GMT
        $time_current = strtotime(current_time("mysql", 1));
        /**
         * Filter how many seconds as most that can pass since an
         * event occured to show "nn minutes ago" (human diff time-format) instead of exact date
         *
         * @since 2.0
         *
         * @param int $time_ago_max_time Seconds
         */
        $time_ago_max_time = DAY_IN_SECONDS * 2;
        $time_ago_max_time = apply_filters("simple_history/header_time_ago_max_time", $time_ago_max_time);
        /**
         * Filter how many seconds as most that can pass since an
         * event occured to show "just now" instead of exact date
         *
         * @since 2.0
         *
         * @param int $time_ago_max_time Seconds
         */
        $time_ago_just_now_max_time = 30;
        $time_ago_just_now_max_time = apply_filters("simple_history/header_just_now_max_time", $time_ago_just_now_max_time);
        if ($time_current - $date_datetime->getTimestamp() <= $time_ago_just_now_max_time) {
            // show "just now" if event is very recent
            $str_when = __("Just now", "simple-history");
        } else {
            if ($time_current - $date_datetime->getTimestamp() > $time_ago_max_time) {
                /* translators: Date format for log row header, see http://php.net/date */
                $datef = __('M j, Y \\a\\t G:i', "simple-history");
                $str_when = date_i18n($datef, strtotime(get_date_from_gmt($row->date)));
            } else {
                // Show "nn minutes ago" when event is xx seconds ago or earlier
                $date_human_time_diff = human_time_diff($date_datetime->getTimestamp(), $time_current);
                /* translators: 1: last modified date and time in human time diff-format */
                $str_when = sprintf(__('%1$s ago', 'simple-history'), $date_human_time_diff);
            }
        }
        $item_permalink = admin_url("index.php?page=simple_history_page");
        if (!empty($row->id)) {
            $item_permalink .= "#item/{$row->id}";
        }
        $date_format = get_option('date_format') . ' - ' . get_option('time_format');
        $str_datetime_title = sprintf(__('%1$s local time %3$s (%2$s GMT time)', "simple-history"), get_date_from_gmt($date_datetime->format('Y-m-d H:i:s'), $date_format), $date_datetime->format($date_format), PHP_EOL);
        $date_html = "<span class='SimpleHistoryLogitem__permalink SimpleHistoryLogitem__when SimpleHistoryLogitem__inlineDivided'>";
        $date_html .= "<a class='' href='{$item_permalink}'>";
        $date_html .= sprintf('<time datetime="%3$s" title="%1$s" class="">%2$s</time>', esc_attr($str_datetime_title), esc_html($str_when), $date_datetime->format(DateTime::RFC3339));
        $date_html .= "</a>";
        $date_html .= "</span>";
        /**
         * Filter the output of the date section of the header.
         *
         * @since 2.5.1
         *
         * @param String $date_html
         * @param array $row
         */
        $date_html = apply_filters("simple_history/row_header_date_output", $date_html, $row);
        // Logger "via" info in header, i.e. output some extra
        // info next to the time to make it more clear what plugin etc.
        // that "caused" this event
        $via_html = "";
        $logger_name_via = $this->getInfoValueByKey("name_via");
        if ($logger_name_via) {
            $via_html = "<span class='SimpleHistoryLogitem__inlineDivided SimpleHistoryLogitem__via'>";
            $via_html .= $logger_name_via;
            $via_html .= "</span>";
        }
        // Loglevel
        // SimpleHistoryLogitem--loglevel-warning
        /*
        		$level_html = sprintf(
        		'<span class="SimpleHistoryLogitem--logleveltag SimpleHistoryLogitem--logleveltag-%1$s">%1$s</span>',
        		$row->level
        		);
        */
        // Glue together final result
        $template = '
			%1$s
			%2$s
			%3$s
		';
        #if ( ! $initiator_html ) {
        #	$template = '%2$s';
        #}
        $html = sprintf($template, $initiator_html, $date_html, $via_html);
        /**
         * Filter generated html for the log row header
         *
         * @since 2.0
         *
         * @param string $html
         * @param object $row Log row
         */
        $html = apply_filters("simple_history/row_header_output", $html, $row);
        return $html;
    }