Skip to content

Commit

Permalink
Setup HTTP and Download Class
Browse files Browse the repository at this point in the history
  • Loading branch information
mehrshaddarzi committed Apr 10, 2023
1 parent caaf40a commit 546339e
Show file tree
Hide file tree
Showing 16 changed files with 628 additions and 80 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ $this->url->parse('https://site.com?query=value');
$this->url->sanitize('https://site.com?query=value');

// Validate Url
$this->url->isValidate('https://site.com?query=value');
$this->url->isValidate('https://site.com<script>alert("xss")<script>?query=value');
// Escape Url
$this->url->esc('https://site.com?query=value');
Expand Down Expand Up @@ -1196,7 +1196,9 @@ add_action('init', 'register_session');
public function register_session()
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
session_start([
'read_and_close' => true
]);
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions example/plugin-slug/src/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class Admin extends Model
{
use Notice, RowActions, Ajax;

public $actions = [
public array $actions = [
'save_post' => ['save_author', 11, 3]
];

public $filters = [
public array $filters = [
'the_content' => 'add_prefix_content',
'show_admin_bar' => '__return_false'
];
Expand Down
33 changes: 33 additions & 0 deletions src/Abstracts/Params.php
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();
}
2 changes: 1 addition & 1 deletion src/Admin/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class Page extends Model
*
* @var Information
*/
public $plugin;
public Information $plugin;

public function __construct(Information $plugin)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PostType extends Page
*
* @var Information
*/
public $plugin;
public Information $plugin;

public function __construct(Information $plugin, $slug, $name, $args = [])
{
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Taxonomy extends Page
*
* @var Information
*/
public $plugin;
public Information $plugin;

public $slug, $name;
public $post_types, $args = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Users extends Page
*
* @var Information
*/
public $plugin;
public Information $plugin;

public function __construct(Information $plugin)
{
Expand Down
13 changes: 13 additions & 0 deletions src/Exceptions/Json/UnableDecodeJsonException.php
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);
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/Json/UnableEncodeJsonException.php
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);
}
}
8 changes: 8 additions & 0 deletions src/Exceptions/TraitException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace WPTrait\Exceptions;

class TraitException extends \Exception
{

}
109 changes: 109 additions & 0 deletions src/Http/Download.php
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;
}
}
}
Loading

0 comments on commit 546339e

Please sign in to comment.