Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.66 KB | None | 0 0
  1. <?php
  2. declare(strict_types=1);
  3. namespace inpsyde;
  4.  
  5. final class WPNonces
  6. {
  7.     protected $nonceAction;
  8.     protected $nonceIdentifier;
  9.  
  10.     private const NONCESACTION = -1;
  11.     private const NONCESIDENTIFIER = "_inpsyde_nonce";
  12.  
  13.     private static $nonceWarning = "Bist du dir sicher, dass du diese Aktion durchführen möchtest?";
  14.     private static $nonceLifetime = 86400;
  15.  
  16.     public function __construct(
  17.         string $nonceAction = NONCESACTION,
  18.         string $nonceIdentifier = NONCESIDENTIFIER
  19.     ) {
  20.  
  21.         $this->nonceAction = $nonceAction;
  22.         $this->nonceIdentifier = $nonceIdentifier;
  23.     }
  24.  
  25.     public function __destruct()
  26.     {
  27.         unset($this->nonceAction);
  28.         unset($this->nonceIdentifier);
  29.     }
  30.  
  31.     public function action(string $nonceAction = null):string
  32.     {
  33.         if ($nonceAction !== null) {
  34.             $this->nonceAction = $nonceAction;
  35.         }
  36.  
  37.         return $this->nonceAction;
  38.     }
  39.  
  40.     public function identifier(string $nonceIdentifier = null):string
  41.     {
  42.         if ($nonceIdentifier !== null) {
  43.             $this->nonceIdentifier = $nonceIdentifier;
  44.         }
  45.  
  46.         return $this->nonceIdentifier;
  47.     }
  48.  
  49.     public static function lifetime(int $nonceLifetime = null):bool
  50.     {
  51.         if ($nonceIdentifier !== null) {
  52.             if ($nonceLifetime < 0) {
  53.                 return false;
  54.             }
  55.  
  56.             self::$nonceLifetime = $nonceLifetime;
  57.             add_filter("nonce_life", function () {
  58.                 return $nonceLifetime;
  59.             });
  60.             return true;
  61.         }
  62.  
  63.         return self::$nonceLifetime;
  64.     }
  65.  
  66.     public static function warning(string $nonceWarning = null):string
  67.     {
  68.         if ($nonceWarning !== null) {
  69.             self::$nonceWarning = $nonceWarning;
  70.  
  71.             $filterFunction = ["WPNonces", "nonceMessage"];
  72.  
  73.             add_filter(
  74.                 "gettext",
  75.                 $filterFunction
  76.             );
  77.         }
  78.  
  79.         return self::$nonceWarning;
  80.     }
  81.  
  82.     public static function nonceMessage(string $messageTranslation):string
  83.     {
  84.         if ($nonceWarning === "Bist du dir sicher, dass du diese Aktion durchführen möchtest?") {
  85.             return self::$nonceWarning;
  86.         }
  87.  
  88.         return $messageTranslation;
  89.     }
  90.  
  91.     public static function adminFunction(string $functionName)
  92.     {
  93.         if (function_exists($functionName)) {
  94.             add_action("check_admin_referer", $functionName, 10, 2);
  95.         }
  96.     }
  97.  
  98.     public static function ajaxFunction(string $functionName)
  99.     {
  100.         if (function_exists($functionName)) {
  101.             add_action("check_ajax_referer", $functionName, 10, 2);
  102.         }
  103.     }
  104.  
  105.     public function loadNonce(): string
  106.     {
  107.         return wp_create_nonce($this->nonceAction);
  108.     }
  109.  
  110.     public function loadNonceURL(string $requestUrl): string
  111.     {
  112.         return wp_nonce_url($requestUrl, $this->nonceAction, $this->nonceIdentifier);
  113.     }
  114.  
  115.     public function loadNonceInput(bool $fieldReferer = true, bool $fieldEcho = true):string
  116.     {
  117.         $nonceObject = wp_nonce_field(
  118.             $this->nonceAction,
  119.             $this->nonceIdentifier,
  120.             $fieldReferer,
  121.             $fieldEcho
  122.         );
  123.  
  124.         if (!$fieldEcho) {
  125.             return $nonceObject;
  126.         }
  127.     }
  128.  
  129.     public function loadNonceWarning()
  130.     {
  131.         wp_nonce_ays($this->nonceAction);
  132.     }
  133.  
  134.     public static function checkNonce(
  135.         string $nonceAction = NONCESACTION,
  136.         string $nonceIdentifier = NONCESIDENTIFIER
  137.     ):string {
  138.  
  139.         return wp_verify_nonce($nonceIdentifier, $nonceAction);
  140.     }
  141.  
  142.     public static function checkNonceURL(
  143.         string $nonceUrl,
  144.         string $nonceIdentifier = NONCESACTION,
  145.         string $nonceAction = NONCESIDENTIFIER
  146.     ):bool {
  147.  
  148.         $nonceUrl = trim(parse_url($nonceUrl));
  149.  
  150.         if ($nonceUrl !== "") {
  151.             parse_str($nonceUrl, $nonceParam);
  152.         }
  153.  
  154.         if (sizeof($nonceParam) > 0 && $nonceParam[$nonceIdentifier] !== "") {
  155.             return wp_verify_nonce($nonceParam[$nonceIdentifier], $nonceAction);
  156.         }
  157.  
  158.         return false;
  159.     }
  160.  
  161.     public static function checkNonceAdmin(
  162.         string $nonceAction = NONCESACTION,
  163.         string $nonceIdentifier = NONCESIDENTIFIER
  164.     ):string {
  165.  
  166.         return check_admin_referer($nonceAction, $nonceIdentifier);
  167.     }
  168.  
  169.     public static function checkNonceAjax(
  170.         string $nonceAction = NONCESACTION,
  171.         string $nonceIdentifier = NONCESIDENTIFIER,
  172.         bool $nonceDie = true
  173.     ):string {
  174.  
  175.         return check_ajax_referer($nonceAction, $nonceIdentifier, $nonceDie);
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement