get_existing_wp_debug_log_path(); $health = '' !== $path ? $this->inspect_existing_wp_debug_backend( $path ) : $this->empty_health( 'not_configured' ); if ( ! in_array( $health['status'] ?? 'failed', array( 'ok', 'pending' ), true ) ) { $this->settings->set_log_reads_health( $health ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Message is plain translated admin text with no user-provided HTML. throw new RuntimeException( $this->build_enable_failure_message( $path, $health ) ); } $expires = time() + self::TTL_SECONDS; $this->settings->set_log_reads_expires_at( $expires ); $this->settings->set_log_reads_backend( 'existing_wp_debug' ); $this->settings->set_log_reads_path( $path ); $this->settings->set_log_reads_health( $health ); $this->settings->set_log_reads_active( true ); $this->schedule_disable( $expires ); } /** * Disable the temporary log-read window. * * @param string $reason Reason the window was disabled. */ public function disable( string $reason = 'manual' ): void { unset( $reason ); $this->unschedule_disable(); $this->settings->set_log_reads_active( false ); $this->settings->set_log_reads_backend( null ); $this->settings->set_log_reads_path( null ); $this->settings->set_log_reads_expires_at( 0 ); $this->settings->set_log_reads_health( array() ); if ( $this->settings->get_allow_log_reads() ) { $this->settings->set_allow_log_reads( false ); } } /** * Refresh the 72-hour read window if it is still valid. * * @throws RuntimeException When the configured debug log is no longer readable. */ public function refresh_expiry(): void { if ( ! $this->settings->is_log_reads_active() ) { return; } $path = $this->settings->get_log_reads_path(); $health = '' !== $path ? $this->inspect_existing_wp_debug_backend( $path ) : $this->empty_health( 'missing_path' ); if ( ! in_array( $health['status'] ?? 'failed', array( 'ok', 'pending' ), true ) ) { $this->settings->set_log_reads_health( $health ); $this->disable( 'debug_log_unavailable' ); throw new RuntimeException( esc_html__( 'The configured WordPress debug log file is no longer readable.', 'actionpanel-ai' ) ); } $expires = time() + self::TTL_SECONDS; $this->settings->set_log_reads_expires_at( $expires ); $this->settings->set_log_reads_health( $health ); $this->schedule_disable( $expires ); } /** * Enforce expiry and availability for the read-only log-sharing window. */ public function maybe_bootstrap(): void { if ( ! $this->settings->get_allow_log_reads() && $this->settings->is_log_reads_active() ) { $this->disable( 'sync' ); return; } if ( ! $this->settings->is_log_reads_active() ) { return; } $expires = $this->settings->get_log_reads_expires_at(); if ( $expires && time() >= $expires ) { $this->disable( 'expired' ); return; } $path = $this->settings->get_log_reads_path(); $health = '' !== $path ? $this->inspect_existing_wp_debug_backend( $path ) : $this->empty_health( 'missing_path' ); $this->settings->set_log_reads_health( $health ); if ( 'failed' === ( $health['status'] ?? 'failed' ) ) { $this->disable( 'debug_log_unavailable' ); } } /** * Describe the current debug log status for administrator-facing UI. */ public function get_status_notice(): array { $path = $this->get_existing_wp_debug_log_path(); $health = '' !== $path ? $this->inspect_existing_wp_debug_backend( $path ) : $this->empty_health( 'not_configured' ); $status = $health['status'] ?? 'failed'; if ( 'ok' === $status ) { return array( 'type' => 'success', 'message' => esc_html__( 'Debug log file is available and readable.', 'actionpanel-ai' ), ); } if ( 'pending' === $status ) { return array( 'type' => 'info', 'message' => esc_html__( 'Debug logging is configured. Waiting for WordPress to create the log file.', 'actionpanel-ai' ), ); } return array( 'type' => 'error', 'message' => $this->build_enable_failure_message( $path, $health ), ); } /** * Cron callback for expiring the temporary read window. */ public function handle_cron_disable(): void { $this->disable( 'cron' ); } /** * Resolve the existing WordPress debug log path for read-only access. * * This intentionally supports only an explicit absolute file path set in * `WP_DEBUG_LOG`. * * The plugin does not scan for alternate log files and does not create, * manage, or relocate logs. */ private function get_existing_wp_debug_log_path(): string { if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { return ''; } if ( ! defined( 'WP_DEBUG_LOG' ) || ! WP_DEBUG_LOG ) { return ''; } if ( in_array( strtolower( (string) WP_DEBUG_LOG ), array( 'true', '1' ), true ) ) { return ''; } if ( ! is_string( WP_DEBUG_LOG ) || '' === trim( WP_DEBUG_LOG ) ) { return ''; } return trim( WP_DEBUG_LOG ); } /** * Inspect whether the configured WordPress debug log can be read. * * @param string $path Debug log path. */ private function inspect_existing_wp_debug_backend( string $path ): array { $path_absolute = '' !== $path && ( str_starts_with( $path, '/' ) || 1 === preg_match( '/^[A-Za-z]:[\\\\\\/]/', $path ) ); $path_exists = '' !== $path && file_exists( $path ); $path_readable = $path_exists && is_readable( $path ); $status = 'failed'; $reason = ''; if ( ! $path_absolute ) { $reason = 'relative_path'; } elseif ( ! $path_exists ) { $status = 'pending'; $reason = 'not_created'; } elseif ( $path_readable ) { $status = 'ok'; } else { $reason = 'not_readable'; } return array( 'status' => $status, 'backend' => 'existing_wp_debug', 'path_source' => $this->get_debug_log_path_source( $path ), 'updated_at' => time(), 'reason' => $reason, 'checks' => array( 'wp_debug_enabled' => defined( 'WP_DEBUG' ) && WP_DEBUG, 'wp_debug_log_configured' => '' !== $path, 'path_absolute' => $path_absolute, 'path_exists' => $path_exists, 'path_readable' => $path_readable, ), ); } /** * Build a failed health payload when no candidate log path exists. * * @param string $reason Failure reason. */ private function empty_health( string $reason ): array { return array( 'status' => 'failed', 'backend' => 'existing_wp_debug', 'path_source' => 'unresolved', 'updated_at' => time(), 'reason' => $reason, 'checks' => array( 'wp_debug_enabled' => defined( 'WP_DEBUG' ) && WP_DEBUG, 'wp_debug_log_configured' => false, 'path_exists' => false, 'path_readable' => false, ), ); } /** * Build a concise administrator-facing failure message for enabling log reads. * * @param string $path Candidate debug log path. * @param array $health Debug log health details. */ private function build_enable_failure_message( string $path, array $health ): string { $checks = isset( $health['checks'] ) && is_array( $health['checks'] ) ? $health['checks'] : array(); $wp_debug_enabled = ! empty( $checks['wp_debug_enabled'] ); $path_configured = ! empty( $checks['wp_debug_log_configured'] ); $path_absolute = ! empty( $checks['path_absolute'] ); $path_exists = ! empty( $checks['path_exists'] ); $path_readable = ! empty( $checks['path_readable'] ); if ( ! $wp_debug_enabled ) { return esc_html__( 'Log reads require WP_DEBUG to be enabled.', 'actionpanel-ai' ); } if ( ! $path_configured ) { return esc_html__( 'Set WP_DEBUG_LOG to an absolute file path before enabling log reads.', 'actionpanel-ai' ); } if ( ! $path_absolute ) { return esc_html__( 'Set WP_DEBUG_LOG to an absolute readable file path before enabling log reads.', 'actionpanel-ai' ); } if ( ! $path_exists ) { return esc_html__( 'The configured WP_DEBUG_LOG file does not exist yet.', 'actionpanel-ai' ); } if ( ! $path_readable ) { return esc_html__( 'The configured WP_DEBUG_LOG file is not readable by PHP.', 'actionpanel-ai' ); } return esc_html__( 'No readable WordPress debug log file is configured.', 'actionpanel-ai' ); } /** * Identify how the current WordPress debug log path was resolved. * * @param string $path Candidate debug log path. */ private function get_debug_log_path_source( string $path ): string { if ( '' === $path ) { return 'unresolved'; } if ( defined( 'WP_DEBUG_LOG' ) && is_string( WP_DEBUG_LOG ) && '' !== trim( WP_DEBUG_LOG ) ) { $normalized = trim( WP_DEBUG_LOG ); if ( ! in_array( strtolower( $normalized ), array( 'true', '1' ), true ) ) { return 'explicit_wp_debug_log'; } } return 'explicit_wp_debug_log'; } /** * Schedule automatic disabling after the temporary sharing window expires. * * @param int $timestamp Expiration timestamp. */ private function schedule_disable( int $timestamp ): void { $this->unschedule_disable(); if ( $timestamp > time() ) { wp_schedule_single_event( $timestamp, self::CRON_HOOK ); } } /** * Remove pending expiry events. */ private function unschedule_disable(): void { $next = wp_next_scheduled( self::CRON_HOOK ); while ( false !== $next ) { wp_unschedule_event( $next, self::CRON_HOOK ); $next = wp_next_scheduled( self::CRON_HOOK ); } } }