<?php
/**
 * CronSchedules
 *
 * Registers weekly and monthly cron intervals used by email reports and data pruning.
 *
 * @package Relatedly\Core
 * @since   2.0.0
 */

namespace Relatedly\Core;

defined( 'ABSPATH' ) || exit;

/**
 * Adds custom cron schedules used by Relatedly.
 *
 * @since 2.0.0
 */
class CronSchedules {

	/**
	 * Register WordPress cron schedule filters.
	 *
	 * @since 2.0.0
	 */
	public function init_hooks(): void {
		add_filter( 'cron_schedules', [ $this, 'register' ] );
	}

	/**
	 * Add weekly and monthly intervals when WordPress has not registered them.
	 *
	 * @since 2.0.0
	 * @param array<string, array<string, int|string>> $schedules Existing schedules.
	 * @return array<string, array<string, int|string>>
	 */
	public function register( array $schedules ): array {
		if ( ! isset( $schedules['weekly'] ) ) {
			$schedules['weekly'] = [
				'interval' => WEEK_IN_SECONDS,
				'display'  => __( 'Once Weekly', 'relatedly' ),
			];
		}

		if ( ! isset( $schedules['monthly'] ) ) {
			$schedules['monthly'] = [
				'interval' => MONTH_IN_SECONDS,
				'display'  => __( 'Once Monthly', 'relatedly' ),
			];
		}

		return $schedules;
	}
}
