| @@ -72,9 +72,12 @@ | ||
| 72 | 72 | * |
| 73 | 73 | * @param Metasync_Admin $admin The admin instance (needed for header/nav rendering). |
| 74 | 74 | */ |
| 75 | 75 | public function metasync_display_error_log($admin) { |
| 76 | - $log_file = WP_CONTENT_DIR . '/metasync_data/plugin_errors.log'; | |
| 76 | + $log_file = Metasync_Data_Store::file_path('plugin_errors.log'); | |
| 77 | + if (false === $log_file) { | |
| 78 | + $log_file = ''; | |
| 79 | + } | |
| 77 | 80 | if (!Metasync::current_user_has_plugin_access()) { |
| 78 | 81 | return; |
| 79 | 82 | } |
| 80 | 83 | |
| @@ -98,16 +101,26 @@ | ||
| 98 | 101 | update_option('wp_debug_log_enabled', $wp_debug_log); |
| 99 | 102 | update_option('wp_debug_display_enabled', $wp_debug_display); |
| 100 | 103 | |
| 101 | 104 | $data = new ConfigControllerMetaSync(); |
| 102 | - $data->store(); | |
| 103 | - | |
| 104 | - add_settings_error( | |
| 105 | - 'metasync_messages', | |
| 106 | - 'metasync_message', | |
| 107 | - 'WordPress debug settings updated successfully.', | |
| 108 | - 'updated' | |
| 109 | - ); | |
| 105 | + $saved = $data->store(); | |
| 106 | + | |
| 107 | + if ($saved) { | |
| 108 | + add_settings_error( | |
| 109 | + 'metasync_messages', | |
| 110 | + 'metasync_message', | |
| 111 | + 'WordPress debug settings updated successfully.', | |
| 112 | + 'updated' | |
| 113 | + ); | |
| 114 | + } else { | |
| 115 | + add_settings_error( | |
| 116 | + 'metasync_messages', | |
| 117 | + 'metasync_message', | |
| 118 | + 'Debug settings were saved, but wp-config.php could not be updated: ' | |
| 119 | + . ($data->getConfigError() !== '' ? $data->getConfigError() : 'the file is not writable.'), | |
| 120 | + 'error' | |
| 121 | + ); | |
| 122 | + } | |
| 110 | 123 | } elseif ($can_manage_debug_settings && isset($_POST['wp_debug_nonce']) && !wp_verify_nonce($_POST['wp_debug_nonce'], 'metasync_wp_debug_settings')) { |
| 111 | 124 | add_settings_error( |
| 112 | 125 | 'metasync_messages', |
| 113 | 126 | 'metasync_message', |
| @@ -224,9 +237,9 @@ | ||
| 224 | 237 | * Update wp-config.php debug constants. |
| 225 | 238 | */ |
| 226 | 239 | public function metasync_update_wp_config() { |
| 227 | 240 | $wp_config_path = ABSPATH . 'wp-config.php'; |
| 228 | - if (file_exists($wp_config_path) && is_writable($wp_config_path)) { | |
| 241 | + if (file_exists($wp_config_path) && wp_is_writable($wp_config_path)) { | |
| 229 | 242 | $config_file = file_get_contents($wp_config_path); |
| 230 | 243 | |
| 231 | 244 | $wp_debug_enabled = get_option('wp_debug_enabled', 'false') === 'true' ? 'true' : 'false'; |
| 232 | 245 | if (preg_match("/define\s*\(\s*['\"]WP_DEBUG['\"]\s*,\s*.*?\s*\)\s*;/", $config_file)) { |
| @@ -248,8 +261,14 @@ | ||
| 248 | 261 | } else { |
| 249 | 262 | $config_file = str_replace("/* That's all, stop editing! Happy publishing. */", "define('WP_DEBUG_DISPLAY', $wp_debug_display_enabled);\n\n/* That's all, stop editing! Happy publishing. */", $config_file); |
| 250 | 263 | } |
| 251 | 264 | |
| 265 | + // ABSPATH is intentional and verified: the Debug Mode feature | |
| 266 | + // toggles WP_DEBUG constants in wp-config.php itself, the one | |
| 267 | + // file whose location ABSPATH exists to describe. It is not a | |
| 268 | + // plugin-directory or generated-content write, so it stays here | |
| 269 | + // (Plugin Check's ABSPATHDetected warning is a false positive — | |
| 270 | + // wp-config.php cannot live in the uploads directory). | |
| 252 | 271 | file_put_contents($wp_config_path, $config_file); |
| 253 | 272 | } else { |
| 254 | 273 | wp_die('The wp-config.php file is not writable. Please check the file permissions.'); |
| 255 | 274 | } |
| @@ -741,9 +760,9 @@ | ||
| 741 | 760 | { |
| 742 | 761 | if (isset($_POST['metasync_debug_mode_action_advanced'])) { |
| 743 | 762 | if (!wp_verify_nonce($_POST['metasync_debug_mode_nonce_advanced'], 'metasync_debug_mode_action_advanced')) { |
| 744 | 763 | $redirect_url = admin_url('admin.php?page=' . $this->get_page_slug() . '&tab=advanced&debug_error=1'); |
| 745 | - wp_redirect($redirect_url); | |
| 764 | + wp_safe_redirect($redirect_url); | |
| 746 | 765 | exit; |
| 747 | 766 | } |
| 748 | 767 | |
| 749 | 768 | if (!current_user_can('manage_options')) { |
| @@ -751,9 +770,9 @@ | ||
| 751 | 770 | } |
| 752 | 771 | |
| 753 | 772 | if (!class_exists('Metasync_Debug_Mode_Manager')) { |
| 754 | 773 | $redirect_url = admin_url('admin.php?page=' . $this->get_page_slug() . '&tab=advanced&debug_error=1&msg=manager_not_available'); |
| 755 | - wp_redirect($redirect_url); | |
| 774 | + wp_safe_redirect($redirect_url); | |
| 756 | 775 | exit; |
| 757 | 776 | } |
| 758 | 777 | |
| 759 | 778 | $debug_manager = Metasync_Debug_Mode_Manager::get_instance(); |
| @@ -790,9 +809,9 @@ | ||
| 790 | 809 | $redirect_url = add_query_arg('debug_error', '1', $redirect_url); |
| 791 | 810 | break; |
| 792 | 811 | } |
| 793 | 812 | |
| 794 | - wp_redirect($redirect_url); | |
| 813 | + wp_safe_redirect($redirect_url); | |
| 795 | 814 | exit; |
| 796 | 815 | } |
| 797 | 816 | } |
| 798 | 817 | |
| @@ -806,27 +825,30 @@ | ||
| 806 | 825 | } |
| 807 | 826 | |
| 808 | 827 | if (isset($_POST['clear_log'])) { |
| 809 | 828 | if (wp_verify_nonce($_POST['clear_log_nonce'], 'metasync_clear_log_nonce')) { |
| 810 | - $log_file = WP_CONTENT_DIR . '/metasync_data/plugin_errors.log'; | |
| 829 | + $data_dir = Metasync_Data_Store::base_dir(); | |
| 830 | + $log_file = Metasync_Data_Store::file_path('plugin_errors.log'); | |
| 811 | 831 | |
| 812 | - if (file_exists($log_file)) { | |
| 832 | + if (false !== $log_file && file_exists($log_file)) { | |
| 813 | 833 | file_put_contents($log_file, ''); |
| 814 | 834 | |
| 815 | - $backup_files = glob(WP_CONTENT_DIR . '/metasync_data/plugin_errors.log.old.*'); | |
| 816 | - if ($backup_files) { | |
| 817 | - foreach ($backup_files as $backup_file) { | |
| 818 | - @unlink($backup_file); | |
| 835 | + if (false !== $data_dir) { | |
| 836 | + $backup_files = glob($data_dir . '/plugin_errors.log.old.*'); | |
| 837 | + if ($backup_files) { | |
| 838 | + foreach ($backup_files as $backup_file) { | |
| 839 | + @wp_delete_file($backup_file); | |
| 840 | + } | |
| 819 | 841 | } |
| 820 | 842 | } |
| 821 | 843 | } |
| 822 | 844 | |
| 823 | 845 | $redirect_url = admin_url('admin.php?page=' . $this->get_page_slug() . '&tab=advanced&log_cleared=1'); |
| 824 | - wp_redirect($redirect_url); | |
| 846 | + wp_safe_redirect($redirect_url); | |
| 825 | 847 | exit; |
| 826 | 848 | } else { |
| 827 | 849 | $redirect_url = admin_url('admin.php?page=' . $this->get_page_slug() . '&tab=advanced&clear_error=1'); |
| 828 | - wp_redirect($redirect_url); | |
| 850 | + wp_safe_redirect($redirect_url); | |
| 829 | 851 | exit; |
| 830 | 852 | } |
| 831 | 853 | } |
| 832 | 854 | |
| @@ -834,18 +856,18 @@ | ||
| 834 | 856 | if (wp_verify_nonce($_POST['clear_error_summary_nonce'], 'metasync_clear_error_summary_nonce')) { |
| 835 | 857 | if (class_exists('Metasync_Error_Logger')) { |
| 836 | 858 | Metasync_Error_Logger::clear_error_summary(); |
| 837 | 859 | $redirect_url = admin_url('admin.php?page=' . $this->get_page_slug() . '&tab=advanced&error_summary_cleared=1'); |
| 838 | - wp_redirect($redirect_url); | |
| 860 | + wp_safe_redirect($redirect_url); | |
| 839 | 861 | exit; |
| 840 | 862 | } else { |
| 841 | 863 | $redirect_url = admin_url('admin.php?page=' . $this->get_page_slug() . '&tab=advanced&error_summary_error=1'); |
| 842 | - wp_redirect($redirect_url); | |
| 864 | + wp_safe_redirect($redirect_url); | |
| 843 | 865 | exit; |
| 844 | 866 | } |
| 845 | 867 | } else { |
| 846 | 868 | $redirect_url = admin_url('admin.php?page=' . $this->get_page_slug() . '&tab=advanced&error_summary_error=1'); |
| 847 | - wp_redirect($redirect_url); | |
| 869 | + wp_safe_redirect($redirect_url); | |
| 848 | 870 | exit; |
| 849 | 871 | } |
| 850 | 872 | } |
| 851 | 873 | } |
| @@ -925,13 +947,13 @@ | ||
| 925 | 947 | 'triggered_by' => 'settings_reset_action' |
| 926 | 948 | ), 'info'); |
| 927 | 949 | |
| 928 | 950 | $redirect_url = admin_url('admin.php?page=' . $this->get_page_slug() . '&tab=advanced&settings_cleared=1'); |
| 929 | - wp_redirect($redirect_url); | |
| 951 | + wp_safe_redirect($redirect_url); | |
| 930 | 952 | exit; |
| 931 | 953 | } else { |
| 932 | 954 | $redirect_url = admin_url('admin.php?page=' . $this->get_page_slug() . '&tab=advanced&clear_settings_error=1'); |
| 933 | - wp_redirect($redirect_url); | |
| 955 | + wp_safe_redirect($redirect_url); | |
| 934 | 956 | exit; |
| 935 | 957 | } |
| 936 | 958 | } |
| 937 | 959 | } |
| @@ -946,9 +968,9 @@ | ||
| 946 | 968 | public function get_error_log_content() |
| 947 | 969 | { |
| 948 | 970 | $execution_time = $this->get_execution_setting('max_execution_time'); |
| 949 | 971 | if (function_exists('set_time_limit')) { |
| 950 | - @set_time_limit($execution_time); | |
| 972 | + @set_time_limit($execution_time); // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- runtime PHP setting with no WordPress API | |
| 951 | 973 | } |
| 952 | 974 | |
| 953 | 975 | $log_file = WP_CONTENT_DIR . '/debug.log'; |
| 954 | 976 | |
| @@ -980,9 +1002,9 @@ | ||
| 980 | 1002 | public function get_log_tail($file_path, $lines = null) |
| 981 | 1003 | { |
| 982 | 1004 | $execution_time = $this->get_execution_setting('max_execution_time'); |
| 983 | 1005 | if (function_exists('set_time_limit')) { |
| 984 | - @set_time_limit($execution_time); | |
| 1006 | + @set_time_limit($execution_time); // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- runtime PHP setting with no WordPress API | |
| 985 | 1007 | } |
| 986 | 1008 | |
| 987 | 1009 | if ($lines === null) { |
| 988 | 1010 | $lines = $this->get_execution_setting('log_batch_size'); |
| @@ -987,9 +1009,9 @@ | ||
| 987 | 1009 | if ($lines === null) { |
| 988 | 1010 | $lines = $this->get_execution_setting('log_batch_size'); |
| 989 | 1011 | } |
| 990 | 1012 | |
| 991 | - $handle = fopen($file_path, 'r'); | |
| 1013 | + $handle = fopen($file_path, 'r'); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- streaming file I/O; WP_Filesystem cannot return a raw stream handle | |
| 992 | 1014 | if (!$handle) { |
| 993 | 1015 | return false; |
| 994 | 1016 | } |
| 995 | 1017 | |
| @@ -1018,9 +1040,9 @@ | ||
| 1018 | 1040 | if (!empty($line) && $line_count < $lines) { |
| 1019 | 1041 | array_unshift($result_lines, strrev($line)); |
| 1020 | 1042 | } |
| 1021 | 1043 | |
| 1022 | - fclose($handle); | |
| 1044 | + fclose($handle); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- operates on a native stream handle | |
| 1023 | 1045 | |
| 1024 | 1046 | return implode("\n", $result_lines); |
| 1025 | 1047 | } |
| 1026 | 1048 | } |