| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: Server IP & Memory Usage Display |
|---|
| 4 | Plugin URI: http://apasionados.es/#utm_source=wpadmin&utm_medium=plugin&utm_campaign=server-ip-memory-usage-plugin |
|---|
| 5 | Description: Show the memory limit, current memory usage and IP address in the admin footer. |
|---|
| 6 | Version: 2.2.0 |
|---|
| 7 | Author: Apasionados, Apasionados del Marketing |
|---|
| 8 | Author URI: http://apasionados.es |
|---|
| 9 | Text Domain: server-ip-memory-usage |
|---|
| 10 | Domain Path: /lang |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 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 | } |
|---|
| 54 | } |
|---|
| 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; |
|---|
| 74 | } |
|---|
| 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 | } ); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | * Check on plugin activation |
|---|
| 112 | */ |
|---|
| 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']; |
|---|
| 120 | |
|---|
| 121 | 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>', |
|---|
| 127 | 'Could not activate plugin: PHP version error', |
|---|
| 128 | array( 'back_link' => true ) |
|---|
| 129 | ); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | register_activation_hook( __FILE__, 'server_ip_memory_usage_activation' ); |
|---|