Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- namespace inpsyde;
- final class WPNonces
- {
- protected $nonceAction;
- protected $nonceIdentifier;
- private const NONCESACTION = -1;
- private const NONCESIDENTIFIER = "_inpsyde_nonce";
- private static $nonceWarning = "Bist du dir sicher, dass du diese Aktion durchführen möchtest?";
- private static $nonceLifetime = 86400;
- public function __construct(
- string $nonceAction = NONCESACTION,
- string $nonceIdentifier = NONCESIDENTIFIER
- ) {
- $this->nonceAction = $nonceAction;
- $this->nonceIdentifier = $nonceIdentifier;
- }
- public function __destruct()
- {
- unset($this->nonceAction);
- unset($this->nonceIdentifier);
- }
- public function action(string $nonceAction = null):string
- {
- if ($nonceAction !== null) {
- $this->nonceAction = $nonceAction;
- }
- return $this->nonceAction;
- }
- public function identifier(string $nonceIdentifier = null):string
- {
- if ($nonceIdentifier !== null) {
- $this->nonceIdentifier = $nonceIdentifier;
- }
- return $this->nonceIdentifier;
- }
- public static function lifetime(int $nonceLifetime = null):bool
- {
- if ($nonceIdentifier !== null) {
- if ($nonceLifetime < 0) {
- return false;
- }
- self::$nonceLifetime = $nonceLifetime;
- add_filter("nonce_life", function () {
- return $nonceLifetime;
- });
- return true;
- }
- return self::$nonceLifetime;
- }
- public static function warning(string $nonceWarning = null):string
- {
- if ($nonceWarning !== null) {
- self::$nonceWarning = $nonceWarning;
- $filterFunction = ["WPNonces", "nonceMessage"];
- add_filter(
- "gettext",
- $filterFunction
- );
- }
- return self::$nonceWarning;
- }
- public static function nonceMessage(string $messageTranslation):string
- {
- if ($nonceWarning === "Bist du dir sicher, dass du diese Aktion durchführen möchtest?") {
- return self::$nonceWarning;
- }
- return $messageTranslation;
- }
- public static function adminFunction(string $functionName)
- {
- if (function_exists($functionName)) {
- add_action("check_admin_referer", $functionName, 10, 2);
- }
- }
- public static function ajaxFunction(string $functionName)
- {
- if (function_exists($functionName)) {
- add_action("check_ajax_referer", $functionName, 10, 2);
- }
- }
- public function loadNonce(): string
- {
- return wp_create_nonce($this->nonceAction);
- }
- public function loadNonceURL(string $requestUrl): string
- {
- return wp_nonce_url($requestUrl, $this->nonceAction, $this->nonceIdentifier);
- }
- public function loadNonceInput(bool $fieldReferer = true, bool $fieldEcho = true):string
- {
- $nonceObject = wp_nonce_field(
- $this->nonceAction,
- $this->nonceIdentifier,
- $fieldReferer,
- $fieldEcho
- );
- if (!$fieldEcho) {
- return $nonceObject;
- }
- }
- public function loadNonceWarning()
- {
- wp_nonce_ays($this->nonceAction);
- }
- public static function checkNonce(
- string $nonceAction = NONCESACTION,
- string $nonceIdentifier = NONCESIDENTIFIER
- ):string {
- return wp_verify_nonce($nonceIdentifier, $nonceAction);
- }
- public static function checkNonceURL(
- string $nonceUrl,
- string $nonceIdentifier = NONCESACTION,
- string $nonceAction = NONCESIDENTIFIER
- ):bool {
- $nonceUrl = trim(parse_url($nonceUrl));
- if ($nonceUrl !== "") {
- parse_str($nonceUrl, $nonceParam);
- }
- if (sizeof($nonceParam) > 0 && $nonceParam[$nonceIdentifier] !== "") {
- return wp_verify_nonce($nonceParam[$nonceIdentifier], $nonceAction);
- }
- return false;
- }
- public static function checkNonceAdmin(
- string $nonceAction = NONCESACTION,
- string $nonceIdentifier = NONCESIDENTIFIER
- ):string {
- return check_admin_referer($nonceAction, $nonceIdentifier);
- }
- public static function checkNonceAjax(
- string $nonceAction = NONCESACTION,
- string $nonceIdentifier = NONCESIDENTIFIER,
- bool $nonceDie = true
- ):string {
- return check_ajax_referer($nonceAction, $nonceIdentifier, $nonceDie);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement