| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Onboarding class |
| 5 |
* |
| 6 |
* @package Give |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Give\Onboarding\Setup; |
| 10 |
|
| 11 |
defined('ABSPATH') || exit; |
| 12 |
|
| 13 |
use Give\Framework\Http\ConnectServer\Client\ConnectClient; |
| 14 |
use Give\Helpers\Gateways\Stripe; |
| 15 |
use Give\Onboarding\FormRepository; |
| 16 |
use Give\PaymentGateways\PayPalCommerce\Repositories\MerchantDetails; |
| 17 |
|
| 18 |
/** |
| 19 |
* @since 2.8.0 |
| 20 |
*/ |
| 21 |
class PageView |
| 22 |
{ |
| 23 |
|
| 24 |
/** @var FormRepository */ |
| 25 |
protected $formRepository; |
| 26 |
|
| 27 |
/** |
| 28 |
* @since 2.25.0 |
| 29 |
* |
| 30 |
* @var ConnectClient |
| 31 |
*/ |
| 32 |
private $connectClient; |
| 33 |
|
| 34 |
/** |
| 35 |
* @since 2.25.0 Use 'ConnectClient' class |
| 36 |
* @since 2.8.0 |
| 37 |
* |
| 38 |
* @param FormRepository $formRepository |
| 39 |
* |
| 40 |
*/ |
| 41 |
public function __construct(FormRepository $formRepository, ConnectClient $connectClient) |
| 42 |
{ |
| 43 |
$this->formRepository = $formRepository; |
| 44 |
$this->connectClient = $connectClient; |
| 45 |
} |
| 46 |
|
| 47 |
public function render() |
| 48 |
{ |
| 49 |
$settings = wp_parse_args( |
| 50 |
get_option('give_onboarding', []), |
| 51 |
[ |
| 52 |
'addons' => [], |
| 53 |
] |
| 54 |
); |
| 55 |
ob_start(); |
| 56 |
include plugin_dir_path(__FILE__) . 'templates/index.html.php'; |
| 57 |
|
| 58 |
return ob_get_clean(); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Render templates |
| 63 |
* |
| 64 |
* @since 2.8.0 |
| 65 |
* |
| 66 |
* @param string $template |
| 67 |
* @param array $data The key/value pairs passed as $data are extracted as variables for use within the template file. |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
public function render_template($template, $data = []) |
| 72 |
{ |
| 73 |
ob_start(); |
| 74 |
include plugin_dir_path(__FILE__) . "templates/$template.html"; |
| 75 |
$output = ob_get_clean(); |
| 76 |
|
| 77 |
foreach ($data as $key => $value) { |
| 78 |
if (is_array($value)) { |
| 79 |
$value = implode('', $value); |
| 80 |
} |
| 81 |
$output = preg_replace('/{{\s*' . $key . '\s*}}/', $value, $output); |
| 82 |
} |
| 83 |
|
| 84 |
// Stripe unmerged tags. |
| 85 |
$output = preg_replace('/{{\s*\w*\s*}}/', '', $output); |
| 86 |
|
| 87 |
return $output; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* @return bool |
| 92 |
*/ |
| 93 |
public function isFormConfigured() |
| 94 |
{ |
| 95 |
return ! ! $this->formRepository->getDefaultFormID(); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @since 2.8.0 |
| 100 |
* @return bool |
| 101 |
* |
| 102 |
*/ |
| 103 |
public function isStripeSetup() |
| 104 |
{ |
| 105 |
return Stripe::isAccountConfigured(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Return whether ot not PayPal account connected. |
| 110 |
* |
| 111 |
* @since 2.8.0 |
| 112 |
* @return bool |
| 113 |
* |
| 114 |
*/ |
| 115 |
public function isPayPalSetup() |
| 116 |
{ |
| 117 |
return (bool)give(MerchantDetails::class)->accountIsConnected(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Returns a qualified image URL. |
| 122 |
* |
| 123 |
* @param string $src |
| 124 |
* |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
public function image($src) |
| 128 |
{ |
| 129 |
return GIVE_PLUGIN_URL . "build/assets/dist/images/setup-page/$src"; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Prepared Stripe Connect URL |
| 134 |
* |
| 135 |
* Copied from includes/gateways/stripe/includes/admin/admin-helpers.php |
| 136 |
* See `give_stripe_connect_button()` |
| 137 |
* |
| 138 |
* @since 2.25.0 Use 'ConnectClient' class |
| 139 |
* @since 2.8.0 |
| 140 |
*/ |
| 141 |
public function stripeConnectURL() |
| 142 |
{ |
| 143 |
return add_query_arg( |
| 144 |
[ |
| 145 |
'stripe_action' => 'connect', |
| 146 |
'mode' => give_is_test_mode() ? 'test' : 'live', |
| 147 |
'return_url' => rawurlencode( |
| 148 |
admin_url('edit.php?post_type=give_forms&page=give-settings&tab=gateways§ion=stripe-settings') |
| 149 |
), |
| 150 |
'website_url' => get_bloginfo('url'), |
| 151 |
'give_stripe_connected' => '0', |
| 152 |
], |
| 153 |
esc_url_raw($this->connectClient->getApiUrl('stripe/connect.php')) |
| 154 |
); |
| 155 |
} |
| 156 |
} |
| 157 |
|