| 1 |
<?php |
| 2 |
/** |
| 3 |
* Form Editor Assets. |
| 4 |
* |
| 5 |
* Handles asset registration and enqueuing for the donation form editor. |
| 6 |
* |
| 7 |
* @package SureDonation |
| 8 |
* @since 0.0.1 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace SureDonation\Inc\FormEditor; |
| 12 |
|
| 13 |
use SureDonation\Inc\Traits\Get_Instance; |
| 14 |
use SureDonation\Inc\Post_Types\Donation_Form; |
| 15 |
|
| 16 |
// Exit if accessed directly. |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Assets Class. |
| 23 |
* |
| 24 |
* @since 0.0.1 |
| 25 |
*/ |
| 26 |
class Assets { |
| 27 |
use Get_Instance; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
* |
| 32 |
* @since 0.0.1 |
| 33 |
*/ |
| 34 |
public function __construct() { |
| 35 |
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ] ); |
| 36 |
add_action( 'init', [ $this, 'register_form_settings_meta' ] ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Enqueue editor assets for the donation form editor. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
* @since 0.0.1 |
| 44 |
*/ |
| 45 |
public function enqueue_editor_assets() { |
| 46 |
$screen = get_current_screen(); |
| 47 |
|
| 48 |
// Only load on donation form editor. |
| 49 |
if ( ! $screen || Donation_Form::POST_TYPE !== $screen->post_type ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
// Get the asset file for dependencies. |
| 54 |
$editor_asset_file = SUREDONATION_DIR . 'assets/build/editor/editor.asset.php'; |
| 55 |
$editor_asset = file_exists( $editor_asset_file ) |
| 56 |
? include $editor_asset_file |
| 57 |
: [ |
| 58 |
'dependencies' => [ |
| 59 |
'wp-plugins', |
| 60 |
'wp-editor', |
| 61 |
'wp-components', |
| 62 |
'wp-data', |
| 63 |
'wp-element', |
| 64 |
'wp-i18n', |
| 65 |
], |
| 66 |
'version' => SUREDONATION_VER, |
| 67 |
]; |
| 68 |
|
| 69 |
// Editor plugin JS. |
| 70 |
wp_enqueue_script( |
| 71 |
'suredonation-form-editor', |
| 72 |
SUREDONATION_URL . 'assets/build/editor/editor.js', |
| 73 |
$editor_asset['dependencies'], |
| 74 |
$editor_asset['version'], |
| 75 |
true |
| 76 |
); |
| 77 |
|
| 78 |
// Editor styles. |
| 79 |
wp_enqueue_style( |
| 80 |
'suredonation-form-editor', |
| 81 |
SUREDONATION_URL . 'assets/build/editor/editor.css', |
| 82 |
[ 'wp-components' ], |
| 83 |
$editor_asset['version'] |
| 84 |
); |
| 85 |
|
| 86 |
// Localized data. |
| 87 |
wp_localize_script( |
| 88 |
'suredonation-form-editor', |
| 89 |
'suredonationFormEditor', |
| 90 |
[ |
| 91 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 92 |
'nonce' => wp_create_nonce( 'suredonation_editor_nonce' ), |
| 93 |
'postType' => Donation_Form::POST_TYPE, |
| 94 |
] |
| 95 |
); |
| 96 |
|
| 97 |
// Set script translations. |
| 98 |
wp_set_script_translations( 'suredonation-form-editor', 'suredonation' ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Register form settings meta fields. |
| 103 |
* |
| 104 |
* These meta fields store form-level settings like submit button text, |
| 105 |
* success message, and confirmation type. |
| 106 |
* |
| 107 |
* @return void |
| 108 |
* @since 0.0.1 |
| 109 |
*/ |
| 110 |
public function register_form_settings_meta() { |
| 111 |
$post_type = Donation_Form::POST_TYPE; |
| 112 |
|
| 113 |
// Submit button text. |
| 114 |
register_post_meta( |
| 115 |
$post_type, |
| 116 |
'_suredonation_form_submitButtonText', |
| 117 |
[ |
| 118 |
'type' => 'string', |
| 119 |
'description' => __( 'Submit button text.', 'suredonation' ), |
| 120 |
'single' => true, |
| 121 |
'default' => 'Donate', |
| 122 |
'show_in_rest' => true, |
| 123 |
'sanitize_callback' => 'sanitize_text_field', |
| 124 |
'auth_callback' => function () { |
| 125 |
return current_user_can( 'edit_posts' ); |
| 126 |
}, |
| 127 |
] |
| 128 |
); |
| 129 |
|
| 130 |
// Success message. |
| 131 |
register_post_meta( |
| 132 |
$post_type, |
| 133 |
'_suredonation_form_successMessage', |
| 134 |
[ |
| 135 |
'type' => 'string', |
| 136 |
'description' => __( 'Success message shown after donation.', 'suredonation' ), |
| 137 |
'single' => true, |
| 138 |
'default' => 'Thank you for your donation!', |
| 139 |
'show_in_rest' => true, |
| 140 |
'sanitize_callback' => 'sanitize_textarea_field', |
| 141 |
'auth_callback' => function () { |
| 142 |
return current_user_can( 'edit_posts' ); |
| 143 |
}, |
| 144 |
] |
| 145 |
); |
| 146 |
|
| 147 |
// Confirmation type. |
| 148 |
register_post_meta( |
| 149 |
$post_type, |
| 150 |
'_suredonation_form_confirmationType', |
| 151 |
[ |
| 152 |
'type' => 'string', |
| 153 |
'description' => __( 'What happens after successful donation.', 'suredonation' ), |
| 154 |
'single' => true, |
| 155 |
'default' => 'message', |
| 156 |
'show_in_rest' => true, |
| 157 |
'sanitize_callback' => function ( $value ) { |
| 158 |
$allowed = [ 'message', 'redirect' ]; |
| 159 |
return in_array( $value, $allowed, true ) ? $value : 'message'; |
| 160 |
}, |
| 161 |
'auth_callback' => function () { |
| 162 |
return current_user_can( 'edit_posts' ); |
| 163 |
}, |
| 164 |
] |
| 165 |
); |
| 166 |
|
| 167 |
// Redirect URL. |
| 168 |
register_post_meta( |
| 169 |
$post_type, |
| 170 |
'_suredonation_form_redirectUrl', |
| 171 |
[ |
| 172 |
'type' => 'string', |
| 173 |
'description' => __( 'URL to redirect after donation.', 'suredonation' ), |
| 174 |
'single' => true, |
| 175 |
'default' => '', |
| 176 |
'show_in_rest' => true, |
| 177 |
'sanitize_callback' => 'esc_url_raw', |
| 178 |
'auth_callback' => function () { |
| 179 |
return current_user_can( 'edit_posts' ); |
| 180 |
}, |
| 181 |
] |
| 182 |
); |
| 183 |
} |
| 184 |
} |
| 185 |
|