Completed
Branch dev (85e891)
by
unknown
01:52
created

___getPageLoadStats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Page Framework
4
 *
5
 * http://admin-page-framework.michaeluno.jp/
6
 * Copyright (c) 2013-2020, Michael Uno; Licensed MIT
7
 *
8
 */
9
10
/**
11
 * Collects data of page loads in admin pages.
12
 *
13
 * @since       2.1.7
14
 * @package     AdminPageFramework/Common/Factory/Debug
15
 * @internal
16
 */
17
abstract class AdminPageFramework_PageLoadInfo_Base extends AdminPageFramework_FrameworkUtility {
18
19
    public $oProp;
20
21
    public $oMsg;
22
23
    protected $_nInitialMemoryUsage;
24
25
    /**
26
     * Sets up hooks and properties.
27
     */
28
    public function __construct( $oProp, $oMsg ) {
29
30
        if ( ! $this->_shouldProceed( $oProp ) ) {
31
            return;
32
        }
33
34
        $this->oProp                = $oProp;
35
        $this->oMsg                 = $oMsg;
36
        $this->_nInitialMemoryUsage = memory_get_usage();
37
38
        add_action( 'in_admin_footer', array( $this, '_replyToSetPageLoadInfoInFooter' ), 999 );
39
40
    }
41
        /**
42
         * @since       3.8.5
43
         * @return      boolean
44
         */
45
        private function _shouldProceed( $oProp ) {
46
47
            if ( $oProp->bIsAdminAjax || ! $oProp->bIsAdmin ) {
48
                return false;
49
            }
50
            return ( boolean ) $oProp->bShowDebugInfo;
51
52
        }
53
54
    /**
55
     * @remark Should be overridden in an extended class.
56
     */
57
    public function _replyToSetPageLoadInfoInFooter() {}
58
59
    /**
60
     * Indicates whether the page load info is inserted or not.
61
     */
62
    static private $_bLoadedPageLoadInfo = false;
63
64
    /**
65
     * Display gathered information.
66
     *
67
     * @param    string $sFooterHTML
68
     * @return   string
69
     * @internal
70
     */
71
    public function _replyToGetPageLoadInfo( $sFooterHTML ) {
72
73
        // 3.8.8+ The `bShowDebugInfo` property may be updated by the user during the page load.
74
        if ( ! $this->oProp->bShowDebugInfo ) {
75
            return $sFooterHTML;
76
        }
77
78
        if ( self::$_bLoadedPageLoadInfo ) {
79
            return $sFooterHTML;
80
        }
81
        self::$_bLoadedPageLoadInfo = true;
82
83
        return $sFooterHTML
84
            . $this->___getPageLoadStats();
85
86
    }
87
        /**
88
         * Returns the output of page load stats.
89
         * @since  3.8.8
90
         * @return string
91
         */
92
        private function ___getPageLoadStats() {
93
94
            $_nSeconds            = timer_stop( 0 );
95
            $_nQueryCount         = get_num_queries();
96
            $_iMemoryUsage        = memory_get_usage();
97
            $_nMemoryUsage        = round( $_iMemoryUsage, 2 );
98
            $_sMemoryUsage        = $this->getReadableBytes( $_iMemoryUsage );
99
            $_nMemoryPeakUsage    = round( memory_get_peak_usage(), 2 );
100
            $_sMemoryPeakUsage    = $this->getReadableBytes( $_nMemoryPeakUsage );
101
            $_iMemoryLimit        = $this->getNumberOfReadableSize( WP_MEMORY_LIMIT );
102
            $_sMemoryLimit        = $this->getReadableBytes( $_iMemoryLimit );
103
            $_nMemoryLimit        = round( $_iMemoryLimit, 2 );
104
            $_nInitialMemoryUsage = round( $this->_nInitialMemoryUsage, 2 );
105
            $_sInitialMemoryUsage = $this->getReadableBytes( $_nInitialMemoryUsage );
106
            return "<div id='admin-page-framework-page-load-stats'>"
107
                    . "<ul>"
108
                        . "<li>" . sprintf( $this->oMsg->get( 'queries_in_seconds' ), $_nQueryCount, $_nSeconds ) . "</li>"
109
                        . "<li>" . sprintf( $this->oMsg->get( 'out_of_x_memory_used' ), $_sMemoryUsage, $_sMemoryLimit, round( ( $_nMemoryUsage / $_nMemoryLimit ), 2 ) * 100 . '%' ) . "</li>"
110
                        . "<li>" . sprintf( $this->oMsg->get( 'peak_memory_usage' ), $_sMemoryPeakUsage ) . "</li>"
111
                        . "<li>" . sprintf( $this->oMsg->get( 'initial_memory_usage' ), $_sInitialMemoryUsage ) . "</li>"
112
                    . "</ul>"
113
                . "</div>";
114
115
        }
116
117
}