-
Notifications
You must be signed in to change notification settings - Fork 39
Log to file #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tijmenbruggeman
wants to merge
24
commits into
tinify:master
Choose a base branch
from
wcreateweb:task/logging-troubleshooting
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Log to file #84
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2a3283c
chore: add settings view
tijmenbruggeman 1625d03
chore: add logger singleton
tijmenbruggeman d3e3be1
chore: add diagnostics class
tijmenbruggeman f38b3c7
chore: download zip file from settings page
tijmenbruggeman 25bbc46
chore: add loader and improve markup
tijmenbruggeman 47cd48f
Add various log points
tijmenbruggeman df29291
Set timeout and proxy options on curl client
tijmenbruggeman 98eade7
Scaffold tests
tijmenbruggeman e72c663
formatting
tijmenbruggeman 178d63f
Add initial tests
tijmenbruggeman 131a86a
On constructor log enabled test
tijmenbruggeman 2cfabf1
Manual formatting logger
tijmenbruggeman f54b12d
Manual formatting fixes in diagnostics
tijmenbruggeman 9adff72
chore: add tests for log path
tijmenbruggeman 76ce4cb
chore: simplify rotation
tijmenbruggeman 396ccbb
Format
tijmenbruggeman 6cfae11
change file name, format and size
tijmenbruggeman d430ff6
Clear logs when turned on
tijmenbruggeman e4c3ef3
Add more tests
tijmenbruggeman 1d7cd63
Add mocks for test
tijmenbruggeman 85f8158
set timeout to 300
tijmenbruggeman cd56cab
Merge branch 'master' of github.com:wcreateweb/wordpress-plugin into …
tijmenbruggeman f995de4
Seperate API timeout and connect timeout
tijmenbruggeman 42623d1
remove server info and add a phpinfo file
tijmenbruggeman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,259 @@ | ||
| <?php | ||
| /* | ||
| * Tiny Compress Images - WordPress plugin. | ||
| * Copyright (C) 2015-2026 Tinify B.V. | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License as published by the Free | ||
| * Software Foundation; either version 2 of the License, or (at your option) | ||
| * any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| * more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License along | ||
| * with this program; if not, write to the Free Software Foundation, Inc., 51 | ||
| * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| */ | ||
|
|
||
| /** | ||
| * Collects diagnostic information and generates downloadable zip files. | ||
| * | ||
| * @since 3.7.0 | ||
| */ | ||
| class Tiny_Diagnostics { | ||
|
|
||
| /** | ||
| * Tiny settings | ||
| * | ||
| * @var Tiny_Settings | ||
| */ | ||
| private $settings; | ||
|
|
||
| /** | ||
| * @param Tiny_Settings $settings | ||
| */ | ||
| public function __construct( $settings ) { | ||
| $this->settings = $settings; | ||
|
|
||
| add_action( | ||
| 'wp_ajax_tiny_download_diagnostics', | ||
| array( $this, 'download_diagnostics' ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Collects all diagnostic information. | ||
| * | ||
| * File contains: | ||
| * - timestamp of export | ||
| * - server information | ||
| * - site information | ||
| * - plugin list | ||
| * - tinify settings | ||
| * - image settings | ||
| * - logs | ||
| * | ||
| * @since 3.7.0 | ||
| * | ||
| * @return array Array of diagnostic information. | ||
| */ | ||
| public function collect_info() { | ||
| $info = array( | ||
| 'timestamp' => current_time( 'Y-m-d H:i:s' ), | ||
| 'site_info' => self::get_site_info(), | ||
| 'active_plugins' => self::get_active_plugins(), | ||
| 'tiny_info' => $this->get_tiny_info(), | ||
| 'image_sizes' => $this->settings->get_active_tinify_sizes(), | ||
| ); | ||
|
|
||
| return $info; | ||
| } | ||
|
|
||
| /** | ||
| * Gets site information. | ||
| * | ||
| * @since 3.7.0 | ||
| * | ||
| * @return array Site information. | ||
| */ | ||
| private static function get_site_info() { | ||
| global $wp_version; | ||
| $theme = wp_get_theme(); | ||
|
|
||
| return array( | ||
| 'wp_version' => $wp_version, | ||
| 'site_url' => get_site_url(), | ||
| 'home_url' => get_home_url(), | ||
| 'is_multisite' => is_multisite(), | ||
| 'site_language' => get_locale(), | ||
| 'timezone' => wp_timezone_string(), | ||
| 'theme_name' => $theme->get( 'Name' ), | ||
| 'theme_version' => $theme->get( 'Version' ), | ||
| 'theme_uri' => $theme->get( 'ThemeURI' ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Gets list of active plugins. | ||
| * | ||
| * @since 3.7.0 | ||
| * | ||
| * @return array List of active plugins. | ||
| */ | ||
| private static function get_active_plugins() { | ||
| $active_plugins = get_option( 'active_plugins', array() ); | ||
| $plugins = array(); | ||
|
|
||
| foreach ( $active_plugins as $plugin ) { | ||
| $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); | ||
| $plugins[] = array( | ||
| 'name' => $plugin_data['Name'], | ||
| 'version' => $plugin_data['Version'], | ||
| 'author' => $plugin_data['Author'], | ||
| 'file' => $plugin, | ||
| ); | ||
| } | ||
|
|
||
| return $plugins; | ||
| } | ||
|
|
||
| /** | ||
| * Gets TinyPNG plugin info & settings. | ||
| * | ||
| * @since 3.7.0 | ||
| * | ||
| * @return array Plugin settings | ||
| */ | ||
| private function get_tiny_info() { | ||
| return array( | ||
| 'version' => Tiny_Plugin::version(), | ||
| 'status' => $this->settings->get_status(), | ||
| 'php_client_supported' => Tiny_PHP::client_supported(), | ||
|
|
||
| 'compression_count' => $this->settings->get_compression_count(), | ||
| 'compression_timing' => $this->settings->get_compression_timing(), | ||
| 'conversion' => $this->settings->get_conversion_options(), | ||
| 'paying_state' => $this->settings->get_paying_state(), | ||
| ); | ||
| } | ||
|
|
||
| public function download_diagnostics() { | ||
| $zippath = $this->create_diagnostic_zip(); | ||
| return $this->download_zip( $zippath ); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a diagnostic zip file. | ||
| * | ||
| * @since 3.7.0 | ||
| * | ||
| * @return string|WP_Error Path to the created zip file or WP_Error on failure. | ||
| */ | ||
| public function create_diagnostic_zip() { | ||
| if ( ! class_exists( 'ZipArchive' ) ) { | ||
| return new WP_Error( | ||
| 'zip_not_available', | ||
| __( 'ZipArchive class is not available on this server.', | ||
| 'tiny-compress-images' | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| $upload_dir = wp_upload_dir(); | ||
| $temp_dir = trailingslashit( $upload_dir['basedir'] ) . 'tiny-compress-temp'; | ||
| if ( ! file_exists( $temp_dir ) ) { | ||
| wp_mkdir_p( $temp_dir ); | ||
| } | ||
|
|
||
| $zip_filename = 'tiny-compress-diagnostics-' . gmdate( 'Y-m-d-His' ) . '.zip'; | ||
| $zip_path = trailingslashit( $temp_dir ) . $zip_filename; | ||
|
|
||
| $zip = new ZipArchive(); | ||
| if ( true !== $zip->open( $zip_path, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) { | ||
| return new WP_Error( 'zip_create_failed', | ||
| __( 'Failed to create zip file.', | ||
| 'tiny-compress-images' ) | ||
| ); | ||
| } | ||
|
|
||
| // Add diagnostic info. | ||
| $info = self::collect_info(); | ||
| $zip->addFromString( 'tiny-diagnostics.json', wp_json_encode( $info, JSON_PRETTY_PRINT ) ); | ||
|
|
||
| // Add phpinfo HTML. | ||
| ob_start(); | ||
| phpinfo( INFO_GENERAL ); | ||
| phpinfo( INFO_CONFIGURATION ); | ||
| phpinfo( INFO_MODULES ); | ||
| $phpinfo_html = ob_get_clean(); | ||
| $zip->addFromString( 'phpinfo.html', $phpinfo_html ); | ||
|
|
||
| // Add log files. | ||
| $logger = Tiny_Logger::get_instance(); | ||
| $log_files = $logger->get_log_files(); | ||
|
|
||
| foreach ( $log_files as $log_file ) { | ||
| if ( file_exists( $log_file ) ) { | ||
| $zip->addFile( $log_file, 'logs/' . basename( $log_file ) ); | ||
| } | ||
| } | ||
|
|
||
| $zip->close(); | ||
| return $zip_path; | ||
| } | ||
|
|
||
| /** | ||
| * Downloads and removes the zip | ||
| * | ||
| * @since 3.7.0 | ||
| * | ||
| * @param string $zip_path Path to the zip file. | ||
| */ | ||
| public static function download_zip( $zip_path ) { | ||
| if ( ! file_exists( $zip_path ) ) { | ||
| wp_die( esc_html__( 'Diagnostic file not found.', 'tiny-compress-images' ) ); | ||
| } | ||
|
|
||
| header( 'Content-Type: application/zip' ); | ||
| header( 'Content-Disposition: attachment; filename="' . basename( $zip_path ) . '"' ); | ||
| header( 'Content-Length: ' . filesize( $zip_path ) ); | ||
| header( 'Pragma: no-cache' ); | ||
| header( 'Expires: 0' ); | ||
|
|
||
| // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile | ||
| readfile( $zip_path ); | ||
|
|
||
| // Clean up. | ||
| // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink | ||
| unlink( $zip_path ); | ||
|
|
||
| exit; | ||
| } | ||
|
|
||
| /** | ||
| * Cleans up old diagnostic zip files. | ||
| * | ||
| * @since 3.7.0 | ||
| */ | ||
| public static function cleanup_old_diagnostics() { | ||
| $upload_dir = wp_upload_dir(); | ||
| $temp_dir = trailingslashit( $upload_dir['basedir'] ) . 'tiny-compress-temp'; | ||
|
|
||
| if ( ! file_exists( $temp_dir ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $files = glob( trailingslashit( $temp_dir ) . 'tiny-compress-diagnostics-*.zip' ); | ||
| $max_age = DAY_IN_SECONDS; // 1 day. | ||
|
|
||
| foreach ( $files as $file ) { | ||
| if ( file_exists( $file ) && (time() - filemtime( $file )) > $max_age ) { | ||
| // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink | ||
| unlink( $file ); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing docblock for CONNECT_TIMEOUT constant. The API_TIMEOUT constant has documentation but CONNECT_TIMEOUT does not.