'block_known_ai', 'log_enabled' => true, 'log_limit' => 500, 'history_enabled' => true, 'auto_update_rules' => false, 'core_rules_enabled' => true, 'custom_rules_enabled'=> true, 'custom_rules' => [], ]; } } if ( ! function_exists( 'aicrg_get_settings' ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Function is prefixed with 'aicrg_' function aicrg_get_settings(): array { $defaults = aicrg_get_default_settings(); $stored = get_option( 'aicrg_settings', [] ); if ( ! is_array( $stored ) ) { $stored = []; } return wp_parse_args( $stored, $defaults ); } } if ( ! function_exists( 'aicrg_update_settings' ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Function is prefixed with 'aicrg_' function aicrg_update_settings( array $settings ): bool { $defaults = aicrg_get_default_settings(); $sanitized = wp_parse_args( $settings, $defaults ); $sanitized['block_mode'] = in_array( $sanitized['block_mode'], [ 'block_known_ai', 'allow_all' ], true ) ? $sanitized['block_mode'] : 'block_known_ai'; $sanitized['log_enabled'] = aicrg_bool( $sanitized['log_enabled'] ?? true ); $sanitized['history_enabled'] = aicrg_bool( $sanitized['history_enabled'] ?? true ); $sanitized['auto_update_rules'] = aicrg_bool( $sanitized['auto_update_rules'] ?? false ); $sanitized['core_rules_enabled'] = aicrg_bool( $sanitized['core_rules_enabled'] ?? true ); $sanitized['custom_rules_enabled']= aicrg_bool( $sanitized['custom_rules_enabled'] ?? true ); $sanitized['log_limit'] = max( 10, absint( $sanitized['log_limit'] ?? 500 ) ); $sanitized['custom_rules'] = aicrg_sanitize_custom_rules( (array) ( $sanitized['custom_rules'] ?? [] ) ); return update_option( 'aicrg_settings', $sanitized ); } } if ( ! function_exists( 'aicrg_bool' ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Function is prefixed with 'aicrg_' function aicrg_bool( $value ): bool { return filter_var( $value, FILTER_VALIDATE_BOOLEAN ); } } if ( ! function_exists( 'aicrg_truncate' ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Function is prefixed with 'aicrg_' function aicrg_truncate( string $value, int $max ): string { if ( $max <= 0 ) { return ''; } $ellipsis = '...'; $max_with_ellipsis = $max - strlen( $ellipsis ); if ( function_exists( 'mb_strlen' ) && function_exists( 'mb_substr' ) ) { if ( mb_strlen( $value ) > $max ) { return mb_substr( $value, 0, $max_with_ellipsis ) . $ellipsis; } return $value; } if ( strlen( $value ) > $max ) { return substr( $value, 0, $max_with_ellipsis ) . $ellipsis; } return $value; } } if ( ! function_exists( 'aicrg_sanitize_custom_rules' ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Function is prefixed with 'aicrg_' function aicrg_sanitize_custom_rules( array $rules ): array { $sanitized = []; $allowed_categories = [ 'ai', 'search', 'social', 'utility', 'custom' ]; foreach ( $rules as $rule ) { $pattern = sanitize_text_field( $rule['pattern'] ?? '' ); if ( '' === $pattern ) { continue; } $name = sanitize_text_field( $rule['name'] ?? '' ); $category = sanitize_key( $rule['category'] ?? 'custom' ); if ( ! in_array( $category, $allowed_categories, true ) ) { $category = 'custom'; } $slug = sanitize_title( $rule['slug'] ?? $name ?? $pattern ); if ( '' === $slug ) { $slug = sanitize_title( $pattern ); } $sanitized[] = [ 'slug' => $slug, 'name' => $name ?: $slug, 'pattern' => $pattern, 'category' => $category, 'source' => 'custom', ]; } return $sanitized; } }