Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/admin/feedzy-rss-feeds-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1275,8 +1275,8 @@ private function save_settings() {
return;
}
$post_tab = isset( $_POST['tab'] ) ? sanitize_text_field( wp_unslash( $_POST['tab'] ) ) : '';

$settings = apply_filters( 'feedzy_get_settings', array() );

switch ( $post_tab ) {
case 'general':
$auto_categories_raw = array();
Expand Down
7 changes: 4 additions & 3 deletions includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -3248,14 +3248,15 @@ public function integration_tabs( $tabs ) {
public function save_tab_settings( $settings, $tab ) {
if (
! isset( $_POST['nonce'] ) ||
! wp_verify_nonce( sanitize_text_field( wp_unslash( 'nonce' ) ), $tab )
! wp_verify_nonce( sanitize_text_field( $_POST['nonce'] ), $tab )
) {
return array();
}

if ( 'misc' === $tab ) {
$settings['canonical'] = isset( $_POST['canonical'] ) ? absint( $_POST['canonical'] ) : 0;
$settings['general']['rss-feeds'] = isset( $_POST['rss-feeds'] ) ? absint( $_POST['rss-feeds'] ) : '';
$settings['canonical'] = isset( $_POST['canonical'] ) ? absint( $_POST['canonical'] ) : 0;
$settings['general']['rss-feeds'] = isset( $_POST['rss-feeds'] ) ? absint( $_POST['rss-feeds'] ) : '';
$settings['logs']['email_frequency'] = isset( $_POST['logs-email-frequency'] ) ? sanitize_text_field( wp_unslash( $_POST['logs-email-frequency'] ) ) : '';
}

return $settings;
Expand Down
41 changes: 38 additions & 3 deletions includes/admin/feedzy-rss-feeds-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,76 +16,87 @@ class Feedzy_Rss_Feeds_Log {
/**
* Option key for storing log statistics.
*
* @since 5.1.0
* @var string Option key for storing log statistics.
*/
const STATS_OPTION_KEY = 'feedzy_log_stats';

/**
* Debug level.
*
* @since 5.1.0
* @var int Debug level.
*/
const DEBUG = 100;

/**
* Info level.
*
* @since 5.1.0
* @var int Info level.
*/
const INFO = 200;

/**
* Warning level.
*
* @since 5.1.0
* @var int Warning level.
*/
const WARNING = 300;

/**
* Error level.
*
* @since 5.1.0
* @var int Error level.
*/
const ERROR = 400;

/**
* Ignore level.
*
* @since 5.1.0
* @var int Ignore level.
*/
const NONE = 500;

/**
* Log file name.
*
* @since 5.1.0
* @var string Default log name.
*/
const FILE_NAME = 'feedzy';

/**
* Log file extension.
*
* @since 5.1.0
* @var string Log file extension.
*/
const FILE_EXT = '.jsonl';

/**
* Default max file size.
*
* @since 5.1.0
* @var int Default max file size in bytes (50MB).
*/
const DEFAULT_MAX_FILE_SIZE = 50 * 1024 * 1024;

/**
* Default max files.
*
* @since 5.1.0
* @var int Default max number of log files.
*/
const DEFAULT_MAX_FILES = 3;

/**
* Log levels.
*
* @since 5.1.0
* @var array<int, string> Log levels.
*/
private static $levels = array(
Expand All @@ -95,6 +106,12 @@ class Feedzy_Rss_Feeds_Log {
self::ERROR => 'error',
);

/**
* Priorities mapping.
*
* @since 5.1.0
* @var array<string, int> Priorities mapping.
*/
const PRIORITIES_MAPPING = array(
'debug' => self::DEBUG,
'info' => self::INFO,
Expand All @@ -106,62 +123,79 @@ class Feedzy_Rss_Feeds_Log {
/**
* The single instance of the class.
*
* @since 5.1.0
* @var ?self The single instance of the class.
*/
private static $instance = null;

/**
* The path to the log file.
*
* @since 5.1.0
* @var string The path to the log file.
*/
private $filepath;

/**
* The WordPress filesystem instance.
*
* @since 5.1.0
* @var \WP_Filesystem_Base|null The WordPress filesystem instance.
*/
private $filesystem;

/**
* The context for the logger.
*
* @since 5.1.0
* @var array<string, mixed> The context for the logger.
*/
private $context = array();

/**
* The minimum log level threshold for logging messages.
*
* @since 5.1.0
* @var int The minimum log level threshold.
*/
public $level_threshold = self::ERROR;

/**
* Whether to retain error messages for import run errors meta.
*
* @since 5.1.0
* @var string[]
*/
private $error_messages_accumulator = array();

/**
* Whether to retain error messages for import run errors meta.
*
* @since 5.1.0
* @var bool Whether to retain error messages.
*/
private $retain_error_messages = false;

/**
* Whether email reports can be sent.
*
* @since 5.1.0
* @var bool Whether email reports can be sent.
*/
public $can_send_email = false;

/**
* The email frequency for sending reports.
*
* @since 5.1.0
* @var string
*/
public $email_frequency = 'weekly';

/**
* The email address to send reports to.
*
* @since 5.1.0
* @var string The email address to send reports to.
*/
public $to_email = '';
Expand Down Expand Up @@ -233,7 +267,7 @@ private function setup_log_directory() {
* @return void
*/
private function init_saved_settings() {
$feedzy_settings = get_option( 'feedzy-settings', array() );
$feedzy_settings = apply_filters( 'feedzy_get_settings', array() );
if ( ! isset( $feedzy_settings['logs'] ) ) {
return;
}
Expand All @@ -242,8 +276,9 @@ private function init_saved_settings() {
$this->level_threshold = self::PRIORITIES_MAPPING[ $feedzy_settings['logs']['level'] ];
}

$this->can_send_email = isset( $feedzy_settings['logs']['send_email_report'] ) && $feedzy_settings['logs']['send_email_report'];
$this->to_email = isset( $feedzy_settings['logs']['email'] ) ? sanitize_email( $feedzy_settings['logs']['email'] ) : '';
$this->can_send_email = isset( $feedzy_settings['logs']['send_email_report'] ) && $feedzy_settings['logs']['send_email_report'];
$this->to_email = isset( $feedzy_settings['logs']['email'] ) ? sanitize_email( $feedzy_settings['logs']['email'] ) : '';
$this->email_frequency = isset( $feedzy_settings['logs']['email_frequency'] ) ? sanitize_text_field( $feedzy_settings['logs']['email_frequency'] ) : 'weekly';
}

/**
Expand Down
79 changes: 64 additions & 15 deletions includes/admin/feedzy-rss-feeds-task-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
* Register and manage scheduled tasks for Feedzy RSS Feeds.
*
* @package Feedzy_Rss_Feeds_Task_Manager
* @version 5.1.0
*/

/**
* Class Feedzy_Rss_Feeds_Task_Manager.
*
* @since 5.1.0
*/
class Feedzy_Rss_Feeds_Task_Manager {

Expand All @@ -22,19 +23,23 @@ public function register_actions() {
'task_feedzy_send_error_report',
array( $this, 'send_error_report' )
);

add_action(
'update_option_feedzy-settings',
array( $this, 'maybe_reschedule_email_report' ),
10,
2
);

add_action(
'task_feedzy_cleanup_logs',
function () {
Feedzy_Rss_Feeds_Log::get_instance()->should_clean_logs();
}
array( $this, 'check_and_clean_logs' )
);

add_action(
'init',
function () {
$this->schedule_weekly_tasks();
$this->schedule_hourly_tasks();
$this->schedule_tasks();
}
);
}
Expand All @@ -45,7 +50,22 @@ function () {
* @since 5.1.0
* @return void
*/
public function schedule_weekly_tasks() {
public function schedule_tasks() {
if (
false === Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'task_feedzy_cleanup_logs' )
) {
Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( time(), 'hourly', 'task_feedzy_cleanup_logs' );
}
$this->schedule_email_report();
}

/**
* Schedule daily tasks.
*
* @since 5.1.0
* @return void
*/
public function schedule_email_report() {
$log_instance = Feedzy_Rss_Feeds_Log::get_instance();

if (
Expand All @@ -54,24 +74,53 @@ public function schedule_weekly_tasks() {
) {
return;
}

Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( time(), 'weekly', 'task_feedzy_send_error_report' );

$frequency = $log_instance->email_frequency;
if ( ! in_array( $frequency, array( 'daily', 'weekly' ), true ) ) {
$frequency = 'weekly';
}

Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( time(), $frequency, 'task_feedzy_send_error_report' );
}

/**
* Schedule daily tasks.
* When feedzy settings are updated, ensure the error report schedule matches the new frequency.
*
* @since 5.1.0
* @param array<string, mixed> $old_value Previous option value.
* @param array<string, mixed> $value New option value.
* @return void
*/
public function schedule_hourly_tasks() {
if (
false !== Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'task_feedzy_cleanup_logs' )
) {
public function maybe_reschedule_email_report( $old_value, $value ) {
$old_freq = isset( $old_value['logs']['email_frequency'] ) ? sanitize_text_field( $old_value['logs']['email_frequency'] ) : '';
$new_freq = isset( $value['logs']['email_frequency'] ) ? sanitize_text_field( $value['logs']['email_frequency'] ) : '';

if ( $old_freq === $new_freq ) {
return;
}

Feedzy_Rss_Feeds_Util_Scheduler::clear_scheduled_hook( 'task_feedzy_send_error_report' );

if ( ! in_array( $new_freq, array( 'daily', 'weekly' ), true ) ) {
$new_freq = 'weekly';
}

$send_reports = ! empty( $value['logs']['send_email_report'] );
$to_email = isset( $value['logs']['email'] ) ? sanitize_email( $value['logs']['email'] ) : '';

if ( $send_reports && ! empty( $to_email ) ) {
Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( time(), $new_freq, 'task_feedzy_send_error_report' );
}
}

Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( time(), 'hourly', 'task_feedzy_cleanup_logs' );
/**
* Check and clean logs if necessary.
*
* @since 5.1.0
* @return void
*/
public function check_and_clean_logs() {
Feedzy_Rss_Feeds_Log::get_instance()->should_clean_logs();
}

/**
Expand Down
Loading
Loading