'array', 'sanitize_callback' => array( $this, 'sanitize_settings' ), 'default' => ocap_get_default_settings(), ) ); } /** * Sanitize and validate all settings inputs. * * @param array $input Raw form input. * @return array Sanitized settings. */ public function sanitize_settings( $input ) { if ( ! is_array( $input ) ) { return ocap_get_default_settings(); } $current = ocap_get_settings(); $output = $current; // 1. Post types tab. if ( isset( $input['post_types'] ) && is_array( $input['post_types'] ) ) { $registered_types = get_post_types( array( 'public' => true ), 'names' ); unset( $registered_types['attachment'] ); $sanitized_types = array(); foreach ( $registered_types as $pt ) { $enabled = ! empty( $input['post_types'][ $pt ]['enabled'] ) ? 1 : 0; $days = isset( $input['post_types'][ $pt ]['days'] ) ? absint( $input['post_types'][ $pt ]['days'] ) : 90; if ( $days < 1 ) { $days = 90; } elseif ( $days > 3650 ) { $days = 3650; } $sanitized_types[ $pt ] = array( 'enabled' => $enabled, 'days' => $days, ); } $output['post_types'] = $sanitized_types; } // Date basis. if ( isset( $input['date_basis'] ) ) { $output['date_basis'] = ( 'published' === $input['date_basis'] ) ? 'published' : 'modified'; } // Show list column. if ( isset( $input['_has_post_types_tab'] ) ) { $output['show_list_column'] = ! empty( $input['show_list_column'] ) ? 1 : 0; } // 2. Exclusions tab. if ( isset( $input['_has_exclusions_tab'] ) ) { if ( ! empty( $input['excluded_terms'] ) && is_array( $input['excluded_terms'] ) ) { $output['excluded_terms'] = array_filter( array_map( 'absint', $input['excluded_terms'] ) ); } else { $output['excluded_terms'] = array(); } // Delete on uninstall checkbox. $output['delete_on_uninstall'] = ! empty( $input['delete_on_uninstall'] ) ? 1 : 0; } // 3. Email digest tab. if ( isset( $input['_has_email_tab'] ) ) { $email_input = isset( $input['email'] ) && is_array( $input['email'] ) ? $input['email'] : array(); $email_cfg = isset( $current['email'] ) ? $current['email'] : array(); // Enabled. $email_cfg['enabled'] = ! empty( $email_input['enabled'] ) ? 1 : 0; // Recipients. if ( isset( $email_input['recipients'] ) ) { if ( is_string( $email_input['recipients'] ) ) { $raw_list = explode( ',', $email_input['recipients'] ); } elseif ( is_array( $email_input['recipients'] ) ) { $raw_list = $email_input['recipients']; } else { $raw_list = array(); } $valid_emails = array(); foreach ( $raw_list as $email ) { $clean = sanitize_email( trim( $email ) ); if ( is_email( $clean ) ) { $valid_emails[] = $clean; } } if ( empty( $valid_emails ) ) { $valid_emails = array( get_option( 'admin_email' ) ); } $email_cfg['recipients'] = array_values( array_unique( $valid_emails ) ); } // Frequency. if ( isset( $email_input['frequency'] ) && in_array( $email_input['frequency'], array( 'weekly', 'monthly', 'quarterly' ), true ) ) { $email_cfg['frequency'] = $email_input['frequency']; } // Send day. if ( isset( $email_input['send_day'] ) ) { $day = absint( $email_input['send_day'] ); if ( 'weekly' === $email_cfg['frequency'] ) { $email_cfg['send_day'] = max( 1, min( 7, $day ) ); } else { $email_cfg['send_day'] = max( 1, min( 28, $day ) ); } } // Send time (HH:MM). if ( ! empty( $email_input['send_time'] ) && preg_match( '/^([01][0-9]|2[0-3]):[0-5][0-9]$/', trim( $email_input['send_time'] ) ) ) { $email_cfg['send_time'] = trim( $email_input['send_time'] ); } // Max items. if ( isset( $email_input['max_items'] ) ) { $max_items = absint( $email_input['max_items'] ); $email_cfg['max_items'] = max( 1, min( 500, $max_items ) ); } // Subject. if ( isset( $email_input['subject'] ) ) { $email_cfg['subject'] = sanitize_text_field( $email_input['subject'] ); } // Heading. if ( isset( $email_input['heading'] ) ) { $email_cfg['heading'] = sanitize_text_field( $email_input['heading'] ); } // Intro. if ( isset( $email_input['intro'] ) ) { $email_cfg['intro'] = wp_kses_post( $email_input['intro'] ); } // Footer. if ( isset( $email_input['footer'] ) ) { $email_cfg['footer'] = wp_kses_post( $email_input['footer'] ); } // Logo ID. if ( isset( $email_input['logo_id'] ) ) { $email_cfg['logo_id'] = absint( $email_input['logo_id'] ); } // Brand color. if ( ! empty( $email_input['brand_color'] ) ) { $clean_hex = sanitize_hex_color( $email_input['brand_color'] ); if ( $clean_hex ) { $email_cfg['brand_color'] = $clean_hex; } } // Button text. if ( isset( $email_input['button_text'] ) ) { $email_cfg['button_text'] = sanitize_text_field( $email_input['button_text'] ); } // Show columns. $allowed_cols = array( 'type', 'days', 'author' ); if ( isset( $email_input['show_columns'] ) && is_array( $email_input['show_columns'] ) ) { $email_cfg['show_columns'] = array_values( array_intersect( $email_input['show_columns'], $allowed_cols ) ); } else { $email_cfg['show_columns'] = array(); } // Only if stale. $email_cfg['only_if_stale'] = ! empty( $email_input['only_if_stale'] ) ? 1 : 0; $output['email'] = $email_cfg; // Reschedule cron event. if ( class_exists( 'OCAP_Email' ) ) { $email_service = new OCAP_Email(); if ( ! empty( $email_cfg['enabled'] ) ) { $email_service->schedule_next_event(); } else { $email_service->clear_scheduled_event(); } } } // Clear transient stale cache. ocap_clear_stale_cache(); return $output; } }