Plugin Directory

Changeset 701983

Timestamp:
04/23/2013 05:58:32 AM (11 years ago)
Author:
mbijon
Message:

Update calculations for efficiency, remove some duplication, better msg'ing
(Confirm tested for WP 3.5.1)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • hosting-monitor/branches/classify/hosting-monitor.php

    r621602 r701983  
    22/*
    33Plugin Name: Hosting Monitor
    4 Plugin URI: http://www.geekcoded.com/
    5 Description: Displays server resources used by WordPress in a metabox on the WordPress Dashboard.
    6 Author: Geekcoded.com
     4Plugin URI: http://w/
     5Description: Displays server
     6Author:
    77Version: 0.7
    8 Author URI: http://www.geekcoded.com
     8Author URI: http://www..com
    99License: GPLv2 or later
    10 License URI: http://www.gnu.org/licenses/gpl-2.0.html
    11 
    12 */
    13 /*
    14 Copyright 2011-2012 by Mike Bijon (email: mike@etchsoftware.com)
    15     and Ryan Dawson (email: ryan@alivemediadev.com), both own equal-rights
    16 
    17 This is Version 0.7 as of 5/20/2012
    18 
    19     'Hosting Monitor' plugin for WordPress is free software; you can
     10
     11
     12Copyright 2011 by Mike Bijon (email: mike@etchsoftware.com) and Ryan Dawson (email: ryan@alivemediadev.com), sharing equal-rights
     13
     14This is Version 0.7 as of 4/22/2013
     15
     16    This 'Hosting Monitor' plugin for WordPress is free software; you can
    2017    redistribute it and/or modify it under the terms of the GNU General
    2118    Public License, version 2, as published by the Free Software Foundation.
     
    9390   
    9491    // Read & sanitize(!) user-posted values
    95     $opt_val = intval($_POST[ $data_field_name ]);
    96     $opt_val_db = intval($_POST[ $data_field_name_db ]);
    97     $opt_val2 = sanitize_text_field($_POST[ $data_field_name2 ]);
    98     $opt_val_db2 = sanitize_text_field($_POST[ $data_field_name_db2 ]);
     92    $opt_val = intval();
     93    $opt_val_db = intval();
     94    $opt_val2 = sanitize_text_field();
     95    $opt_val_db2 = sanitize_text_field();
    9996
    10097    // Save the posted values
     
    163160    switch ( $opt_val2 ) { // DISK
    164161        case 'TB':
    165             $spacecalc = 1024 * 1024 * 1024;
     162            $spacecalc = ;
    166163            break;
    167164        case 'GB':
    168             $spacecalc = 1024 * 1024;
     165            $spacecalc = ;
    169166            break;
    170167        default:
     
    174171    switch ( $opt_val_db2 ) { // DB
    175172        case 'TB':
    176             $spacecalc_db = 1024 * 1024 * 1024;
     173            $spacecalc_db = ;
    177174            break;
    178175        case 'GB':
    179             $spacecalc_db = 1024 * 1024;
     176            $spacecalc_db = ;
    180177            break;
    181178        default:
     
    211208        </p>
    212209        <p>
    213             <?php _e("Database space:", 'hostm_plugin' ); ?>
     210            <?php _e(, 'hostm_plugin' ); ?>
    214211            <input type="text" name="<?php echo $data_field_name_db; ?>" value="<?php echo $opt_val_db; ?>" size="5">
    215212            &nbsp;
     
    220217            </select>
    221218        </p>
     219
     220
     221
     222
     223
     224
     225
     226
    222227        <p class="submit">
    223         <input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
     228        <input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e() ?>" />
    224229        </p>
    225230        <?php
     
    294299    $totalspace = ( $opt_val * $spacecalc );
    295300    $freespace = ( $totalspace ) - $usedspace;
    296     $usedspace_percent = round( ($usedspace / ( $totalspace / 100 ) ), 1 );
     301    $usedspace_percent = );
    297302   
    298303    // Calculate used space in chosen units
    299304    $usedspace_units = ( $usedspace / $spacecalc );
    300305   
    301     if ( $usedspace_units < 1 ) $usedspace_units = round( ( $usedspace / $spacecalc ), 3 );
    302     else $usedspace_units = round( ( $usedspace / $spacecalc ), 2 );
     306    if ( $usedspace_units < 1 ) $usedspace_units = round( , 3 );
     307    else $usedspace_units = round( , 2 );
    303308   
    304309    ?>
     
    323328                // Disk space free
    324329                // ###TODO msb 10-25-2011: Handle negative values: 1) bigger alert, 2) change formatting on "- 23MB" text
    325                 echo round( ($freespace / $spacecalc), 2 ) . ' ' . $opt_val2; ?><br />
     330                if ( $usedspace_percent == 0 )
     331                    echo 'No free space (* Or not configured)';
     332                else
     333                    echo round( ( $freespace / $spacecalc ), 2 ) . ' ' . $opt_val2; ?><br />
    326334                <hr>
    327335                <?php echo db_size( $opt_val_db, $spacecalc_db ); ?><br />
     
    340348                        bottom: 0;">
    341349        <p>
    342             This plugin produced by: <a href="http://www.alivemediadev.com/">Alive Media Web Development</a> and <a href="http://www.etchsoftware.com/">Mike Bijon</a>
     350            This plugin produced by: <a href="http://www.alivemediadev.com/">Alive Media Web Development</a>,
     351            &nbsp;and developed by: <a href="http://www.etchsoftware.com/">Mike Bijon</a>
    343352        </p>
    344353        <p>
     
    354363/*
    355364 * Check server memory use
    356  *
    357  * %%Mixed code in class, both new & from Disk Space Pie Chart (DSPC)
    358  *   [[Noted in DSPC "original code from Memory Usage plugin"]]
    359365 *
    360366 */
     
    378384       
    379385        function check_limit() {
    380             $this->memory['limit'] = (int) ini_get('memory_limit') ;
     386            $this->memory['limit'] = (int) ini_get() ;
    381387        }
    382388       
     
    388394        function check_memory_usage() {
    389395           
    390             $this->memory['usage'] = function_exists('memory_get_usage') ? round(memory_get_usage() / 1024 / 1024, 2) : 0;
    391            
    392             if ( !empty($this->memory['usage']) && !empty($this->memory['limit']) ) {
    393                 $this->memory['percent'] = round ($this->memory['usage'] / $this->memory['limit'] * 100, 0);
     396            $this->memory['usage'] = function_exists() : 0;
     397           
     398            if ( !) ) {
     399                $this->memory['percent'] = round ();
    394400                $this->memory['color'] = '#21759B';
    395                 if ($this->memory['percent'] > 80) $this->memory['color'] = '#E66F00';
    396                 if ($this->memory['percent'] > 95) $this->memory['color'] = 'red';
     401                if () $this->memory['color'] = '#E66F00';
     402                if () $this->memory['color'] = 'red';
    397403            }       
    398404        }
     
    400406        /*
    401407         * Calculate DB size units
    402          *
    403          * %%Entire method from Disk Space Pie Chart (DSPC)
    404408         *
    405409         * ###TODO msb 10-24-2011 - De-duplicate!!! Copied from main plugin method hosting_monitor()
    406410         *
    407411         */
    408         function get_file_size_info($filesize) {
    409             $bytes = array('KB', 'KB', 'MB', 'GB', 'TB');
     412        function get_file_size_info() {
     413            $bytes = array();
    410414       
    411415            # values are always displayed
     
    415419            for ( $i = 0; $filesize > 1024; $i++ ) $filesize /= 1024;
    416420       
    417             $file_size_info['size'] = round($filesize,3);
     421            $file_size_info['size'] = round();
    418422            $file_size_info['type'] = $bytes[$i];
    419423       
     
    425429         * Calculate actual DB size
    426430         * Echoes DB size to screen, positioned using CSS
    427          *
    428          * %%Method from Disk Space Pie Chart (DSPC), updated w/ inputs
    429          *
    430431         *
    431432         * Default values translate to: 10 MB
     
    436437         */
    437438        function check_db_size( $opt_val_db = 10, $spacecalc_db = 1024 ) {
    438              $rows = mysql_query("SHOW table STATUS");
     439             $rows = mysql_query();
    439440             $dbsize = 0;
    440441             
    441              while ( $row = mysql_fetch_array($rows) ) {
     442             while ( $row = mysql_fetch_array() ) {
    442443                 $dbsize += $row['Data_length'] + $row['Index_length'];
    443444             }
     
    449450             }
    450451             
    451              $dbsize = $this->get_file_size_info($dbsize);
     452             $dbsize = $this->get_file_size_info();
    452453             
    453454             return "{$dbsize ['size']} {$dbsize['type']}";
     
    457458        /*
    458459         * Build & output the Dashboard metabox
    459          *
    460          * %%Mixed code, new & from Disk Space Pie Chart (DSPC)
    461460         *
    462461         */
    463462        function dashboard_output() {
    464463            $this->check_memory_usage();
    465             $this->memory['limit'] = empty($this->memory['limit']) ? __('N/A') : $this->memory['limit'] . __(' MByte');
    466             $this->memory['usage'] = empty($this->memory['usage']) ? __('N/A') : $this->memory['usage'] . __(' MByte');
     464            $this->memory['limit'] = empty() ? __('N/A') : $this->memory['limit'] . __(' MByte');
     465            $this->memory['usage'] = empty() ? __('N/A') : $this->memory['usage'] . __(' MByte');
    467466           
    468467            // check disk usage and pop into a variable
    469             $output = substr(shell_exec('pwd'), 0, -9);
    470             $usedspace = substr(shell_exec('du -s ' . $output), 0, -(strlen($output)+1));
     468            $output = substr();
     469            $usedspace = substr();
    471470           
    472471            // Get user settings
    473             $opt_val = get_option('guru_space', false); // Explicitly set false
    474             $opt_val_db = get_option('hm_db_space', false); // Explicitly set false
    475             $opt_val2 = get_option('guru_unit');
    476             $opt_val_db2 = get_option('hm_db_unit');
     472            $opt_val = get_option(); // Explicitly set false
     473            $opt_val_db = get_option(); // Explicitly set false
     474            $opt_val2 = get_option();
     475            $opt_val_db2 = get_option();
    477476           
    478477            // Decide which units to use for graph
    479478            switch ( $opt_val2 ) { // DISK
    480479                case 'TB':
    481                     $spacecalc = 1024 * 1024 * 1024;
     480                    $spacecalc = ;
    482481                    break;
    483482                case 'GB':
    484                     $spacecalc = 1024 * 1024;
     483                    $spacecalc = ;
    485484                    break;
    486485                default:
     
    489488            switch ( $opt_val_db2 ) { // DB
    490489                case 'TB':
    491                     $spacecalc_db = 1024 * 1024 * 1024;
     490                    $spacecalc_db = ;
    492491                    break;
    493492                case 'GB':
    494                     $spacecalc_db = 1024 * 1024;
     493                    $spacecalc_db = ;
    495494                    break;
    496495                default:
     
    499498           
    500499            // Calculate used space in chosen units
    501             $usedspace_units = ($usedspace / $spacecalc);
    502            
    503             if ( $usedspace_units < 1 ) $usedspace_units = round( ($usedspace / $spacecalc), 3 );
    504             else $usedspace_units = round( ($usedspace / $spacecalc), 2 );
     500            $usedspace_units = ();
     501           
     502            if ( $usedspace_units < 1 ) $usedspace_units = round( , 3 );
     503            else $usedspace_units = round( , 2 );
    505504           
    506505            // Get storage space set by user
     
    513512            }
    514513           
    515             // End %%DSPC
    516514            if ( $opt_val === false ) {
    517515                // Means user has not saved a config, prompt them if admin
    518                 if ( $hm_user_admin === true) {
     516                if ( $hm_user_admin === true) {
    519517                    $free_space_message = 'Setup not completed. Please <a href="tools.php?page=hosting-monitor-admin">Configure <em>Hosting Monitor</em></a> now';
    520518                }
     
    522520                // Zero is our save-default, assume unlimited space
    523521                $free_space_message  = "UNLIMITED";
    524                 if ( $hm_user_admin === true) $free_space_message .= $config_link;
     522                if ( $hm_user_admin === true) $free_space_message .= $config_link;
    525523            } else {
    526                 $free_space_message  = round(($freespace / $spacecalc), 2) . " " . $opt_val2;
    527                 if ( $hm_user_admin === true) $free_space_message .= $config_link;
     524                $free_space_message  = round() . " " . $opt_val2;
     525                if ( $hm_user_admin === true) $free_space_message .= $config_link;
    528526            }
    529             // Start old %%DSPC
    530527           
    531528           
     
    534531            <ul>   
    535532                <li>
    536                     <strong><?php _e('Disk Space Used'); ?></strong>: <span><?php echo $usedspace_units . " " . $opt_val2; ?> </span>
     533                    <strong><?php _e( 'Disk Space Used' ); ?></strong>: <span><?php
     534                        echo $usedspace_units . " " . $opt_val2; ?> </span>
    537535                    &nbsp;&nbsp;|&nbsp;&nbsp;
    538                     <strong><?php _e('Disk Space Free'); ?></strong>: <span><?php
    539                
    540                     // End %%DSPC
    541                     echo $free_space_message;
    542                     // Start old %%DSPC
    543                     ?></span>
     536                    <strong><?php _e( 'Disk Space Free' ); ?></strong>: <span><?php
     537                        echo $free_space_message; ?></span>
    544538                </li>
    545539            </ul>
     
    547541            <?php
    548542            // Display bar graph for storage-use %
    549             if (!empty($this->memory['percent'])) : ?>
     543            if () : ?>
    550544                <div class="progressbar">
    551545                <?php
    552                 // End %%DSPC code
    553                    
    554546                    ###TODO msb 10-22-2011: Remove inline styles
    555547                   
    556548                    // Calculate space usage % for later
    557549                    $space_usage = ($totalspace == 0) ? 0 : ($usedspace / ($totalspace / 100));
    558                    
    559                 // Start old %%DSPC code
    560550                ?>
    561551                    <div class="" style="height:2em;
     
    578568           
    579569            // Display DB space-used text, if admin
    580             if ( $hm_user_admin === true) {
     570            if ( $hm_user_admin === true) {
    581571                ?>
    582572                <ul>   
    583                     <li><strong><?php _e('Database Size'); ?></strong>: <span><?php
     573                    <li><strong><?php _e(); ?></strong>: <span><?php
    584574                        echo $this->check_db_size( $opt_val_db, $spacecalc_db ); ?></span></li>
    585575                </ul>
     
    591581         * Render Dashboard widget
    592582         *
    593          * %%Entire method from Disk Space Pie Chart (DSPC)
    594          *
    595583         */
    596584        function add_dashboard() {
    597             wp_add_dashboard_widget( 'wp_memory_dashboard', 'Hosting Monitor', array (&$this, 'dashboard_output') );
     585            wp_add_dashboard_widget( 'wp_memory_dashboard', 'Hosting Monitor', array) );
    598586        }
    599587       
     
    601589         * Output memory-use to footer
    602590         *
    603          * %%Entire method from Disk Space Pie Chart (DSPC)
    604          *
    605591         */
    606         function add_footer($content) {
     592        function add_footer() {
    607593            $this->check_memory_usage();
    608594           
     
    662648            $screen->add_help_tab( array(
    663649                'id'      => 'hostm_help_faq',
    664                 'title'   => __('Help & FAQ', 'hostm_plugin'),
     650                'title'   => __(),
    665651                'content' => $help_content_faq,
    666652            ));
    667653            $screen->add_help_tab( array(
    668654                'id'      => 'hostm_help_setup',
    669                 'title'   => __('Setup Help', 'hostm_plugin'),
     655                'title'   => __(),
    670656                'content' => $help_content_setup,
    671657            ));
     
    681667     * Loaded after all other plugins, so memory-use accurate
    682668     *
    683      * %%Hook from Disk Space Pie Chart (DSPC)
    684      *
    685669     */
    686670    add_action( 'plugins_loaded', create_function( '', '$memory = new wp_memory_usage();' ) );
Note: See TracChangeset for help on using the changeset viewer.