PluginProbe
Memory Limit Manager / trunk
Memory Limit Manager vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4
memory-limit-manager / includes / class-admin.php

class-admin.php in Memory Limit Manager trunk, at includes/class-admin.php

707 lines 34.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin functionality
4 */
5
6 // Exit if accessed directly
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10
11 /**
12 * Admin class
13 */
14 class Memory_Manager_WP_Admin {
15
16 /**
17 * Instance of this class
18 */
19 private static $instance = null;
20
21 /**
22 * Get instance of this class
23 */
24 public static function get_instance() {
25 if ( null === self::$instance ) {
26 self::$instance = new self();
27 }
28 return self::$instance;
29 }
30
31 /**
32 * Constructor
33 */
34 private function __construct() {
35 add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
36 add_action( 'admin_post_memory_limit_manager_update', array( $this, 'handle_form_submission' ) );
37 add_action( 'admin_notices', array( $this, 'display_admin_notices' ) );
38 add_filter( 'plugin_action_links_' . plugin_basename( MEMORY_MANAGER_WP_PLUGIN_FILE ), array( $this, 'add_plugin_action_links' ) );
39 }
40
41 /**
42 * Add Settings link on plugins page
43 */
44 public function add_plugin_action_links( $links ) {
45 $settings_link = '<a href="' . esc_url( admin_url( 'options-general.php?page=memory-limit-manager' ) ) . '">' . esc_html__( 'Settings', 'memory-limit-manager' ) . '</a>';
46 array_unshift( $links, $settings_link );
47 return $links;
48 }
49
50 /**
51 * Add admin menu
52 */
53 public function add_admin_menu() {
54 add_options_page(
55 __( 'Memory Limit Manager', 'memory-limit-manager' ),
56 __( 'Memory Manager', 'memory-limit-manager' ),
57 'manage_options',
58 'memory-limit-manager',
59 array( $this, 'render_admin_page' )
60 );
61 }
62
63 /**
64 * Handle form submission via admin-post.php
65 */
66 public function handle_form_submission() {
67 // Check nonce
68 if ( ! isset( $_POST['memory_limit_manager_nonce'] ) ||
69 ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['memory_limit_manager_nonce'] ) ), 'memory_limit_manager_save' ) ) {
70 wp_die( esc_html__( 'Security check failed. Please try again.', 'memory-limit-manager' ), esc_html__( 'Security Error', 'memory-limit-manager' ), array( 'back_link' => true ) );
71 }
72
73 // Check permissions
74 if ( ! current_user_can( 'manage_options' ) ) {
75 wp_die( esc_html__( 'You do not have permission to perform this action.', 'memory-limit-manager' ), esc_html__( 'Permission Error', 'memory-limit-manager' ), array( 'back_link' => true ) );
76 }
77
78 // Get and validate input
79 $wp_memory_limit = isset( $_POST['wp_memory_limit'] ) ? sanitize_text_field( wp_unslash( $_POST['wp_memory_limit'] ) ) : '';
80 $wp_max_memory_limit = isset( $_POST['wp_max_memory_limit'] ) ? sanitize_text_field( wp_unslash( $_POST['wp_max_memory_limit'] ) ) : '';
81
82 // Validate memory format
83 $errors = array();
84
85 if ( ! $this->validate_memory_value( $wp_memory_limit ) ) {
86 $errors[] = __( 'WP Memory Limit format is invalid. Use format like: 256M, 512M, 1G', 'memory-limit-manager' );
87 }
88
89 if ( ! $this->validate_memory_value( $wp_max_memory_limit ) ) {
90 $errors[] = __( 'WP Max Memory Limit format is invalid. Use format like: 256M, 512M, 1G', 'memory-limit-manager' );
91 }
92
93 // Check if max is greater than regular limit
94 if ( empty( $errors ) && $this->parse_memory_value( $wp_max_memory_limit ) < $this->parse_memory_value( $wp_memory_limit ) ) {
95 $errors[] = __( 'WP Max Memory Limit must be greater than or equal to WP Memory Limit', 'memory-limit-manager' );
96 }
97
98 if ( ! empty( $errors ) ) {
99 $user_id = get_current_user_id();
100 set_transient( 'memory_limit_manager_errors_' . $user_id, $errors, 300 );
101 wp_safe_redirect( add_query_arg( 'status', 'validation_error', admin_url( 'options-general.php?page=memory-limit-manager' ) ) );
102 exit;
103 }
104
105 // Update wp-config.php
106 $config_handler = new Memory_Manager_WP_Config_Handler();
107 $result = $config_handler->update_memory_limits( $wp_memory_limit, $wp_max_memory_limit );
108
109 $user_id = get_current_user_id();
110
111 if ( is_wp_error( $result ) ) {
112 // Store attempted values for manual config
113 set_transient( 'memory_limit_manager_attempted_values_' . $user_id, array(
114 'wp_memory_limit' => $wp_memory_limit,
115 'wp_max_memory_limit' => $wp_max_memory_limit,
116 ), 300 );
117
118 set_transient( 'memory_limit_manager_errors_' . $user_id, array( $result->get_error_message() ), 300 );
119 wp_safe_redirect( add_query_arg( 'status', 'error', admin_url( 'options-general.php?page=memory-limit-manager' ) ) );
120 exit;
121 }
122
123 // Verify the changes were actually written
124 $config_file_values = $config_handler->get_config_file_values();
125
126 if ( $config_file_values['wp_memory_limit'] === $wp_memory_limit &&
127 $config_file_values['wp_max_memory_limit'] === $wp_max_memory_limit ) {
128 // Clear attempted values on success
129 delete_transient( 'memory_limit_manager_attempted_values_' . $user_id );
130
131 set_transient( 'memory_limit_manager_success_' . $user_id, __( 'Memory limits updated successfully in wp-config.php. The new values will be used on subsequent requests.', 'memory-limit-manager' ), 300 );
132 wp_safe_redirect( add_query_arg( array( 'status' => 'success', 'updated' => time() ), admin_url( 'options-general.php?page=memory-limit-manager' ) ) );
133 exit;
134 } else {
135 $errors = array();
136 $errors[] = __( 'The plugin attempted to write to wp-config.php, but the values were not saved correctly.', 'memory-limit-manager' );
137
138 if ( empty( $config_file_values['wp_memory_limit'] ) ) {
139 $errors[] = sprintf(
140 /* translators: %s: Attempted memory limit value */
141 __( 'WP_MEMORY_LIMIT was not added to wp-config.php. Attempted to set: %s', 'memory-limit-manager' ),
142 $wp_memory_limit
143 );
144 }
145
146 if ( empty( $config_file_values['wp_max_memory_limit'] ) ) {
147 $errors[] = sprintf(
148 /* translators: %s: Attempted max memory limit value */
149 __( 'WP_MAX_MEMORY_LIMIT was not added to wp-config.php. Attempted to set: %s', 'memory-limit-manager' ),
150 $wp_max_memory_limit
151 );
152 }
153
154 $errors[] = __( 'Please scroll down and use the Manual Configuration section below.', 'memory-limit-manager' );
155
156 // Store the attempted values so we can display them in manual config
157 set_transient( 'memory_limit_manager_attempted_values_' . $user_id, array(
158 'wp_memory_limit' => $wp_memory_limit,
159 'wp_max_memory_limit' => $wp_max_memory_limit,
160 ), 300 ); // 5 minutes
161
162 set_transient( 'memory_limit_manager_errors_' . $user_id, $errors, 300 );
163 wp_safe_redirect( add_query_arg( 'status', 'write_failed', admin_url( 'options-general.php?page=memory-limit-manager' ) ) );
164 exit;
165 }
166 }
167
168 /**
169 * Validate memory value format
170 */
171 private function validate_memory_value( $value ) {
172 return preg_match( '/^\d+[MG]$/i', $value );
173 }
174
175 /**
176 * Parse memory value to bytes
177 */
178 private function parse_memory_value( $value ) {
179 $unit = strtoupper( substr( $value, -1 ) );
180 $number = (int) substr( $value, 0, -1 );
181
182 if ( $unit === 'G' ) {
183 return $number * 1024 * 1024 * 1024;
184 } elseif ( $unit === 'M' ) {
185 return $number * 1024 * 1024;
186 }
187
188 return 0;
189 }
190
191 /**
192 * Display admin notices
193 */
194 public function display_admin_notices() {
195 // Only show on our plugin page
196 $screen = get_current_screen();
197 if ( ! $screen || $screen->id !== 'settings_page_memory-limit-manager' ) {
198 return;
199 }
200
201 // Check if we have a status parameter
202 $status = isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
203
204 // Always check for transients even without status parameter (in case redirect failed)
205 $user_id = get_current_user_id();
206 $success = get_transient( 'memory_limit_manager_success_' . $user_id );
207 $errors = get_transient( 'memory_limit_manager_errors_' . $user_id );
208
209 // Show success message
210 if ( $status === 'success' || $success ) {
211 $message = $success ? $success : __( 'Memory limits updated successfully! Please clear your site cache and refresh this page to see the changes.', 'memory-limit-manager' );
212 echo '<div class="notice notice-success is-dismissible mlm-notice-success" style="background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%); border-left: 4px solid #28a745; padding: 15px;">';
213 echo '<p style="margin: 0; font-size: 14px;"><strong style="font-size: 16px;">�
214 ' . esc_html__( 'Success!', 'memory-limit-manager' ) . '</strong><br>' . esc_html( $message ) . '</p>';
215 echo '</div>';
216 delete_transient( 'memory_limit_manager_success_' . $user_id );
217 }
218
219 // Show error messages
220 if ( in_array( $status, array( 'error', 'write_failed', 'validation_error' ), true ) || $errors ) {
221 if ( $errors && is_array( $errors ) ) {
222 echo '<div class="notice notice-error is-dismissible mlm-notice-error" id="mlm-error-notice" style="background: linear-gradient(135deg, #f8d7da 0%, #f5c2c7 100%); border-left: 4px solid #dc3545; padding: 15px;">';
223 echo '<p style="margin: 0 0 10px 0; font-size: 16px;"><strong> ' . esc_html__( 'Error:', 'memory-limit-manager' ) . '</strong></p>';
224 echo '<ul style="margin: 8px 0 8px 20px; list-style: disc; font-size: 14px;">';
225 foreach ( $errors as $error ) {
226 echo '<li style="margin: 4px 0;">' . esc_html( $error ) . '</li>';
227 }
228 echo '</ul>';
229 echo '</div>';
230 delete_transient( 'memory_limit_manager_errors_' . $user_id );
231 } else {
232 // Fallback error message
233 echo '<div class="notice notice-error is-dismissible mlm-notice-error" id="mlm-error-notice" style="background: linear-gradient(135deg, #f8d7da 0%, #f5c2c7 100%); border-left: 4px solid #dc3545; padding: 15px;">';
234 echo '<p style="margin: 0; font-size: 14px;"><strong> ' . esc_html__( 'Error:', 'memory-limit-manager' ) . '</strong> ' . esc_html__( 'An error occurred while updating memory limits.', 'memory-limit-manager' ) . '</p>';
235 echo '</div>';
236 }
237 }
238 }
239
240 /**
241 * Render admin page
242 */
243 public function render_admin_page() {
244 if ( ! current_user_can( 'manage_options' ) ) {
245 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'memory-limit-manager' ) );
246 }
247
248 // Get current values
249 $config_handler = new Memory_Manager_WP_Config_Handler();
250 $current_values = $config_handler->get_current_memory_limits();
251
252 $current_wp_memory = defined( 'WP_MEMORY_LIMIT' ) ? WP_MEMORY_LIMIT : '40M';
253 $current_wp_max_memory = defined( 'WP_MAX_MEMORY_LIMIT' ) ? WP_MAX_MEMORY_LIMIT : '256M';
254
255 $actual_memory = ini_get( 'memory_limit' );
256
257 // Diagnostic information
258 $config_path = $config_handler->get_config_path();
259 $is_writable = $config_handler->is_config_writable();
260 $config_perms = $config_path ? substr( sprintf( '%o', fileperms( $config_path ) ), -4 ) : 'N/A';
261
262 // Check for conflicts
263 $conflicts = $config_handler->check_for_conflicts();
264 $config_file_values = $config_handler->get_config_file_values();
265
266 // Get attempted values if write failed (for manual config display)
267 $user_id = get_current_user_id();
268 $attempted_values = get_transient( 'memory_limit_manager_attempted_values_' . $user_id );
269 if ( ! $attempted_values ) {
270 $attempted_values = $current_values; // Fallback to current values
271 }
272
273 ?>
274 <div class="wrap memory-limit-manager-wrap">
275 <h1>
276 <span class="dashicons dashicons-performance"></span>
277 <?php esc_html_e( 'Memory Limit Manager', 'memory-limit-manager' ); ?>
278 </h1>
279
280 <div class="memory-limit-manager-container">
281 <!-- Current Status Card -->
282 <div class="mlm-card mlm-status-card">
283 <div class="mlm-card-header">
284 <h2><?php esc_html_e( 'Current Memory Status', 'memory-limit-manager' ); ?></h2>
285 </div>
286 <div class="mlm-card-body">
287 <div class="mlm-status-grid">
288 <div class="mlm-status-item">
289 <div class="mlm-status-icon mlm-icon-blue">
290 <span class="dashicons dashicons-chart-area"></span>
291 </div>
292 <div class="mlm-status-content">
293 <span class="mlm-status-label"><?php esc_html_e( 'WP Memory Limit', 'memory-limit-manager' ); ?></span>
294 <span class="mlm-status-value"><?php echo esc_html( $current_wp_memory ); ?></span>
295 </div>
296 </div>
297
298 <div class="mlm-status-item">
299 <div class="mlm-status-icon mlm-icon-purple">
300 <span class="dashicons dashicons-admin-settings"></span>
301 </div>
302 <div class="mlm-status-content">
303 <span class="mlm-status-label"><?php esc_html_e( 'WP Max Memory Limit', 'memory-limit-manager' ); ?></span>
304 <span class="mlm-status-value"><?php echo esc_html( $current_wp_max_memory ); ?></span>
305 </div>
306 </div>
307
308 <div class="mlm-status-item">
309 <div class="mlm-status-icon mlm-icon-green">
310 <span class="dashicons dashicons-dashboard"></span>
311 </div>
312 <div class="mlm-status-content">
313 <span class="mlm-status-label"><?php esc_html_e( 'PHP Memory Limit', 'memory-limit-manager' ); ?></span>
314 <span class="mlm-status-value"><?php echo esc_html( $actual_memory ); ?></span>
315 </div>
316 </div>
317 </div>
318
319 <div class="mlm-info-box">
320 <span class="dashicons dashicons-info"></span>
321 <div>
322 <strong><?php esc_html_e( 'Note:', 'memory-limit-manager' ); ?></strong>
323 <?php esc_html_e( 'WP_MEMORY_LIMIT controls WordPress frontend memory. WP_MAX_MEMORY_LIMIT applies to the admin area. Both should not exceed your PHP memory limit.', 'memory-limit-manager' ); ?>
324 </div>
325 </div>
326 </div>
327 </div>
328
329 <!-- Settings Form Card -->
330 <div class="mlm-card mlm-settings-card">
331 <div class="mlm-card-header">
332 <h2><?php esc_html_e( 'Update Memory Limits', 'memory-limit-manager' ); ?></h2>
333 </div>
334 <div class="mlm-card-body">
335 <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" id="memory-limit-manager-form">
336 <input type="hidden" name="action" value="memory_limit_manager_update" />
337 <?php wp_nonce_field( 'memory_limit_manager_save', 'memory_limit_manager_nonce' ); ?>
338
339 <div class="mlm-form-group">
340 <label for="wp_memory_limit" class="mlm-label">
341 <?php esc_html_e( 'WP Memory Limit', 'memory-limit-manager' ); ?>
342 <span class="mlm-required">*</span>
343 </label>
344 <div class="mlm-input-wrapper">
345 <input
346 type="text"
347 name="wp_memory_limit"
348 id="wp_memory_limit"
349 class="mlm-input"
350 value="<?php echo esc_attr( $current_values['wp_memory_limit'] ); ?>"
351 placeholder="256M"
352 required
353 >
354 <span class="mlm-input-hint"><?php esc_html_e( 'Format: 64M, 128M, 256M, 512M, 1G, etc.', 'memory-limit-manager' ); ?></span>
355 </div>
356 </div>
357
358 <div class="mlm-form-group">
359 <label for="wp_max_memory_limit" class="mlm-label">
360 <?php esc_html_e( 'WP Max Memory Limit', 'memory-limit-manager' ); ?>
361 <span class="mlm-required">*</span>
362 </label>
363 <div class="mlm-input-wrapper">
364 <input
365 type="text"
366 name="wp_max_memory_limit"
367 id="wp_max_memory_limit"
368 class="mlm-input"
369 value="<?php echo esc_attr( $current_values['wp_max_memory_limit'] ); ?>"
370 placeholder="512M"
371 required
372 >
373 <span class="mlm-input-hint"><?php esc_html_e( 'Should be equal to or greater than WP Memory Limit', 'memory-limit-manager' ); ?></span>
374 </div>
375 </div>
376
377 <div class="mlm-quick-presets">
378 <label class="mlm-label"><?php esc_html_e( 'Quick Presets:', 'memory-limit-manager' ); ?></label>
379 <div class="mlm-preset-buttons">
380 <button type="button" class="mlm-preset-btn" data-memory="128M" data-max="256M">
381 <span class="dashicons dashicons-arrow-up-alt"></span> <?php esc_html_e( 'Low', 'memory-limit-manager' ); ?> (128M / 256M)
382 </button>
383 <button type="button" class="mlm-preset-btn" data-memory="256M" data-max="512M">
384 <span class="dashicons dashicons-arrow-up-alt"></span> <?php esc_html_e( 'Medium', 'memory-limit-manager' ); ?> (256M / 512M)
385 </button>
386 <button type="button" class="mlm-preset-btn" data-memory="512M" data-max="1G">
387 <span class="dashicons dashicons-arrow-up-alt"></span> <?php esc_html_e( 'High', 'memory-limit-manager' ); ?> (512M / 1G)
388 </button>
389 <button type="button" class="mlm-preset-btn" data-memory="1G" data-max="2G">
390 <span class="dashicons dashicons-arrow-up-alt"></span> <?php esc_html_e( 'Very High', 'memory-limit-manager' ); ?> (1G / 2G)
391 </button>
392 </div>
393 </div>
394
395 <div class="mlm-warning-box">
396 <span class="dashicons dashicons-warning"></span>
397 <div>
398 <strong><?php esc_html_e( 'Warning:', 'memory-limit-manager' ); ?></strong>
399 <?php esc_html_e( 'This will modify your wp-config.php file. A backup will be created automatically before any changes.', 'memory-limit-manager' ); ?>
400 </div>
401 </div>
402
403 <div class="mlm-info-box" style="background: #e8f5e9; border-left: 4px solid #4caf50; margin-top: 15px; margin-bottom: 15px;">
404 <span class="dashicons dashicons-shield" style="color: #4caf50;"></span>
405 <div>
406 <strong><?php esc_html_e( 'Automatic Safety Backup:', 'memory-limit-manager' ); ?></strong>
407 <p style="margin: 8px 0 0 0; font-size: 13px;"><?php esc_html_e( 'Your wp-config.php file will be automatically backed up before any changes. One timestamped backup is kept as wp-config.mlm-backup-YYYY-MM-DD-HHMMSS.php in the same directory. If anything goes wrong, your original file is automatically restored.', 'memory-limit-manager' ); ?></p>
408 </div>
409 </div>
410
411 <div class="mlm-info-box" style="background: #e3f2fd; border-left: 4px solid #2196f3; margin-top: 15px; margin-bottom: 25px;">
412 <span class="dashicons dashicons-lightbulb" style="color: #2196f3;"></span>
413 <div>
414 <strong><?php esc_html_e( 'How it works:', 'memory-limit-manager' ); ?></strong>
415 <p style="margin: 8px 0 0 0; font-size: 13px;"><?php esc_html_e( 'When you click "Update Memory Limits", the plugin will modify your wp-config.php file. After a successful update:', 'memory-limit-manager' ); ?></p>
416 <ol style="margin: 8px 0 0 20px; padding-left: 0; font-size: 13px;">
417 <li><?php esc_html_e( 'You will see a success message at the top of this page', 'memory-limit-manager' ); ?></li>
418 <li><?php esc_html_e( 'The values in wp-config.php have been changed and the "Current Memory Status" section above will display them', 'memory-limit-manager' ); ?></li>
419 </ol>
420 </div>
421 </div>
422
423 <div class="mlm-form-actions">
424 <button type="submit" name="memory_limit_manager_submit" class="button button-primary button-hero mlm-submit-btn">
425 <span class="dashicons dashicons-saved"></span> <?php esc_html_e( 'Update Memory Limits', 'memory-limit-manager' ); ?>
426 </button>
427 </div>
428 </form>
429 </div>
430 </div>
431
432 <!-- Diagnostics Card -->
433 <div class="mlm-card mlm-diagnostics-card">
434 <div class="mlm-card-header">
435 <h2><?php esc_html_e( 'System Diagnostics', 'memory-limit-manager' ); ?></h2>
436 </div>
437 <div class="mlm-card-body">
438 <table class="mlm-diagnostics-table">
439 <tbody>
440 <tr>
441 <td class="mlm-diag-label"><?php esc_html_e( 'wp-config.php Location:', 'memory-limit-manager' ); ?></td>
442 <td class="mlm-diag-value">
443 <code><?php echo esc_html( $config_path ? $config_path : __( 'Not found', 'memory-limit-manager' ) ); ?></code>
444 </td>
445 </tr>
446 <tr>
447 <td class="mlm-diag-label"><?php esc_html_e( 'File Permissions:', 'memory-limit-manager' ); ?></td>
448 <td class="mlm-diag-value">
449 <code><?php echo esc_html( $config_perms ); ?></code>
450 <?php if ( $config_perms !== 'N/A' && ! in_array( $config_perms, array( '0644', '0664', '0666' ), true ) ) : ?>
451 <span class="mlm-diag-warning"><?php esc_html_e( '(Unusual permissions)', 'memory-limit-manager' ); ?></span>
452 <?php endif; ?>
453 </td>
454 </tr>
455 <tr>
456 <td class="mlm-diag-label"><?php esc_html_e( 'Writable:', 'memory-limit-manager' ); ?></td>
457 <td class="mlm-diag-value">
458 <?php if ( $is_writable ) : ?>
459 <span class="mlm-diag-success">✓ <?php esc_html_e( 'Yes', 'memory-limit-manager' ); ?></span>
460 <?php else : ?>
461 <span class="mlm-diag-error">✗ <?php esc_html_e( 'No', 'memory-limit-manager' ); ?></span>
462 <div class="mlm-diag-help">
463 <?php esc_html_e( 'Try running: chmod 644 wp-config.php', 'memory-limit-manager' ); ?>
464 </div>
465 <?php endif; ?>
466 </td>
467 </tr>
468 </tbody>
469 </table>
470
471 <!-- System Requirements Check -->
472 <h3 class="mlm-diag-section-title"><?php esc_html_e( 'System Requirements', 'memory-limit-manager' ); ?></h3>
473 <table class="mlm-diagnostics-table">
474 <tbody>
475 <tr>
476 <td class="mlm-diag-label"><?php esc_html_e( 'WordPress Version:', 'memory-limit-manager' ); ?></td>
477 <td class="mlm-diag-value">
478 <code><?php echo esc_html( get_bloginfo( 'version' ) ); ?></code>
479 <?php if ( version_compare( get_bloginfo( 'version' ), '6.0', '>=' ) ) : ?>
480 <span class="mlm-diag-success"> ✓ <?php esc_html_e( 'Compatible (Requires 6.0+)', 'memory-limit-manager' ); ?></span>
481 <?php else : ?>
482 <span class="mlm-diag-error"> ✗ <?php esc_html_e( 'Incompatible (Requires 6.0+)', 'memory-limit-manager' ); ?></span>
483 <?php endif; ?>
484 </td>
485 </tr>
486 <tr>
487 <td class="mlm-diag-label"><?php esc_html_e( 'PHP Version:', 'memory-limit-manager' ); ?></td>
488 <td class="mlm-diag-value">
489 <code><?php echo esc_html( PHP_VERSION ); ?></code>
490 <?php if ( version_compare( PHP_VERSION, '7.4', '>=' ) ) : ?>
491 <span class="mlm-diag-success"> ✓ <?php esc_html_e( 'Compatible (Requires 7.4+)', 'memory-limit-manager' ); ?></span>
492 <?php else : ?>
493 <span class="mlm-diag-error"> ✗ <?php esc_html_e( 'Incompatible (Requires 7.4+)', 'memory-limit-manager' ); ?></span>
494 <?php endif; ?>
495 </td>
496 </tr>
497 </tbody>
498 </table>
499
500 <h3 class="mlm-diag-section-title"><?php esc_html_e( 'Values Comparison', 'memory-limit-manager' ); ?></h3>
501 <table class="mlm-diagnostics-table mlm-comparison-table">
502 <thead>
503 <tr>
504 <th><?php esc_html_e( 'Constant', 'memory-limit-manager' ); ?></th>
505 <th><?php esc_html_e( 'In wp-config.php', 'memory-limit-manager' ); ?></th>
506 <th><?php esc_html_e( 'WordPress Using', 'memory-limit-manager' ); ?></th>
507 <th><?php esc_html_e( 'Status', 'memory-limit-manager' ); ?></th>
508 </tr>
509 </thead>
510 <tbody>
511 <tr>
512 <td class="mlm-diag-label"><code>WP_MEMORY_LIMIT</code></td>
513 <td class="mlm-diag-value">
514 <?php if ( $config_file_values['wp_memory_limit'] ) : ?>
515 <code><?php echo esc_html( $config_file_values['wp_memory_limit'] ); ?></code>
516 <?php else : ?>
517 <span class="mlm-diag-warning"><?php esc_html_e( 'Not defined', 'memory-limit-manager' ); ?></span>
518 <?php endif; ?>
519 </td>
520 <td class="mlm-diag-value">
521 <code><?php echo esc_html( defined( 'WP_MEMORY_LIMIT' ) ? WP_MEMORY_LIMIT : 'Not defined' ); ?></code>
522 </td>
523 <td class="mlm-diag-value">
524 <?php if ( $config_file_values['wp_memory_limit'] && defined( 'WP_MEMORY_LIMIT' ) && $config_file_values['wp_memory_limit'] === WP_MEMORY_LIMIT ) : ?>
525 <span class="mlm-diag-success">✓ <?php esc_html_e( 'Match', 'memory-limit-manager' ); ?></span>
526 <?php elseif ( $config_file_values['wp_memory_limit'] && defined( 'WP_MEMORY_LIMIT' ) ) : ?>
527 <span class="mlm-diag-error">✗ <?php esc_html_e( 'Mismatch!', 'memory-limit-manager' ); ?></span>
528 <?php else : ?>
529 <span class="mlm-diag-warning">—</span>
530 <?php endif; ?>
531 </td>
532 </tr>
533 <tr>
534 <td class="mlm-diag-label"><code>WP_MAX_MEMORY_LIMIT</code></td>
535 <td class="mlm-diag-value">
536 <?php if ( $config_file_values['wp_max_memory_limit'] ) : ?>
537 <code><?php echo esc_html( $config_file_values['wp_max_memory_limit'] ); ?></code>
538 <?php else : ?>
539 <span class="mlm-diag-warning"><?php esc_html_e( 'Not defined', 'memory-limit-manager' ); ?></span>
540 <?php endif; ?>
541 </td>
542 <td class="mlm-diag-value">
543 <code><?php echo esc_html( defined( 'WP_MAX_MEMORY_LIMIT' ) ? WP_MAX_MEMORY_LIMIT : 'Not defined' ); ?></code>
544 </td>
545 <td class="mlm-diag-value">
546 <?php if ( $config_file_values['wp_max_memory_limit'] && defined( 'WP_MAX_MEMORY_LIMIT' ) && $config_file_values['wp_max_memory_limit'] === WP_MAX_MEMORY_LIMIT ) : ?>
547 <span class="mlm-diag-success">✓ <?php esc_html_e( 'Match', 'memory-limit-manager' ); ?></span>
548 <?php elseif ( $config_file_values['wp_max_memory_limit'] && defined( 'WP_MAX_MEMORY_LIMIT' ) ) : ?>
549 <span class="mlm-diag-error">✗ <?php esc_html_e( 'Mismatch!', 'memory-limit-manager' ); ?></span>
550 <?php else : ?>
551 <span class="mlm-diag-warning">—</span>
552 <?php endif; ?>
553 </td>
554 </tr>
555 </tbody>
556 </table>
557
558 <?php
559 // Only show manual configuration if the user tried to update and it failed
560 $attempted_values_exist = get_transient( 'memory_limit_manager_attempted_values_' . get_current_user_id() );
561 $show_manual_config = ( $attempted_values_exist !== false );
562 ?>
563
564 <?php if ( $show_manual_config ) : ?>
565 <!-- Manual Instructions -->
566 <div class="mlm-warning-box" id="mlm-manual-config" style="background: linear-gradient(135deg, #fff3cd 0%, #ffe9a6 100%); border-left: 4px solid #ff9800;">
567 <span class="dashicons dashicons-admin-tools"></span>
568 <div>
569 <strong><?php esc_html_e( 'Manual Configuration Required', 'memory-limit-manager' ); ?></strong>
570 <p><?php esc_html_e( 'The plugin cannot automatically update your wp-config.php file. Please add the following lines manually:', 'memory-limit-manager' ); ?></p>
571 <?php if ( $attempted_values !== $current_values ) : ?>
572 <p style="background: #e3f2fd; padding: 10px; border-radius: 4px; margin: 10px 0;">
573 <strong><?php esc_html_e( 'Note:', 'memory-limit-manager' ); ?></strong>
574 <?php
575 printf(
576 /* translators: 1: Memory limit value, 2: Max memory limit value */
577 esc_html__( 'You tried to set WP_MEMORY_LIMIT to %1$s and WP_MAX_MEMORY_LIMIT to %2$s. The code below reflects these values.', 'memory-limit-manager' ),
578 '<code>' . esc_html( $attempted_values['wp_memory_limit'] ) . '</code>',
579 '<code>' . esc_html( $attempted_values['wp_max_memory_limit'] ) . '</code>'
580 );
581 ?>
582 </p>
583 <?php endif; ?>
584
585 <div style="display: flex; align-items: center; justify-content: space-between; margin: 12px 0 8px 0;">
586 <p style="margin: 0;"><strong><?php esc_html_e( 'Add these lines to your wp-config.php file:', 'memory-limit-manager' ); ?></strong></p>
587 <button type="button" class="button mlm-copy-config-btn" style="margin-left: 10px;">
588 <span class="dashicons dashicons-clipboard" style="margin-top: 3px;"></span>
589 <?php esc_html_e( 'Copy Code', 'memory-limit-manager' ); ?>
590 </button>
591 </div>
592 <pre style="background: #2d2d2d; color: #f8f8f2; padding: 15px; border-radius: 6px; overflow-x: auto; font-size: 13px;"><code><?php
593 $manual_code = "// WordPress Memory Limits\n";
594 $manual_code .= "define( 'WP_MEMORY_LIMIT', '" . esc_html( $attempted_values['wp_memory_limit'] ) . "' );\n";
595 $manual_code .= "define( 'WP_MAX_MEMORY_LIMIT', '" . esc_html( $attempted_values['wp_max_memory_limit'] ) . "' );\n";
596 echo esc_html( $manual_code );
597 ?></code></pre>
598
599 <p><strong><?php esc_html_e( 'Where to add them:', 'memory-limit-manager' ); ?></strong></p>
600 <ol style="margin: 8px 0 8px 20px;">
601 <li><?php esc_html_e( 'Connect to your site via FTP or cPanel File Manager', 'memory-limit-manager' ); ?></li>
602 <li><?php esc_html_e( 'Open wp-config.php for editing', 'memory-limit-manager' ); ?></li>
603 <li><?php esc_html_e( 'Find this line: /* That\'s all, stop editing! Happy publishing. */', 'memory-limit-manager' ); ?></li>
604 <li><?php esc_html_e( 'Add the code ABOVE that line', 'memory-limit-manager' ); ?></li>
605 <li><?php esc_html_e( 'Save the file', 'memory-limit-manager' ); ?></li>
606 <li><?php esc_html_e( 'Refresh this page to see if values update', 'memory-limit-manager' ); ?></li>
607 </ol>
608 </div>
609 </div>
610 <?php endif; ?>
611
612 <?php if ( ! empty( $conflicts ) ) : ?>
613 <!-- Conflicts Warning -->
614 <div class="mlm-error-box">
615 <span class="dashicons dashicons-warning"></span>
616 <div>
617 <strong><?php esc_html_e( 'Conflicts Detected!', 'memory-limit-manager' ); ?></strong>
618 <p><?php esc_html_e( 'The memory limits in wp-config.php are being overridden by another source:', 'memory-limit-manager' ); ?></p>
619 <ul>
620 <?php foreach ( $conflicts as $conflict ) : ?>
621 <li><?php echo esc_html( $conflict ); ?></li>
622 <?php endforeach; ?>
623 </ul>
624 <p><strong><?php esc_html_e( 'Possible causes:', 'memory-limit-manager' ); ?></strong></p>
625 <ul>
626 <li><?php esc_html_e( ' Another plugin is defining these constants', 'memory-limit-manager' ); ?></li>
627 <li><?php esc_html_e( ' Your theme\'s functions.php is defining these constants', 'memory-limit-manager' ); ?></li>
628 <li><?php esc_html_e( '✗ A must-use plugin (mu-plugin) is defining these constants', 'memory-limit-manager' ); ?></li>
629 <li><?php esc_html_e( '✗ Multiple definitions exist in wp-config.php', 'memory-limit-manager' ); ?></li>
630 </ul>
631 <p><strong><?php esc_html_e( 'How to fix:', 'memory-limit-manager' ); ?></strong></p>
632 <ol>
633 <li><?php esc_html_e( 'Search your theme\'s functions.php for WP_MEMORY_LIMIT or WP_MAX_MEMORY_LIMIT', 'memory-limit-manager' ); ?></li>
634 <li><?php esc_html_e( 'Check wp-content/mu-plugins/ for any files defining these constants', 'memory-limit-manager' ); ?></li>
635 <li><?php esc_html_e( 'Deactivate plugins one by one to find which one is setting these values', 'memory-limit-manager' ); ?></li>
636 <li><?php esc_html_e( 'Open wp-config.php and search for duplicate definitions', 'memory-limit-manager' ); ?></li>
637 </ol>
638 </div>
639 </div>
640 <?php endif; ?>
641
642 <?php if ( ! $is_writable ) : ?>
643 <div class="mlm-error-box">
644 <span class="dashicons dashicons-warning"></span>
645 <div>
646 <strong><?php esc_html_e( 'Action Required:', 'memory-limit-manager' ); ?></strong>
647 <p><?php esc_html_e( 'The wp-config.php file is not writable. Please fix file permissions before updating memory limits.', 'memory-limit-manager' ); ?></p>
648 <p><strong><?php esc_html_e( 'Fix via FTP/SSH:', 'memory-limit-manager' ); ?></strong></p>
649 <pre><code>chmod 644 <?php echo esc_html( basename( $config_path ) ); ?></code></pre>
650 </div>
651 </div>
652 <?php endif; ?>
653 </div>
654 </div>
655
656 <!-- Help & Documentation Card -->
657 <div class="mlm-card mlm-help-card">
658 <div class="mlm-card-header">
659 <h2><?php esc_html_e( 'Help & Documentation', 'memory-limit-manager' ); ?></h2>
660 </div>
661 <div class="mlm-card-body">
662 <div class="mlm-help-item">
663 <h3><?php esc_html_e( 'What is WP_MEMORY_LIMIT?', 'memory-limit-manager' ); ?></h3>
664 <p><?php esc_html_e( 'Controls the maximum amount of memory WordPress can use on the frontend of your site. Default is 40MB, but 64MB or higher is recommended for modern WordPress sites.', 'memory-limit-manager' ); ?></p>
665 </div>
666
667 <div class="mlm-help-item">
668 <h3><?php esc_html_e( 'What is WP_MAX_MEMORY_LIMIT?', 'memory-limit-manager' ); ?></h3>
669 <p><?php esc_html_e( 'Controls the maximum memory limit for WordPress admin area operations. This should be higher than WP_MEMORY_LIMIT as admin operations typically require more memory.', 'memory-limit-manager' ); ?></p>
670 </div>
671
672 <div class="mlm-help-item">
673 <h3><?php esc_html_e( 'Recommended Values', 'memory-limit-manager' ); ?></h3>
674 <ul>
675 <li><strong><?php esc_html_e( 'Small sites:', 'memory-limit-manager' ); ?></strong> <?php esc_html_e( '128M / 256M', 'memory-limit-manager' ); ?></li>
676 <li><strong><?php esc_html_e( 'Medium sites:', 'memory-limit-manager' ); ?></strong> <?php esc_html_e( '256M / 512M', 'memory-limit-manager' ); ?></li>
677 <li><strong><?php esc_html_e( 'Large sites:', 'memory-limit-manager' ); ?></strong> <?php esc_html_e( '512M / 1G', 'memory-limit-manager' ); ?></li>
678 <li><strong><?php esc_html_e( 'Enterprise:', 'memory-limit-manager' ); ?></strong> <?php esc_html_e( '1G / 2G or higher', 'memory-limit-manager' ); ?></li>
679 </ul>
680 </div>
681 </div>
682 </div>
683
684 <!-- Footer Credits -->
685 <div style="margin-top: 40px; padding: 20px; text-align: center; border-top: 2px solid #e8eaed;">
686 <p style="margin: 0; color: #5f6368; font-size: 14px;">
687 <strong><?php esc_html_e( 'Memory Limit Manager', 'memory-limit-manager' ); ?></strong>
688 <?php esc_html_e( 'by', 'memory-limit-manager' ); ?>
689 <a href="<?php echo esc_url( 'https://muhammadshakeel.com/' ); ?>" target="_blank" rel="noopener" style="color: #2271b1; text-decoration: none; font-weight: 600;">Muhammad Shakeel</a>
690 </p>
691 <p style="margin: 8px 0 0 0; font-size: 13px; color: #999;">
692 <a href="<?php echo esc_url( 'https://wordpress.org/plugins/memory-limit-manager/' ); ?>" target="_blank" rel="noopener" style="color: #999; text-decoration: none;"><?php esc_html_e( 'Plugin Page', 'memory-limit-manager' ); ?></a>
693 <span style="margin: 0 8px;"></span>
694 <a href="<?php echo esc_url( 'https://wordpress.org/support/plugin/memory-limit-manager/' ); ?>" target="_blank" rel="noopener" style="color: #999; text-decoration: none;"><?php esc_html_e( 'Get Support', 'memory-limit-manager' ); ?></a>
695 <span style="margin: 0 8px;"></span>
696 <?php
697 /* translators: %s: Plugin version number */
698 printf( esc_html__( 'Version %s', 'memory-limit-manager' ), esc_html( MEMORY_MANAGER_WP_VERSION ) );
699 ?>
700 </p>
701 </div>
702 </div>
703 </div>
704 <?php
705 }
706 }
707