isValid() ) { return new WP_Error( 'invalid_locale', sprintf( /* translators: %s: the locale tag supplied. */ __( '"%s" is not a locale Loco can parse. Use a WordPress locale such as de_DE, fr_FR or pt_BR; call translations/list-locales to see what this site already has.', 'acrossai-abilities-manager' ), $tag ) ); } return $locale; } /** * @since 0.0.34 * @param array $input Ability input. * @param string $message What the caller is confirming. * @return true|WP_Error */ public static function assert_confirmed( array $input, string $message = '' ) { if ( empty( $input['confirm'] ) ) { return new WP_Error( 'confirmation_required', '' !== $message ? $message : __( 'Pass confirm: true to proceed.', 'acrossai-abilities-manager' ) ); } return true; } /** * Permission callback factory. * * A single `current_user_can( $floor )`, and the filter is consulted only after it passes — so a * filter can tighten access and never widen it (PATTERN-FILTERABLE-CAPABILITY-RAISE-ONLY). * * Loco's own capability is `loco_admin`, and it creates a `translator` role. Neither is used as * the floor here: a translator editing strings through wp-admin is a different risk from an AI * client writing files that execute nowhere but render everywhere. * * @since 0.0.34 * @param string $floor WordPress capability floor. * @return callable */ public static function can( string $floor = 'manage_options' ): callable { return static function () use ( $floor ): bool { if ( ! current_user_can( $floor ) ) { return false; } /** * Filters whether the current user may use a Loco Translate ability. * * @since 0.0.34 * @param bool $allowed Whether access is granted. Always true at this point. * @param string $floor WordPress capability floor. */ return (bool) apply_filters( self::PERMISSION_FILTER, true, $floor ); }; } /** * Success envelope. * * `success` and `message` are set last so a payload key cannot spoof them. * * @since 0.0.34 * @param array $payload Ability-specific keys. * @param string $message Human-readable summary. * @return array */ public static function ok( array $payload, string $message ): array { unset( $payload['success'], $payload['error_code'], $payload['message'] ); return array_merge( array( 'success' => true ), $payload, array( 'message' => $message ) ); } /** * @since 0.0.34 * @param WP_Error $error Error. * @return array */ public static function fail( WP_Error $error ): array { return self::error( (string) $error->get_error_code(), (string) $error->get_error_message() ); } /** * @since 0.0.34 * @param string $code Machine-readable code. * @param string $message Human-readable message. * @return array */ public static function error( string $code, string $message ): array { return array( 'success' => false, 'error_code' => $code, 'message' => $message, ); } }