3600, // seconds; 1 hour 'max_posts' => 200, 'delete_on_uninstall' => false, ]; /** * Sync interval choices in seconds: 30 min, 1 h, 3 h, 6 h — the same for * everyone (not a plan limit, so not filterable). */ private const ALLOWED_INTERVALS = [ 1800, 3600, 10800, 21600 ]; /** @var SourceRepository */ private $sourceRepository; /** @var Scheduler */ private $scheduler; public function __construct( SourceRepository $sourceRepository, Scheduler $scheduler ) { $this->sourceRepository = $sourceRepository; $this->scheduler = $scheduler; } public function register(): void { add_action( 'rest_api_init', [ $this, 'registerRoutes' ] ); } public function registerRoutes(): void { register_rest_route( self::NAMESPACE, '/settings', [ [ 'methods' => 'GET', 'callback' => [ $this, 'getSettings' ], 'permission_callback' => [ Permissions::class, 'adminOnly' ], ], [ 'methods' => 'PUT', 'callback' => [ $this, 'updateSettings' ], 'permission_callback' => [ Permissions::class, 'adminOnly' ], 'args' => $this->writeArgs(), ], ] ); } /** * GET /settings * * @param \WP_REST_Request $request Incoming REST request. * @return \WP_REST_Response */ public function getSettings( \WP_REST_Request $request ): \WP_REST_Response { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found return new \WP_REST_Response( $this->publicSettings( $this->readSettings() ), 200 ); } /** * Only keys known to the current defaults are returned. Unknown stored keys * (e.g. written by a deactivated add-on) stay in the option untouched. * * @param array $settings * @return array */ private function publicSettings( array $settings ): array { return array_intersect_key( $settings, self::defaults() ); } /** * PUT /settings * * @param \WP_REST_Request $request Incoming REST request. * @return \WP_REST_Response */ public function updateSettings( \WP_REST_Request $request ): \WP_REST_Response { $current = $this->readSettings(); $oldInterval = (int) $current['fetch_interval']; if ( null !== $request->get_param( 'fetch_interval' ) ) { $interval = (int) $request->get_param( 'fetch_interval' ); if ( in_array( $interval, self::allowedIntervals(), true ) ) { $current['fetch_interval'] = $interval; } } if ( null !== $request->get_param( 'max_posts' ) ) { $max = (int) $request->get_param( 'max_posts' ); if ( $max >= 50 && $max <= 500 ) { $current['max_posts'] = $max; } } if ( null !== $request->get_param( 'delete_on_uninstall' ) ) { $current['delete_on_uninstall'] = (bool) $request->get_param( 'delete_on_uninstall' ); } /** * Lets add-ons apply their own settings keys (declared via * feednetic/settings/write_args) from the request. * * @param array $current Settings about to be stored. * @param \WP_REST_Request $request The PUT /settings request. */ // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores $current = apply_filters( 'feednetic/settings/update', $current, $request ); update_option( self::OPTION_KEY, (string) wp_json_encode( $current ), false ); // Reschedule recurring fetch jobs for all active sources when the interval changes. $newInterval = (int) $current['fetch_interval']; if ( $newInterval !== $oldInterval ) { foreach ( $this->sourceRepository->findAll() as $source ) { if ( 'active' === (string) $source['status'] ) { $this->scheduler->rescheduleFetchSource( (int) $source['id'], $newInterval ); } } // Also re-aligns schedules of non-active sources and records the // applied interval so the init-time check stays a no-op. $this->scheduler->reconcileFetchSchedules(); } return new \WP_REST_Response( $this->publicSettings( $current ), 200 ); } /** * @return array */ private function readSettings(): array { $stored = get_option( self::OPTION_KEY, '' ); $decoded = ''; if ( is_string( $stored ) && '' !== $stored ) { $decoded = json_decode( $stored, true ); } $saved = is_array( $decoded ) ? $decoded : []; $settings = array_merge( self::defaults(), $saved ); // Never below the technical floor, whatever was stored. $settings['fetch_interval'] = max( \Feednetic\Limits::minFetchInterval(), (int) $settings['fetch_interval'] ); return $settings; } /** * Default values for every settings key, extensible via feednetic/settings/defaults. * * @return array */ public static function defaults(): array { // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores $defaults = apply_filters( 'feednetic/settings/defaults', self::DEFAULTS ); return is_array( $defaults ) ? array_merge( self::DEFAULTS, $defaults ) : self::DEFAULTS; } /** * Sync intervals (seconds) the admin may choose — the same for everyone. * * @return int[] */ public static function allowedIntervals(): array { return self::ALLOWED_INTERVALS; } /** * @return array> */ private function writeArgs(): array { $args = [ 'fetch_interval' => [ 'type' => 'integer', 'enum' => self::allowedIntervals(), ], 'max_posts' => [ 'type' => 'integer', 'minimum' => 50, 'maximum' => 500, ], 'delete_on_uninstall' => [ 'type' => 'boolean', ], ]; // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores $filtered = apply_filters( 'feednetic/settings/write_args', $args ); return is_array( $filtered ) ? $filtered : $args; } }