-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
caaf40a
commit 546339e
Showing
16 changed files
with
628 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace WPTrait\Abstracts; | ||
|
||
abstract class Params | ||
{ | ||
|
||
/** | ||
* Setup Method Params | ||
* | ||
* @var array | ||
*/ | ||
protected array $params; | ||
|
||
/** | ||
* Get List Of Prepare Params For Use in WordPress Method | ||
* | ||
* @return array | ||
*/ | ||
public function toParams(): array | ||
{ | ||
if (empty($this->params)) { | ||
$this->setParams(); | ||
} | ||
|
||
return $this->params; | ||
} | ||
|
||
/** | ||
* Prepare Parameter For User in WordPress Method | ||
*/ | ||
abstract public function setParams(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace WPTrait\Exceptions\Json; | ||
|
||
use WPTrait\Exceptions\TraitException; | ||
|
||
class UnableDecodeJsonException extends TraitException | ||
{ | ||
public function __construct(string $message = "", int $code = 0, \Throwable $previous = null) | ||
{ | ||
parent::__construct("Unable to decode JSON: {$message}", $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace WPTrait\Exceptions\Json; | ||
|
||
use WPTrait\Exceptions\TraitException; | ||
|
||
class UnableEncodeJsonException extends TraitException | ||
{ | ||
public function __construct(string $message = "", int $code = 0, \Throwable $previous = null) | ||
{ | ||
parent::__construct("Unable to encode JSON: {$message}", $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace WPTrait\Exceptions; | ||
|
||
class TraitException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace WPTrait\Http; | ||
|
||
if (!defined('ABSPATH')) { | ||
exit; // Exit if accessed directly. | ||
} | ||
|
||
if (!class_exists('WPTrait\HTTP\Download')) { | ||
|
||
class Download | ||
{ | ||
/** | ||
* File Url | ||
* | ||
* @var string | ||
*/ | ||
public string $url = ''; | ||
|
||
/** | ||
* Request TimeOut | ||
* | ||
* @var float | ||
*/ | ||
public float $timeout = 300; | ||
|
||
/** | ||
* Whether to perform Signature Verification. | ||
* | ||
* @var bool | ||
*/ | ||
protected bool $signature_verification = false; | ||
|
||
/** | ||
* HTTP Response | ||
*/ | ||
public string|\WP_Error $response; | ||
|
||
public function __construct($url = '') | ||
{ | ||
$this->url = $url; | ||
} | ||
|
||
public function timeout($timeout): static | ||
{ | ||
$this->timeout = $timeout; | ||
return $this; | ||
} | ||
|
||
public function verification($bool): static | ||
{ | ||
$this->signature_verification = (bool)$bool; | ||
return $this; | ||
} | ||
|
||
public function send() | ||
{ | ||
// Check Url | ||
if (empty($this->url)) { | ||
return false; | ||
} | ||
|
||
// Check Function Exists | ||
if (!function_exists('download_url')) { | ||
require_once ABSPATH . 'wp-admin/includes/file.php'; | ||
} | ||
|
||
// Send request | ||
$this->response = download_url($this->url, $this->timeout, $this->signature_verification); | ||
|
||
// Return | ||
return $this; | ||
} | ||
|
||
public function hasError(): bool | ||
{ | ||
return is_wp_error($this->response); | ||
} | ||
|
||
public function getErrorMessage(): bool | ||
{ | ||
if ($this->hasError()) { | ||
return $this->response->get_error_message(); | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
public function getFilename(): \WP_Error|string | ||
{ | ||
if (!$this->hasError()) { | ||
return $this->response; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
public function copyTo($path): bool | ||
{ | ||
if ($this->hasError()) { | ||
return false; | ||
} | ||
|
||
$copy = copy($this->response, $path); | ||
@unlink($this->response); | ||
return $copy; | ||
} | ||
} | ||
} |
Oops, something went wrong.