registry = $registry; $this->settings = $settings; } /** * Returns every valid policy value. * * @return string[] Policy values. */ public static function values(): array { return array( self::ALLOW, self::SIGNAL_ONLY, self::BLOCK ); } /** * Whether a string is a valid policy value. * * @param string $policy Candidate policy. * @return bool True when valid. */ public static function is_valid( string $policy ): bool { return in_array( $policy, self::values(), true ); } /** * Resolves the effective policy for an agent. * * @param array $agent Agent array from the registry. * @return string One of the policy constants. */ public function for_agent( array $agent ): string { $policies = $this->settings->get( 'policies', array() ); $by_agent = isset( $policies['agents'] ) && is_array( $policies['agents'] ) ? $policies['agents'] : array(); $by_group = isset( $policies['groups'] ) && is_array( $policies['groups'] ) ? $policies['groups'] : array(); foreach ( $by_agent as $ua => $policy ) { if ( 0 === strcasecmp( (string) $ua, $agent['ua'] ) && self::is_valid( (string) $policy ) ) { return (string) $policy; } } $group = isset( $agent['group'] ) ? (string) $agent['group'] : ''; if ( '' !== $group && isset( $by_group[ $group ] ) && self::is_valid( (string) $by_group[ $group ] ) ) { return (string) $by_group[ $group ]; } $defaults = $this->settings->get( 'default_by_purpose', array() ); $purpose = isset( $agent['purpose'] ) ? (string) $agent['purpose'] : ''; if ( isset( $defaults[ $purpose ] ) && self::is_valid( (string) $defaults[ $purpose ] ) ) { return (string) $defaults[ $purpose ]; } return self::ALLOW; } /** * Returns every agent whose effective policy is one of the given values. * * @param string[] $policies Policy values to include. * @return array List of agent arrays. */ public function agents_with( array $policies ): array { $matched = array(); foreach ( $this->registry->get_agents() as $agent ) { if ( in_array( $this->for_agent( $agent ), $policies, true ) ) { $matched[] = $agent; } } return $matched; } /** * Returns every agent that should be declared in robots.txt. * * @return array List of agent arrays. */ public function agents_to_declare(): array { return $this->agents_with( array( self::SIGNAL_ONLY, self::BLOCK ) ); } /** * Returns every agent that should be enforced at the HTTP level. * * @return array List of agent arrays. */ public function agents_to_enforce(): array { $enforceable = array(); foreach ( $this->agents_with( array( self::BLOCK ) ) as $agent ) { if ( Registry::MATCH_UA === $agent['match'] ) { $enforceable[] = $agent; } } return $enforceable; } /** * Sets the policy for a whole group. * * @param string $group_id Group identifier. * @param string $policy Policy value. * @return true|\WP_Error True on success, WP_Error describing the refusal. */ public function set_group_policy( string $group_id, string $policy ) { if ( ! self::is_valid( $policy ) ) { return new \WP_Error( 'aiblackout_invalid_policy', __( 'That is not a valid crawler policy.', 'ai-blackout' ) ); } $group = $this->registry->get_group( $group_id ); if ( null === $group ) { return new \WP_Error( 'aiblackout_unknown_group', __( 'That crawler group is not in the bundled list.', 'ai-blackout' ) ); } if ( self::BLOCK === $policy ) { foreach ( $this->registry->get_agents() as $agent ) { if ( $agent['group'] !== $group_id ) { continue; } $guard = $this->guard_search_impact( $agent ); if ( is_wp_error( $guard ) ) { return $guard; } } } $policies = $this->settings->get( 'policies', array() ); if ( ! isset( $policies['groups'] ) || ! is_array( $policies['groups'] ) ) { $policies['groups'] = array(); } $policies['groups'][ $group_id ] = $policy; $this->settings->set( 'policies', $policies ); return true; } /** * Sets the default policy for every crawler with a given purpose. * * @param string $purpose Purpose identifier, e.g. training. * @param string $policy Policy value. * @return true|\WP_Error True on success, WP_Error describing the refusal. */ public function set_purpose_default( string $purpose, string $policy ) { if ( ! self::is_valid( $policy ) ) { return new \WP_Error( 'aiblackout_invalid_policy', __( 'That is not a valid crawler policy.', 'ai-blackout' ) ); } $defaults = $this->settings->get( 'default_by_purpose', array() ); if ( ! is_array( $defaults ) || ! array_key_exists( $purpose, $defaults ) ) { return new \WP_Error( 'aiblackout_unknown_purpose', __( 'That is not a crawler purpose this plugin recognises.', 'ai-blackout' ) ); } if ( self::BLOCK === $policy ) { foreach ( $this->registry->get_agents() as $agent ) { if ( ! isset( $agent['purpose'] ) || $agent['purpose'] !== $purpose ) { continue; } $guard = $this->guard_search_impact( $agent ); if ( is_wp_error( $guard ) ) { return $guard; } } } $defaults[ $purpose ] = $policy; $this->settings->set( 'default_by_purpose', $defaults ); return true; } /** * Sets the policy for a single agent. * * @param string $ua User-agent token. * @param string $policy Policy value. * @return true|\WP_Error True on success, WP_Error describing the refusal. */ public function set_agent_policy( string $ua, string $policy ) { if ( ! self::is_valid( $policy ) ) { return new \WP_Error( 'aiblackout_invalid_policy', __( 'That is not a valid crawler policy.', 'ai-blackout' ) ); } $agent = $this->registry->get_agent( $ua ); if ( null === $agent ) { return new \WP_Error( 'aiblackout_unknown_agent', __( 'That crawler is not in the bundled list.', 'ai-blackout' ) ); } if ( self::BLOCK === $policy ) { $guard = $this->guard_search_impact( $agent ); if ( is_wp_error( $guard ) ) { return $guard; } } $policies = $this->settings->get( 'policies', array() ); if ( ! isset( $policies['agents'] ) || ! is_array( $policies['agents'] ) ) { $policies['agents'] = array(); } $policies['agents'][ $agent['ua'] ] = $policy; $this->settings->set( 'policies', $policies ); return true; } /** * Refuses to block an agent with search impact until the user has * confirmed it. * * @param array $agent Agent array. * @return true|\WP_Error True when blocking is permitted. */ private function guard_search_impact( array $agent ) { $impact = isset( $agent['search_impact'] ) ? (string) $agent['search_impact'] : 'none'; if ( 'none' === $impact ) { return true; } if ( $this->settings->has_search_impact_ack( $agent['ua'] ) ) { return true; } return new \WP_Error( 'aiblackout_search_impact_unconfirmed', sprintf( /* translators: %s: Crawler user-agent token, e.g. OAI-SearchBot. */ __( 'Blocking %s will reduce where your site appears in search. Confirm you want this before it can be enabled.', 'ai-blackout' ), $agent['ua'] ), array( 'agent' => $agent['ua'], 'search_impact' => $impact, 'description' => $agent['description'], ) ); } }