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