diff --git a/.wp-env.json b/.wp-env.json index 2eba13842..57c1c9aea 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -14,6 +14,7 @@ "WP_DEBUG_DISPLAY": false }, "mappings": { + "wp-content/mu-plugins": "./tests/e2e/mu-plugins", "wp-content/themes/gutenberg-test-themes/twentytwentyone": "https://downloads.wordpress.org/theme/twentytwentyone.2.1.zip", "wp-content/themes/gutenberg-test-themes/twentytwentyfour": "https://downloads.wordpress.org/theme/twentytwentyfour.1.0.zip" } diff --git a/css/settings.css b/css/settings.css index c555bfaf0..e365c7e8a 100644 --- a/css/settings.css +++ b/css/settings.css @@ -2821,6 +2821,34 @@ button.feedzy-action-button { margin-top: 10px; } +/* Import/feed attribution badges */ +.fz-log-container__badges { + display: flex; + flex-wrap: wrap; + gap: 6px; + margin-top: 6px; +} + +.fz-log-context-import, +.fz-log-context-feed-url { + display: inline-block; + padding: 1px 8px; + border-radius: 10px; + font-size: 0.85em; + word-break: break-all; + overflow-wrap: break-word; +} + +.fz-log-context-import { + background-color: #eef2ff; + color: #3730a3; +} + +.fz-log-context-feed-url { + background-color: #ecfdf5; + color: #065f46; +} + /* Right section - Context */ .fz-log-container__right { flex: 1; diff --git a/form/form.php b/form/form.php index 582d91476..feba4f88b 100644 --- a/form/form.php +++ b/form/form.php @@ -12,6 +12,10 @@ * @subpackage feedzy-rss-feeds/form */ +if ( ! defined( 'ABSPATH' ) ) { + exit; +} + $html_parts = Feedzy_Rss_Feeds_Ui_Lang::get_form_elements(); ?> diff --git a/includes/abstract/feedzy-rss-feeds-admin-abstract.php b/includes/abstract/feedzy-rss-feeds-admin-abstract.php index 6e83a4b55..e9587629f 100644 --- a/includes/abstract/feedzy-rss-feeds-admin-abstract.php +++ b/includes/abstract/feedzy-rss-feeds-admin-abstract.php @@ -899,15 +899,26 @@ private function init_single_feed( $feed_url, $cache, $sc, $allow_https = FEEDZY $feed->init(); - if ( ! $feed->get_type() ) { - return $feed; - } - $error = $feed->error(); if ( is_array( $error ) ) { $error = implode( '|', $error ); } + if ( ! $feed->get_type() ) { + if ( ! empty( $error ) ) { + Feedzy_Rss_Feeds_Log::error( + // translators: %1$s is the feed URL, %2$s is the error message. + sprintf( __( 'Error while parsing feed URL "%1$s": %2$s', 'feedzy-rss-feeds' ), $feed_url, $error ), + array( + 'feed_url' => $feed_url, + 'cache' => $cache, + 'sc' => $sc, + ) + ); + } + return $feed; + } + if ( ! empty( $error ) ) { $feed = $this->handle_feed_error( $feed, $cloned_feed, $feed_url, $cache, $sc, $allow_https, $default_agent, $error ); } @@ -1074,10 +1085,10 @@ private function get_lazyload_cache_key( $sc, $feed_url ) { * @param array $sc The shortcode attributes. * @param bool $allow_https Whether to allow HTTPS. * - * @return Feedzy_Rss_Feeds_Util_SimplePie SimplePie instance. + * @return Feedzy_Rss_Feeds_Util_Feed SimplePie instance. */ private function create_simplepie_instance( array $sc, $allow_https ) { - $feed = new Feedzy_Rss_Feeds_Util_SimplePie( $sc ); + $feed = new Feedzy_Rss_Feeds_Util_Feed( $sc ); if ( ! $allow_https && method_exists( $feed, 'set_curl_options' ) ) { $feed->set_curl_options( @@ -1198,8 +1209,9 @@ private function handle_feed_error( $feed, $cloned_feed, $feed_url, $cache, $sc, Feedzy_Rss_Feeds_Log::debug( sprintf( 'Using raw data for feed: %s', $feed_url ), array( - 'cache' => $cache, - 'sc' => $sc, + 'feed_url' => $feed_url, + 'cache' => $cache, + 'sc' => $sc, ) ); diff --git a/includes/admin/feedzy-rss-feeds-actions.php b/includes/admin/feedzy-rss-feeds-actions.php index 604684924..524c4a9de 100644 --- a/includes/admin/feedzy-rss-feeds-actions.php +++ b/includes/admin/feedzy-rss-feeds-actions.php @@ -9,6 +9,10 @@ * @subpackage feedzy-rss-feeds/includes/admin */ +if ( ! defined( 'ABSPATH' ) ) { + exit; +} + if ( ! class_exists( 'Feedzy_Rss_Feeds_Actions' ) ) { /** * Singleton class for content action process. diff --git a/includes/admin/feedzy-rss-feeds-import.php b/includes/admin/feedzy-rss-feeds-import.php index 0827bc946..569020a3c 100644 --- a/includes/admin/feedzy-rss-feeds-import.php +++ b/includes/admin/feedzy-rss-feeds-import.php @@ -1413,7 +1413,9 @@ private function run_now() { isset( $job->post_title ) ? $job->post_title : 'Unknown Job' ), array( - 'job_id' => $job_id, + 'job_id' => $job_id, + 'import_id' => (int) $job_id, + 'import_title' => isset( $job->post_title ) ? $job->post_title : '', ) ); @@ -1612,8 +1614,10 @@ public function run_cron( $max = 100, $job_id = 0 ) { Feedzy_Rss_Feeds_Log::debug( 'Cron job run for: ' . $job->post_title, array( - 'job_id' => $job->ID, - 'result' => $result, + 'job_id' => $job->ID, + 'import_id' => $job->ID, + 'import_title' => $job->post_title, + 'result' => $result, ) ); @@ -1623,8 +1627,10 @@ public function run_cron( $max = 100, $job_id = 0 ) { Feedzy_Rss_Feeds_Log::debug( 'Previous run did not return any results, running again for job: ' . $job->post_title, array( - 'job_id' => $job->ID, - 'result' => $result, + 'job_id' => $job->ID, + 'import_id' => $job->ID, + 'import_title' => $job->post_title, + 'result' => $result, ) ); } @@ -1638,8 +1644,10 @@ public function run_cron( $max = 100, $job_id = 0 ) { // translators: %1$s is the import job title, %2$s is the error message. sprintf( __( 'Error when running "%1$s": %2$s', 'feedzy-rss-feeds' ), $job->post_title, $e->getMessage() ), array( - 'job_id' => $job->ID, - 'error' => $e->getMessage(), + 'job_id' => $job->ID, + 'import_id' => $job->ID, + 'import_title' => $job->post_title, + 'error' => $e->getMessage(), ) ); } @@ -1647,17 +1655,50 @@ public function run_cron( $max = 100, $job_id = 0 ) { } /** - * Runs a specific job. - * + * Runs a specific job, scoping the logger context to it. + * + * Every log entry written while the job runs (including feed fetch/parse + * errors logged deeper in the stack) is tagged with the import ID, title + * and source, so it can be traced back to this job. + * * @param \WP_Post $job The custom post type with the job options. * @param int $max The import feed limit. * * @return int The number of imported items. - * + * * @since 1.6.1 * @access private */ private function run_job( $job, $max ) { + $logger = Feedzy_Rss_Feeds_Log::get_instance(); + $previous_context = $logger->get_context(); + $logger->set_context( + array( + 'import_id' => $job->ID, + 'import_title' => $job->post_title, + 'source' => (string) get_post_meta( $job->ID, 'source', true ), + ) + ); + + try { + return $this->run_job_logic( $job, $max ); + } finally { + $logger->set_context( $previous_context ); + } + } + + /** + * The logic of running a specific job. + * + * @param \WP_Post $job The custom post type with the job options. + * @param int $max The import feed limit. + * + * @return int The number of imported items. + * + * @since 1.6.1 + * @access private + */ + private function run_job_logic( $job, $max ) { Feedzy_Rss_Feeds_Usage::get_instance()->track_rss_import(); Feedzy_Rss_Feeds_Log::get_instance()->enable_error_messages_retention(); @@ -1708,7 +1749,8 @@ private function run_job( $job, $max ) { 'inc_key' => $inc_key, 'inc_on' => $inc_on, 'exc_on' => $exc_on, - 'import_title' => $import_title, + // `import_title` is reserved for the job attribution set in run_job(); this is the post-title mapping template. + 'import_post_title' => $import_title, 'import_date' => $import_date, 'post_excerpt' => $post_excerpt, 'import_content' => $import_content, @@ -3156,8 +3198,7 @@ private function try_save_featured_image( $img_source_url, $post_id, $post_title ) ); - // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_unlink - unlink( $local_file ); + wp_delete_file( $local_file ); return false; } @@ -3174,8 +3215,7 @@ private function try_save_featured_image( $img_source_url, $post_id, $post_title ) ); - // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_unlink - unlink( $local_file ); + wp_delete_file( $local_file ); return false; } @@ -3187,7 +3227,7 @@ private function try_save_featured_image( $img_source_url, $post_id, $post_title $correct_local_file = preg_replace( '/\.[a-z0-9]+$/i', $correct_extension, $local_file ); if ( $correct_local_file !== $local_file ) { - // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_rename + // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_rename, WordPress.WP.AlternativeFunctions.rename_rename -- renaming a temp file created by download_url(). if ( rename( $local_file, $correct_local_file ) ) { $local_file = $correct_local_file; } else { @@ -3201,8 +3241,7 @@ private function try_save_featured_image( $img_source_url, $post_id, $post_title ) ); - // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_unlink - unlink( $local_file ); + wp_delete_file( $local_file ); return false; } @@ -3283,7 +3322,7 @@ private function try_save_featured_image( $img_source_url, $post_id, $post_title $extension = ! empty( $extension ) ? '.' . $extension : str_replace( 'image/', '.', $type ); $new_local_file = preg_replace( '/\.tmp$/', $extension, $local_file ); - // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_rename + // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_rename, WordPress.WP.AlternativeFunctions.rename_rename -- renaming a temp file created by download_url(). $renamed = rename( $local_file, $new_local_file ); if ( $renamed ) { $local_file = $new_local_file; @@ -3313,8 +3352,7 @@ private function try_save_featured_image( $img_source_url, $post_id, $post_title ) ); - // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_unlink - unlink( $local_file ); + wp_delete_file( $local_file ); return false; } @@ -3334,10 +3372,8 @@ private function try_save_featured_image( $img_source_url, $post_id, $post_title ) ); - // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_unlink if ( file_exists( $file_array['tmp_name'] ) ) { - // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_unlink - unlink( $file_array['tmp_name'] ); + wp_delete_file( $file_array['tmp_name'] ); } return false; diff --git a/includes/admin/feedzy-rss-feeds-log.php b/includes/admin/feedzy-rss-feeds-log.php index 6c0a35c37..2a1cb2400 100644 --- a/includes/admin/feedzy-rss-feeds-log.php +++ b/includes/admin/feedzy-rss-feeds-log.php @@ -293,6 +293,19 @@ public function set_context( array $context ) { return $this; } + /** + * Get the current context of the logger. + * + * Useful for saving the context before temporarily overriding it, so it + * can be restored afterwards. + * + * @since 5.2.6 + * @return array The current context. + */ + public function get_context() { + return $this->context; + } + /** * Log a message. * @@ -611,6 +624,7 @@ private function tail_file( $lines = 50 ) { break; } } + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- direct-access fast path, guarded by can_use_direct_file_access(). fclose( $handle ); return $text; @@ -740,6 +754,7 @@ public function export_logs_endpoint( $request ) { header( 'Pragma: public' ); header( 'Content-Length: ' . filesize( $this->filepath ) ); + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile -- streams the log download without loading it into memory. readfile( $this->filepath ); exit; diff --git a/includes/admin/feedzy-rss-feeds-options.php b/includes/admin/feedzy-rss-feeds-options.php index 4abae3a40..088f431a5 100644 --- a/includes/admin/feedzy-rss-feeds-options.php +++ b/includes/admin/feedzy-rss-feeds-options.php @@ -9,6 +9,10 @@ * @subpackage feedzy-rss-feeds/includes/admin */ +if ( ! defined( 'ABSPATH' ) ) { + exit; +} + if ( ! class_exists( 'Feedy_Rss_Feeds_Options' ) ) { /** * Singleton class for options wrapper diff --git a/includes/admin/feedzy-rss-feeds-upgrader.php b/includes/admin/feedzy-rss-feeds-upgrader.php index 7231a7823..851a0a4b0 100644 --- a/includes/admin/feedzy-rss-feeds-upgrader.php +++ b/includes/admin/feedzy-rss-feeds-upgrader.php @@ -9,6 +9,10 @@ * @subpackage feedzy-rss-feeds/includes/admin */ +if ( ! defined( 'ABSPATH' ) ) { + exit; +} + if ( ! class_exists( 'Feedzy_Rss_Feeds_Upgrader' ) ) { /** * Class Feedzy_Rss_Feeds_Upgrader for upgrading processes diff --git a/includes/feedzy-rss-feeds-feed-tweaks.php b/includes/feedzy-rss-feeds-feed-tweaks.php index 953dd6bc0..45c84fee1 100644 --- a/includes/feedzy-rss-feeds-feed-tweaks.php +++ b/includes/feedzy-rss-feeds-feed-tweaks.php @@ -9,6 +9,10 @@ * @subpackage feedzy-rss-feeds/includes */ +if ( ! defined( 'ABSPATH' ) ) { + exit; +} + /** * Adds the featured image to the website's RSS feed. * diff --git a/includes/layouts/feedzy-documentation.php b/includes/layouts/feedzy-documentation.php index 82737838b..d23996a9b 100644 --- a/includes/layouts/feedzy-documentation.php +++ b/includes/layouts/feedzy-documentation.php @@ -1,3 +1,8 @@ +