Plugin Directory

Changeset 3444788


Ignore:
Timestamp:
01/22/2026 11:47:55 AM (3 days ago)
Author:
apasionados
Message:

Improved: Refactored plugin code to a more modern class structure (no early translation calls, cleaner hooks).
Security: Hardened admin footer output with proper escaping.
Minimum PHP 7.4

Location:
server-ip-memory-usage/tags/3.0.0
Files:
3 copied

Legend:

Unmodified
Added
Removed
  • server-ip-memory-usage/tags/3.0.0/readme.txt

    r3444782 r3444788  
    66Requires at least: 3.0.1
    77Tested up to: 6.9
    8 Requires PHP: 5.4
    9 Stable tag: 2.2.0
     8Requires PHP: 7.4
     9Stable tag: 3.0.0
    1010License: GPLv2 or later
    1111License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    8989== Changelog ==
    9090
     91= 3.0.0 (22/JAN/2026) =
     92* Improved: Refactored plugin code to a more modern class structure (no early translation calls, cleaner hooks).
     93* Security: Hardened admin footer output with proper escaping.
     94
    9195= 2.2.0 (22/JAN/2026) =
    9296* Solved notice since WordPress 6.7.0: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the server-ip-memory-usage domain was triggered too early.
     
    137141== Upgrade Notice ==
    138142
    139 = 2.2.0 =
    140 * UPDATED: Solved notice since WordPress 6.7.0: Function _load_textdomain_just_in_time was called incorrectly.
     143= 3.0.0 =
     144* IMPROVED: Refactored plugin code to a more modern class structure (no early translation calls, cleaner hooks). PHP 7.4+ needed.
    141145
    142146== Contact ==
  • server-ip-memory-usage/tags/3.0.0/server-ip-memory-usage.php

    r3444782 r3444788  
    11<?php
     2declare(strict_types=1);
     3
    24/*
    35Plugin Name: Server IP & Memory Usage Display
    46Plugin URI: http://apasionados.es/#utm_source=wpadmin&utm_medium=plugin&utm_campaign=server-ip-memory-usage-plugin
    57Description: Show the memory limit, current memory usage and IP address in the admin footer.
    6 Version: 2.2.0
     8Version: 3.0.0
    79Author: Apasionados, Apasionados del Marketing
    810Author URI: http://apasionados.es
     
    1113*/
    1214
    13 if ( is_admin() ) {
    14 
    15     class IP_Address_Memory_Usage {
    16 
    17         private $memory = array();
    18 
    19         public function __construct() {
    20             // Cargar traducciones a partir de init (o plugins_loaded)
    21             add_action( 'init', array( $this, 'load_textdomain' ) );
    22 
    23             add_action( 'init', array( $this, 'check_limit' ) );
    24             add_filter( 'admin_footer_text', array( $this, 'add_footer' ) );
    25         }
    26 
    27         public function load_textdomain() {
    28             load_plugin_textdomain(
    29                 'server-ip-memory-usage',
    30                 false,
    31                 dirname( plugin_basename( __FILE__ ) ) . '/lang/'
    32             );
    33         }
    34 
    35         public function check_limit() {
    36             $this->memory['limit'] = (int) ini_get( 'memory_limit' );
    37         }
    38 
    39         private function check_memory_usage() {
    40             $this->memory['usage'] = function_exists( 'memory_get_peak_usage' )
    41                 ? round( memory_get_peak_usage( true ) / 1024 / 1024, 2 )
    42                 : 0;
    43 
    44             if ( ! empty( $this->memory['usage'] ) && ! empty( $this->memory['limit'] ) ) {
    45                 $this->memory['percent'] = round( $this->memory['usage'] / $this->memory['limit'] * 100, 0 );
    46                 $this->memory['color']   = 'font-weight:normal;';
    47 
    48                 if ( $this->memory['percent'] > 75 ) {
    49                     $this->memory['color'] = 'font-weight:bold;color:#E66F00';
    50                 }
    51                 if ( $this->memory['percent'] > 90 ) {
    52                     $this->memory['color'] = 'font-weight:bold;color:red';
    53                 }
     15if ( ! defined('ABSPATH') ) {
     16    exit;
     17}
     18
     19final class Server_IP_Memory_Usage_Display {
     20
     21    private const TEXT_DOMAIN = 'server-ip-memory-usage';
     22    private const DOMAIN_PATH = '/lang';
     23
     24    /** @var array{limit:int,usage:float,percent:int,color:string} */
     25    private array $memory = [
     26        'limit'   => 0,
     27        'usage'   => 0.0,
     28        'percent' => 0,
     29        'color'   => 'font-weight:normal;',
     30    ];
     31
     32    public static function init(): void {
     33        // Solo admin
     34        if ( ! is_admin() ) {
     35            return;
     36        }
     37
     38        $instance = new self();
     39
     40        // Cargar traducciones: init o posterior (WP 6.7+)
     41        add_action('init', [ $instance, 'load_textdomain' ]);
     42
     43        // Inicializar límites a partir de init
     44        add_action('init', [ $instance, 'set_php_memory_limit' ]);
     45
     46        // Añadir info al footer del admin
     47        add_filter('admin_footer_text', [ $instance, 'add_footer' ]);
     48    }
     49
     50    public function load_textdomain(): void {
     51        load_plugin_textdomain(
     52            self::TEXT_DOMAIN,
     53            false,
     54            dirname(plugin_basename(__FILE__)) . self::DOMAIN_PATH
     55        );
     56    }
     57
     58    public function set_php_memory_limit(): void {
     59        // memory_limit puede venir como "256M" o "-1". Convertimos a MB si es posible.
     60        $limit = ini_get('memory_limit');
     61
     62        if ( $limit === false ) {
     63            $this->memory['limit'] = 0;
     64            return;
     65        }
     66
     67        $limit = trim((string) $limit);
     68
     69        // Caso especial: -1 (sin límite)
     70        if ( $limit === '-1' ) {
     71            // Para mostrarlo “humano”, usamos 0 y luego lo reflejamos en salida si quieres.
     72            $this->memory['limit'] = 0;
     73            return;
     74        }
     75
     76        $this->memory['limit'] = $this->convert_to_mb($limit);
     77    }
     78
     79    /**
     80     * Convierte valores tipo "256M", "1G", "1024K" a MB (int).
     81     */
     82    private function convert_to_mb(string $value): int {
     83        $value = trim($value);
     84
     85        if ( $value === '' ) {
     86            return 0;
     87        }
     88
     89        $unit = strtoupper(substr($value, -1));
     90        $num  = substr($value, 0, -1);
     91
     92        // Si termina en número (sin unidad)
     93        if ( ctype_digit($unit) ) {
     94            return (int) $value;
     95        }
     96
     97        $num = (int) $num;
     98
     99        switch ($unit) {
     100            case 'G':
     101                return $num * 1024;
     102            case 'M':
     103                return $num;
     104            case 'K':
     105                return (int) round($num / 1024);
     106            default:
     107                return (int) $num;
     108        }
     109    }
     110
     111    private function update_memory_usage(): void {
     112        $usage = function_exists('memory_get_peak_usage')
     113            ? (float) memory_get_peak_usage(true) / 1024 / 1024
     114            : 0.0;
     115
     116        $this->memory['usage'] = round($usage, 2);
     117
     118        $limit = $this->memory['limit'];
     119
     120        // Si no hay límite usable, evitamos divisiones
     121        if ( $limit > 0 && $this->memory['usage'] > 0 ) {
     122            $percent = (int) round(($this->memory['usage'] / $limit) * 100, 0);
     123            $this->memory['percent'] = $percent;
     124
     125            $color = 'font-weight:normal;';
     126            if ( $percent > 75 ) {
     127                $color = 'font-weight:bold;color:#E66F00';
    54128            }
    55         }
    56 
    57         private function format_wp_limit( $size ) {
    58             $value  = substr( $size, -1 );
    59             $return = substr( $size, 0, -1 );
    60 
    61             $return = (int) $return;
    62 
    63             switch ( strtoupper( $value ) ) {
    64                 case 'P':
    65                     $return *= 1024;
    66                 case 'T':
    67                     $return *= 1024;
    68                 case 'G':
    69                     $return *= 1024;
    70                 case 'M':
    71                     $return *= 1024;
    72                 case 'K':
    73                     $return *= 1024;
     129            if ( $percent > 90 ) {
     130                $color = 'font-weight:bold;color:red';
    74131            }
    75 
    76             return $return;
    77         }
    78 
    79         private function check_wp_limit() {
    80             $memory = $this->format_wp_limit( WP_MEMORY_LIMIT );
    81             $memory = size_format( $memory );
    82 
    83             return ( $memory ) ? $memory : __( 'N/A', 'server-ip-memory-usage' );
    84         }
    85 
    86         public function add_footer( $content ) {
    87             $this->check_memory_usage();
    88 
    89             $server_ip_address = ! empty( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : '';
    90             if ( $server_ip_address === '' ) {
    91                 $server_ip_address = ! empty( $_SERVER['LOCAL_ADDR'] ) ? $_SERVER['LOCAL_ADDR'] : '';
    92             }
    93 
    94             $content .= ' | ' . __( 'Memory', 'server-ip-memory-usage' ) . ': ' . $this->memory['usage'] . ' ' .
    95                 __( 'of', 'server-ip-memory-usage' ) . ' ' . $this->memory['limit'] .
    96                 ' MB (<span style="' . $this->memory['color'] . '">' . $this->memory['percent'] .
    97                 '%</span>) | ' . __( 'WP LIMIT', 'server-ip-memory-usage' ) . ': ' . $this->check_wp_limit() .
    98                 ' | IP ' . $server_ip_address . ' (' . gethostname() . ') | PHP ' . PHP_VERSION .
    99                 ' @' . ( PHP_INT_SIZE * 8 ) . 'BitOS';
    100 
    101             return $content;
    102         }
    103     }
    104 
    105     add_action( 'plugins_loaded', function () {
    106         new IP_Address_Memory_Usage();
    107     } );
     132            $this->memory['color'] = $color;
     133        } else {
     134            $this->memory['percent'] = 0;
     135            $this->memory['color']   = 'font-weight:normal;';
     136        }
     137    }
     138
     139    private function wp_memory_limit_formatted(): string {
     140        $bytes = $this->format_wp_limit_bytes(WP_MEMORY_LIMIT);
     141        $fmt   = $bytes > 0 ? size_format($bytes) : '';
     142
     143        return $fmt !== '' ? $fmt : __('N/A', self::TEXT_DOMAIN);
     144    }
     145
     146    private function format_wp_limit_bytes(string $size): int {
     147        $size = trim($size);
     148
     149        if ( $size === '' ) {
     150            return 0;
     151        }
     152
     153        $unit = strtoupper(substr($size, -1));
     154        $num  = substr($size, 0, -1);
     155
     156        // Si termina en número (sin unidad)
     157        if ( ctype_digit($unit) ) {
     158            return (int) $size;
     159        }
     160
     161        $num = (int) $num;
     162
     163        switch ($unit) {
     164            case 'P':
     165                $num *= 1024;
     166                // no break
     167            case 'T':
     168                $num *= 1024;
     169                // no break
     170            case 'G':
     171                $num *= 1024;
     172                // no break
     173            case 'M':
     174                $num *= 1024;
     175                // no break
     176            case 'K':
     177                $num *= 1024;
     178                break;
     179        }
     180
     181        return $num;
     182    }
     183
     184    private function get_server_ip(): string {
     185        $ip = $_SERVER['SERVER_ADDR'] ?? '';
     186        if ( $ip === '' ) {
     187            // IIS fallback
     188            $ip = $_SERVER['LOCAL_ADDR'] ?? '';
     189        }
     190        return (string) $ip;
     191    }
     192
     193    public function add_footer(string $content): string {
     194        $this->update_memory_usage();
     195
     196        $server_ip = $this->get_server_ip();
     197
     198        // Si memory_limit = -1, aquí lo mostramos como “Unlimited” (opcional)
     199        $limit_display = $this->memory['limit'] > 0 ? (string) $this->memory['limit'] . ' MB' : __('N/A', self::TEXT_DOMAIN);
     200
     201        // OJO: El footer permite HTML; mantenemos el span de color.
     202        $content .= ' | ' . esc_html__( 'Memory', self::TEXT_DOMAIN ) . ': ' . esc_html((string) $this->memory['usage']) . ' MB '
     203            . esc_html__( 'of', self::TEXT_DOMAIN ) . ' ' . esc_html($limit_display)
     204            . ' (<span style="' . esc_attr($this->memory['color']) . '">' . esc_html((string) $this->memory['percent']) . '%</span>)'
     205            . ' | ' . esc_html__( 'WP LIMIT', self::TEXT_DOMAIN ) . ': ' . esc_html($this->wp_memory_limit_formatted())
     206            . ' | IP ' . esc_html($server_ip) . ' (' . esc_html((string) gethostname()) . ')'
     207            . ' | PHP ' . esc_html(PHP_VERSION) . ' @' . esc_html((string) (PHP_INT_SIZE * 8)) . 'BitOS';
     208
     209        return $content;
     210    }
    108211}
    109212
    110213/**
    111  * Check on plugin activation
     214 * Activación: chequeo mínimo sin traducciones (evita “too early”)
    112215 */
    113 function server_ip_memory_usage_activation() {
    114     if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
    115         deactivate_plugins( plugin_basename( __FILE__ ) );
    116 
    117         $plugin_data    = get_plugin_data( __FILE__ );
    118         $plugin_version = $plugin_data['Version'];
    119         $plugin_name    = $plugin_data['Name'];
     216function server_ip_memory_usage_activation(): void {
     217    if ( version_compare(PHP_VERSION, '5.3', '<') ) {
     218        deactivate_plugins(plugin_basename(__FILE__));
     219
     220        if ( ! function_exists('get_plugin_data') ) {
     221            require_once ABSPATH . 'wp-admin/includes/plugin.php';
     222        }
     223
     224        $plugin_data    = get_plugin_data(__FILE__);
     225        $plugin_version = $plugin_data['Version'] ?? '';
     226        $plugin_name    = $plugin_data['Name'] ?? 'Server IP & Memory Usage Display';
    120227
    121228        wp_die(
    122             '<h1>Could not activate plugin: PHP version error</h1>' .
    123             '<h2>PLUGIN: <i>' . esc_html( $plugin_name . ' ' . $plugin_version ) . '</i></h2>' .
    124             '<p><strong>You are using PHP version ' . esc_html( PHP_VERSION ) . '</strong>. ' .
    125             'This plugin has been tested with PHP versions 5.3 and greater.</p>' .
    126             '<p>WordPress itself recommends using PHP version 7 or greater. Please upgrade your PHP version or contact your Server administrator.</p>',
     229            '<h1>Could not activate plugin: PHP version error</h1>'
     230            . '<h2>PLUGIN: <i>' . esc_html($plugin_name . ' ' . $plugin_version) . '</i></h2>'
     231            . '<p><strong>You are using PHP version ' . esc_html(PHP_VERSION) . '</strong>. This plugin requires PHP 5.3 or greater.</p>'
     232            . '<p>WordPress recommends using PHP 7 or greater. Please upgrade your PHP version or contact your Server administrator.</p>',
    127233            'Could not activate plugin: PHP version error',
    128             array( 'back_link' => true )
     234            [ 'back_link' => true ]
    129235        );
    130236    }
    131237}
    132 register_activation_hook( __FILE__, 'server_ip_memory_usage_activation' );
     238register_activation_hook(__FILE__, 'server_ip_memory_usage_activation');
     239
     240add_action('plugins_loaded', [ 'Server_IP_Memory_Usage_Display', 'init' ]);
Note: See TracChangeset for help on using the changeset viewer.