| 1 |
<?php |
| 2 |
/** |
| 3 |
* Form builder: Template explorer |
| 4 |
* |
| 5 |
* @package SimplePay |
| 6 |
* @subpackage Core |
| 7 |
* @copyright Copyright (c) 2022, Sandhills Development, LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 4.4.3 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SimplePay\Core\Admin\FormBuilder; |
| 13 |
|
| 14 |
use SimplePay\Core\EventManagement\SubscriberInterface; |
| 15 |
use SimplePay\Core\License\LicenseAwareInterface; |
| 16 |
use SimplePay\Core\License\LicenseAwareTrait; |
| 17 |
|
| 18 |
/** |
| 19 |
* TemplateExplorer class. |
| 20 |
* |
| 21 |
* @since 4.4.3 |
| 22 |
*/ |
| 23 |
class TemplateExplorer implements SubscriberInterface, LicenseAwareInterface { |
| 24 |
|
| 25 |
use LicenseAwareTrait; |
| 26 |
|
| 27 |
/** |
| 28 |
* {@inheritdoc} |
| 29 |
*/ |
| 30 |
public function get_subscribed_events() { |
| 31 |
if ( false === $this->is_license_valid() ) { |
| 32 |
return array(); |
| 33 |
} |
| 34 |
|
| 35 |
return array( |
| 36 |
'admin_init' => 'maybe_restrict_template', |
| 37 |
'edit_form_top' => 'maybe_show_template_explorer', |
| 38 |
'admin_notices' => 'maybe_show_previewing_template', |
| 39 |
'init' => array( 'maybe_set_post_type_queryable', 9 ), |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Restricts template usage to the current license type. |
| 45 |
* |
| 46 |
* Override the global $_GET to ensure the template is only used with the current license. |
| 47 |
* |
| 48 |
* @todo This is hacky and should be refactored once templates are applied to the form itself |
| 49 |
* vs. injecting in to form settings individually. |
| 50 |
* |
| 51 |
* @since 4.4.3 |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function maybe_restrict_template() { |
| 56 |
if ( ! isset( $_GET['simpay-template'] ) ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
$template_id = sanitize_text_field( $_GET['simpay-template'] ); |
| 61 |
$template = __unstable_simpay_get_payment_form_template( |
| 62 |
$template_id |
| 63 |
); |
| 64 |
|
| 65 |
if ( null === $template ) { |
| 66 |
$_GET['simpay-template'] = null; |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
/** @var array<string> $licenses */ |
| 71 |
$licenses = $template['license']; |
| 72 |
$license_level = $this->license->get_level(); |
| 73 |
|
| 74 |
if ( ! in_array( $license_level, $licenses, true ) ) { |
| 75 |
$_GET['simpay-template'] = null; |
| 76 |
return; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Outputs a notice that a template is being used before the form is saved. |
| 82 |
* |
| 83 |
* @since 4.4.3 |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
function maybe_show_previewing_template() { |
| 88 |
$screen = get_current_screen(); |
| 89 |
|
| 90 |
if ( null === $screen ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
if ( 'simple-pay' !== $screen->id || 'add' !== $screen->action ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! isset( $_GET['simpay-template'] ) ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
$template_id = sanitize_text_field( $_GET['simpay-template'] ); |
| 103 |
$template = __unstable_simpay_get_payment_form_template( |
| 104 |
$template_id |
| 105 |
); |
| 106 |
|
| 107 |
if ( null === $template ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
$new_url = add_query_arg( |
| 112 |
array( |
| 113 |
'post_type' => 'simple-pay', |
| 114 |
), |
| 115 |
admin_url( 'post-new.php' ) |
| 116 |
); |
| 117 |
|
| 118 |
// @todo use a ViewLoader |
| 119 |
include_once SIMPLE_PAY_DIR . '/views/admin-notice-template-preview.php'; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Outputs the template explorer when adding a new form. |
| 124 |
* |
| 125 |
* @since 4.4.3 |
| 126 |
* |
| 127 |
* @param \WP_Post $post Payment form post object. |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
public function maybe_show_template_explorer( $post ) { |
| 131 |
// We are not adding a payment form, do not show the explorer. |
| 132 |
if ( |
| 133 |
! isset( $_GET['post_type'] ) || |
| 134 |
'simple-pay' !== sanitize_text_field( $_GET['post_type'] ) |
| 135 |
) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
// A payment form template is in the URL, do not show the explorer. |
| 140 |
if ( isset( $_GET['simpay-template'] ) ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
// The payment form has already been created, do not show the explorer. |
| 145 |
if ( 'auto-draft' !== $post->post_status ) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
$this->render(); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Renders the template explorer. |
| 154 |
* |
| 155 |
* @since 4.6.5 |
| 156 |
* |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
public function render() { |
| 160 |
$asset_file = SIMPLE_PAY_INC . 'core/assets/js/dist/simpay-admin-form-template-explorer.asset.php'; |
| 161 |
|
| 162 |
if ( ! file_exists( $asset_file ) ) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
$asset_data = require $asset_file; |
| 167 |
|
| 168 |
wp_enqueue_script( |
| 169 |
'simpay-admin-form-template-explorer', |
| 170 |
SIMPLE_PAY_INC_URL . 'core/assets/js/dist/simpay-admin-form-template-explorer.js', |
| 171 |
$asset_data['dependencies'], |
| 172 |
$asset_data['version'], |
| 173 |
true |
| 174 |
); |
| 175 |
|
| 176 |
$is_lite = $this->license->is_lite(); |
| 177 |
|
| 178 |
wp_localize_script( |
| 179 |
'simpay-admin-form-template-explorer', |
| 180 |
'simpayFormBuilderTemplateExplorer', |
| 181 |
array( |
| 182 |
'licenseLevel' => $this->license->get_level(), |
| 183 |
'addNewUrl' => add_query_arg( |
| 184 |
array( |
| 185 |
'post_type' => 'simple-pay', |
| 186 |
), |
| 187 |
admin_url( 'post-new.php' ) |
| 188 |
), |
| 189 |
'suggestUrl' => simpay_ga_url( |
| 190 |
'https://wpsimplepay.com/payment-form-template-suggestion/', |
| 191 |
'template-explorer', |
| 192 |
'suggest a template' |
| 193 |
), |
| 194 |
'upgradeUrl' => simpay_ga_url( |
| 195 |
( |
| 196 |
$is_lite |
| 197 |
? 'https://wpsimplepay.com/lite-vs-pro/' |
| 198 |
// add_query_arg escapes this, which doesn't play nicely on the client. |
| 199 |
: 'https://wpsimplepay.com/pricing/?license_key=' . $this->license->get_key() |
| 200 |
), |
| 201 |
'template-explorer', |
| 202 |
$is_lite ? 'Upgrade to Pro' : 'Upgrade Now' |
| 203 |
), |
| 204 |
'alreadyPurchasedUrl' => simpay_docs_link( |
| 205 |
'Already purchased?', |
| 206 |
( |
| 207 |
$is_lite |
| 208 |
? 'upgrading-wp-simple-pay-lite-to-pro' |
| 209 |
: 'activate-wp-simple-pay-pro-license' |
| 210 |
), |
| 211 |
'template-explorer', |
| 212 |
true |
| 213 |
), |
| 214 |
'hasNew' => ( |
| 215 |
isset( $_GET['page'] ) && |
| 216 |
__unstable_simpay_has_new_form_templates() |
| 217 |
), |
| 218 |
'categories' => array_merge( |
| 219 |
array( |
| 220 |
'' => esc_html__( 'All Templates', 'stripe' ), |
| 221 |
), |
| 222 |
__unstable_simpay_get_form_template_categories() |
| 223 |
), |
| 224 |
'templates' => array_values( |
| 225 |
__unstable_simpay_get_payment_form_templates() |
| 226 |
), |
| 227 |
'isLite' => $is_lite, |
| 228 |
) |
| 229 |
); |
| 230 |
|
| 231 |
wp_set_script_translations( |
| 232 |
'simpay-admin-form-template-explorer', |
| 233 |
'stripe', |
| 234 |
SIMPLE_PAY_DIR . '/languages' |
| 235 |
); |
| 236 |
|
| 237 |
wp_enqueue_style( |
| 238 |
'simpay-admin-form-template-explorer', |
| 239 |
SIMPLE_PAY_INC_URL . 'core/assets/css/simpay-admin-form-template-explorer.min.css', |
| 240 |
array( |
| 241 |
'wp-components', |
| 242 |
), |
| 243 |
$asset_data['version'] |
| 244 |
); |
| 245 |
|
| 246 |
echo '<div id="simpay-form-template-explorer"></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
/** |
| 251 |
* Temporarily sets the `simple-pay` post type `publicaly_queryable` to `true` |
| 252 |
* when a payment form template is detected. |
| 253 |
* |
| 254 |
* @since 4.4.3 |
| 255 |
* |
| 256 |
* @return void |
| 257 |
*/ |
| 258 |
public function maybe_set_post_type_queryable() { |
| 259 |
if ( ! isset( $_GET['simpay-template'], $_GET['post_type'] ) ) { |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
if ( 'simple-pay' !== sanitize_text_field( $_GET['post_type'] ) ) { |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
if ( ! is_admin() ) { |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
add_filter( |
| 272 |
'register_post_type_args', |
| 273 |
function( $args, $post_type ) { |
| 274 |
if ( 'simple-pay' !== $post_type ) { |
| 275 |
return $args; |
| 276 |
} |
| 277 |
|
| 278 |
$args['publicly_queryable'] = true; |
| 279 |
|
| 280 |
return $args; |
| 281 |
}, |
| 282 |
10, |
| 283 |
2 |
| 284 |
); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Determines if the current license is valid and the Templat Explorer can be used. |
| 289 |
* |
| 290 |
* @since 4.4.6 |
| 291 |
* |
| 292 |
* @return bool |
| 293 |
*/ |
| 294 |
private function is_license_valid() { |
| 295 |
if ( $this->license->is_lite() ) { |
| 296 |
return true; |
| 297 |
} |
| 298 |
|
| 299 |
if ( empty( $this->license->get_key() ) ) { |
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
switch ( $this->license->get_status() ) { |
| 304 |
case 'expired': |
| 305 |
if ( ! $this->license->is_in_grace_period() ) { |
| 306 |
return false; |
| 307 |
} |
| 308 |
break; |
| 309 |
case 'invalid': |
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
return true; |
| 314 |
} |
| 315 |
|
| 316 |
} |
| 317 |
|