| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\FormBuilder; |
| 4 |
|
| 5 |
use Give\Campaigns\ValueObjects\CampaignType; |
| 6 |
use Give\Framework\Database\DB; |
| 7 |
use Give\Helpers\Language; |
| 8 |
|
| 9 |
class FormBuilderRouteBuilder |
| 10 |
{ |
| 11 |
const SLUG = 'givewp-form-builder'; |
| 12 |
|
| 13 |
/** |
| 14 |
* @var int|string |
| 15 |
*/ |
| 16 |
protected $donationFormID; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $locale; |
| 22 |
|
| 23 |
/** |
| 24 |
* @since 3.22.0 Add locale support |
| 25 |
* @since 3.0.0 |
| 26 |
* |
| 27 |
* @param int|string $donationFormID |
| 28 |
*/ |
| 29 |
protected function __construct($donationFormID, string $locale = '') |
| 30 |
{ |
| 31 |
$this->donationFormID = $donationFormID; |
| 32 |
$this->locale = ! empty($locale) ? $locale : Language::getLocale(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @since 3.22.0 Add locale support |
| 37 |
* @since 3.0.0 |
| 38 |
*/ |
| 39 |
public static function makeCreateFormRoute(string $locale = ''): self |
| 40 |
{ |
| 41 |
// @todo Refactor create route so as not to mix types for $donationFormID. |
| 42 |
return new self('new', $locale); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @since 3.22.0 Add locale support |
| 47 |
* @since 3.0.0 |
| 48 |
*/ |
| 49 |
public static function makeEditFormRoute(int $donationFormID, string $locale = ''): self |
| 50 |
{ |
| 51 |
return new self($donationFormID, $locale); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @since 3.0.0 |
| 56 |
*/ |
| 57 |
public function __toString() |
| 58 |
{ |
| 59 |
return $this->getUrl(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @since 4.0.0 add p2p param |
| 64 |
* @since 3.22.0 Add locale support |
| 65 |
* @since 3.0.0 |
| 66 |
*/ |
| 67 |
public function getUrl(): string |
| 68 |
{ |
| 69 |
$queryArgs = [ |
| 70 |
'post_type' => 'give_forms', |
| 71 |
'page' => self::SLUG, |
| 72 |
'donationFormID' => $this->donationFormID, |
| 73 |
'locale' => $this->locale, |
| 74 |
]; |
| 75 |
|
| 76 |
if (isset($_GET['campaignId'])) { |
| 77 |
$queryArgs['campaignId'] = $_GET['campaignId']; |
| 78 |
} |
| 79 |
|
| 80 |
// Check if it's P2P form |
| 81 |
$form = DB::table('give_campaigns') |
| 82 |
->where('form_id', $this->donationFormID) |
| 83 |
->where('campaign_type', CampaignType::CORE, '!=') |
| 84 |
->get(); |
| 85 |
|
| 86 |
if ($form) { |
| 87 |
$queryArgs['p2p'] = true; |
| 88 |
} |
| 89 |
|
| 90 |
return add_query_arg( |
| 91 |
[ |
| 92 |
$queryArgs, |
| 93 |
], |
| 94 |
admin_url('edit.php') |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @since 3.0.0 |
| 100 |
*/ |
| 101 |
public static function isRoute(): bool |
| 102 |
{ |
| 103 |
return isset($_GET['post_type'], $_GET['page']) && $_GET['post_type'] === 'give_forms' && $_GET['page'] === self::SLUG; |
| 104 |
} |
| 105 |
} |
| 106 |
|