| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Form Block class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* ConvertKit Form Block for Gutenberg and Shortcode. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Block_Form extends ConvertKit_Block { |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor |
| 19 |
* |
| 20 |
* @since 1.9.6 |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
|
| 24 |
// Register this as a shortcode in the ConvertKit Plugin. |
| 25 |
add_filter( 'convertkit_shortcodes', array( $this, 'register' ) ); |
| 26 |
|
| 27 |
// Register this as a Gutenberg block in the ConvertKit Plugin. |
| 28 |
add_filter( 'convertkit_blocks', array( $this, 'register' ) ); |
| 29 |
|
| 30 |
// Enqueue scripts for this Gutenberg Block in the editor view. |
| 31 |
add_action( 'convertkit_gutenberg_enqueue_scripts', array( $this, 'enqueue_scripts_editor' ) ); |
| 32 |
|
| 33 |
// Enqueue styles for this Gutenberg Block in the editor view. |
| 34 |
add_action( 'convertkit_gutenberg_enqueue_styles', array( $this, 'enqueue_styles_editor' ) ); |
| 35 |
|
| 36 |
// Enqueue scripts and styles for this Gutenberg Block in the editor and frontend views. |
| 37 |
add_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend', array( $this, 'enqueue_styles' ) ); |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Enqueues scripts for this Gutenberg Block in the editor view. |
| 43 |
* |
| 44 |
* @since 1.9.6.5 |
| 45 |
*/ |
| 46 |
public function enqueue_scripts_editor() { |
| 47 |
|
| 48 |
wp_enqueue_script( 'convertkit-gutenberg-block-form', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-form.js', array( 'convertkit-gutenberg' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Enqueues styles for this Gutenberg Block in the editor view. |
| 54 |
* |
| 55 |
* @since 1.9.6.9 |
| 56 |
*/ |
| 57 |
public function enqueue_styles_editor() { |
| 58 |
|
| 59 |
wp_enqueue_style( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/gutenberg.css', array( 'wp-edit-blocks' ), CONVERTKIT_PLUGIN_VERSION ); |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Enqueues styles for this Gutenberg Block in the editor and frontend views. |
| 65 |
* |
| 66 |
* @since 2.3.3 |
| 67 |
*/ |
| 68 |
public function enqueue_styles() { |
| 69 |
|
| 70 |
wp_enqueue_style( 'convertkit-form', CONVERTKIT_PLUGIN_URL . 'resources/frontend/css/form.css', array(), CONVERTKIT_PLUGIN_VERSION ); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns this block's programmatic name, excluding the convertkit- prefix. |
| 76 |
* |
| 77 |
* @since 1.9.6 |
| 78 |
* |
| 79 |
* @return string |
| 80 |
*/ |
| 81 |
public function get_name() { |
| 82 |
|
| 83 |
/** |
| 84 |
* This will register as: |
| 85 |
* - a shortcode, with the name [convertkit_form]. |
| 86 |
* - a shortcode, with the name [convertkit], for backward compat. |
| 87 |
* - a Gutenberg block, with the name convertkit/form. |
| 88 |
*/ |
| 89 |
return 'form'; |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns this block's Title, Icon, Categories, Keywords and properties. |
| 95 |
* |
| 96 |
* @since 1.9.6 |
| 97 |
* |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
public function get_overview() { |
| 101 |
|
| 102 |
$convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' ); |
| 103 |
$settings = new ConvertKit_Settings(); |
| 104 |
|
| 105 |
return array( |
| 106 |
'title' => __( 'Kit Form', 'convertkit' ), |
| 107 |
'description' => __( 'Displays a Kit Form.', 'convertkit' ), |
| 108 |
'icon' => 'resources/backend/images/block-icon-form.svg', |
| 109 |
'category' => 'convertkit', |
| 110 |
'keywords' => array( |
| 111 |
__( 'ConvertKit', 'convertkit' ), |
| 112 |
__( 'Kit', 'convertkit' ), |
| 113 |
__( 'Form', 'convertkit' ), |
| 114 |
), |
| 115 |
|
| 116 |
// Function to call when rendering as a block or a shortcode on the frontend web site. |
| 117 |
'render_callback' => array( $this, 'render' ), |
| 118 |
|
| 119 |
// Shortcode: TinyMCE / QuickTags Modal Width and Height. |
| 120 |
'modal' => array( |
| 121 |
'width' => 500, |
| 122 |
'height' => 55, |
| 123 |
), |
| 124 |
|
| 125 |
// Shortcode: Include a closing [/shortcode] tag when using TinyMCE or QuickTag Modals. |
| 126 |
'shortcode_include_closing_tag' => false, |
| 127 |
|
| 128 |
// Gutenberg: Block Icon in Editor. |
| 129 |
'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-form.svg' ), |
| 130 |
|
| 131 |
// Gutenberg: Example image showing how this block looks when choosing it in Gutenberg. |
| 132 |
'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-form.png', |
| 133 |
|
| 134 |
// Help descriptions, displayed when no API key / resources exist and this block/shortcode is added. |
| 135 |
'no_access_token' => array( |
| 136 |
'notice' => __( 'Not connected to Kit.', 'convertkit' ), |
| 137 |
'link' => convertkit_get_setup_wizard_plugin_link(), |
| 138 |
'link_text' => __( 'Click here to connect your Kit account.', 'convertkit' ), |
| 139 |
'instruction_text' => __( 'Connect your Kit account at Settings > Kit, and then refresh this page to select a form.', 'convertkit' ), |
| 140 |
), |
| 141 |
'no_resources' => array( |
| 142 |
'notice' => __( 'No forms exist in Kit.', 'convertkit' ), |
| 143 |
'link' => convertkit_get_new_form_url(), |
| 144 |
'link_text' => __( 'Click here to create your first form.', 'convertkit' ), |
| 145 |
'instruction_text' => __( 'Add a form to your Kit account, and then refresh this page to select a form.', 'convertkit' ), |
| 146 |
), |
| 147 |
|
| 148 |
// Gutenberg: Help descriptions, displayed when no settings defined for a newly added Block. |
| 149 |
'gutenberg_help_description' => __( 'Select a Form using the Form option in the Gutenberg sidebar.', 'convertkit' ), |
| 150 |
|
| 151 |
// Gutenberg: JS function to call when rendering the block preview in the Gutenberg editor. |
| 152 |
// If not defined, render_callback above will be used. |
| 153 |
'gutenberg_preview_render_callback' => 'convertKitGutenbergFormBlockRenderPreview', |
| 154 |
|
| 155 |
// General: Any other strings for use in JS that need to support translation / i18n. |
| 156 |
'i18n' => array( |
| 157 |
/* translators: Form name in ConvertKit */ |
| 158 |
'gutenberg_form_modal' => __( 'Modal form "%s" selected. View on the frontend site to see the modal form.', 'convertkit' ), |
| 159 |
|
| 160 |
/* translators: Form name in ConvertKit */ |
| 161 |
'gutenberg_form_slide_in' => __( 'Slide in form "%s" selected. View on the frontend site to see the slide in form.', 'convertkit' ), |
| 162 |
|
| 163 |
/* translators: Form name in ConvertKit */ |
| 164 |
'gutenberg_form_sticky_bar' => __( 'Sticky bar form "%s" selected. View on the frontend site to see the sticky bar form.', 'convertkit' ), |
| 165 |
), |
| 166 |
|
| 167 |
// Whether an API Key exists in the Plugin, and are the required resources (forms) available. |
| 168 |
// If no API Key is specified in the Plugin's settings, render the "No API Key" output. |
| 169 |
'has_access_token' => $settings->has_access_and_refresh_token(), |
| 170 |
'has_resources' => $convertkit_forms->exist(), |
| 171 |
); |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Returns this block's Attributes |
| 177 |
* |
| 178 |
* @since 1.9.6.5 |
| 179 |
* |
| 180 |
* @return array |
| 181 |
*/ |
| 182 |
public function get_attributes() { |
| 183 |
|
| 184 |
return array( |
| 185 |
'form' => array( |
| 186 |
'type' => 'string', |
| 187 |
), |
| 188 |
|
| 189 |
// get_supports() style, color and typography attributes. |
| 190 |
'align' => array( |
| 191 |
'type' => 'string', |
| 192 |
), |
| 193 |
'style' => array( |
| 194 |
'type' => 'object', |
| 195 |
), |
| 196 |
'backgroundColor' => array( |
| 197 |
'type' => 'string', |
| 198 |
), |
| 199 |
|
| 200 |
// Always required for Gutenberg. |
| 201 |
'is_gutenberg_example' => array( |
| 202 |
'type' => 'boolean', |
| 203 |
'default' => false, |
| 204 |
), |
| 205 |
); |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Returns this block's supported built-in Attributes. |
| 211 |
* |
| 212 |
* @since 1.9.7.4 |
| 213 |
* |
| 214 |
* @return array Supports |
| 215 |
*/ |
| 216 |
public function get_supports() { |
| 217 |
|
| 218 |
return array( |
| 219 |
'align' => true, |
| 220 |
'className' => true, |
| 221 |
'color' => array( |
| 222 |
'link' => false, |
| 223 |
'background' => true, |
| 224 |
'text' => false, |
| 225 |
), |
| 226 |
'spacing' => array( |
| 227 |
'margin' => true, |
| 228 |
'padding' => true, |
| 229 |
), |
| 230 |
); |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Returns this block's Fields |
| 236 |
* |
| 237 |
* @since 1.9.6 |
| 238 |
* |
| 239 |
* @return bool|array |
| 240 |
*/ |
| 241 |
public function get_fields() { |
| 242 |
|
| 243 |
// Bail if the request is not for the WordPress Administration or frontend editor. |
| 244 |
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
// Get ConvertKit Forms. |
| 249 |
$forms = array(); |
| 250 |
$convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' ); |
| 251 |
if ( $convertkit_forms->exist() ) { |
| 252 |
foreach ( $convertkit_forms->get() as $form ) { |
| 253 |
// Legacy forms don't include a `format` key, so define them as inline. |
| 254 |
$forms[ absint( $form['id'] ) ] = sprintf( |
| 255 |
'%s [%s]', |
| 256 |
sanitize_text_field( $form['name'] ), |
| 257 |
( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' ) |
| 258 |
); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
return array( |
| 263 |
'form' => array( |
| 264 |
'label' => __( 'Form', 'convertkit' ), |
| 265 |
'type' => 'resource', |
| 266 |
'resource' => 'forms', |
| 267 |
'values' => $forms, |
| 268 |
'data' => array( |
| 269 |
// Used by resources/backend/js/gutenberg-block-form.js to determine the selected form's format |
| 270 |
// (modal, slide in, sticky bar) and output a message in the block editor for the preview to explain |
| 271 |
// why some formats cannot be previewed. |
| 272 |
'forms' => ( $convertkit_forms->exist() ? $convertkit_forms->get() : array() ), |
| 273 |
), |
| 274 |
), |
| 275 |
); |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Returns this block's UI panels / sections. |
| 281 |
* |
| 282 |
* @since 1.9.6 |
| 283 |
* |
| 284 |
* @return bool|array |
| 285 |
*/ |
| 286 |
public function get_panels() { |
| 287 |
|
| 288 |
// Bail if the request is not for the WordPress Administration or frontend editor. |
| 289 |
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { |
| 290 |
return false; |
| 291 |
} |
| 292 |
|
| 293 |
return array( |
| 294 |
'general' => array( |
| 295 |
'label' => __( 'General', 'convertkit' ), |
| 296 |
'fields' => array( |
| 297 |
'form', |
| 298 |
), |
| 299 |
), |
| 300 |
); |
| 301 |
|
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Returns this block's Default Values |
| 306 |
* |
| 307 |
* @since 1.9.6 |
| 308 |
* |
| 309 |
* @return array |
| 310 |
*/ |
| 311 |
public function get_default_values() { |
| 312 |
|
| 313 |
return array( |
| 314 |
'form' => '', |
| 315 |
'id' => '', // Backward compat. |
| 316 |
); |
| 317 |
|
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Returns the block's output, based on the supplied configuration attributes. |
| 322 |
* |
| 323 |
* @since 1.9.6 |
| 324 |
* |
| 325 |
* @param array $atts Block / Shortcode Attributes. |
| 326 |
* @return string Output |
| 327 |
*/ |
| 328 |
public function render( $atts ) { |
| 329 |
|
| 330 |
global $post; |
| 331 |
|
| 332 |
$post_id = is_a( $post, 'WP_Post' ) ? $post->ID : 0; |
| 333 |
|
| 334 |
// Check if the Block Visibility Plugin permits displaying this block. |
| 335 |
if ( ! $this->is_block_visible( $atts ) ) { |
| 336 |
// Block should not be displayed due to Block Visibility Plugin conditions. |
| 337 |
// Return a blank string now. |
| 338 |
return ''; |
| 339 |
} |
| 340 |
|
| 341 |
// Parse shortcode attributes, defining fallback defaults if required. |
| 342 |
$atts = shortcode_atts( |
| 343 |
$this->get_default_values(), |
| 344 |
$this->sanitize_atts( $atts ), |
| 345 |
$this->get_name() |
| 346 |
); |
| 347 |
|
| 348 |
// Setup Settings class. |
| 349 |
$settings = new ConvertKit_Settings(); |
| 350 |
|
| 351 |
// Determine Form ID. |
| 352 |
// 'id' attribute is for backward compat. |
| 353 |
$form_id = 0; |
| 354 |
if ( $atts['form'] > 0 ) { |
| 355 |
$form_id = $atts['form']; |
| 356 |
} elseif ( $atts['id'] > 0 ) { |
| 357 |
$form_id = $atts['id']; |
| 358 |
} |
| 359 |
|
| 360 |
// If no Form ID specified, bail. |
| 361 |
if ( ! $form_id ) { |
| 362 |
if ( $settings->debug_enabled() ) { |
| 363 |
return '<!-- No Form ID Specified -->'; |
| 364 |
} |
| 365 |
|
| 366 |
return ''; |
| 367 |
} |
| 368 |
|
| 369 |
// Get Form HTML. |
| 370 |
$forms = new ConvertKit_Resource_Forms( 'output_form' ); |
| 371 |
$form = $forms->get_html( $form_id, $post_id ); |
| 372 |
|
| 373 |
// If an error occured, it might be that we're requesting a Form ID that exists in ConvertKit |
| 374 |
// but does not yet exist in the Plugin's Form Resources. |
| 375 |
// If so, refresh the Form Resources and try again. |
| 376 |
if ( is_wp_error( $form ) ) { |
| 377 |
// Refresh Forms from the API. |
| 378 |
$forms->refresh(); |
| 379 |
|
| 380 |
// Get Form HTML again. |
| 381 |
$form = $forms->get_html( $form_id, $post_id ); |
| 382 |
} |
| 383 |
|
| 384 |
// If an error still occured, the shortcode might be from the ConvertKit App for a Legacy Form ID |
| 385 |
// These ConvertKit App shortcodes, for some reason, use a different Form ID than the one presented |
| 386 |
// to us in the API. |
| 387 |
// For example, a Legacy Form ID might be 470099, but the ConvertKit app says to use the shortcode [convertkit form=5281783]). |
| 388 |
// In this instance, fetch the Form HTML without checking that the Form ID exists in the Form Resources. |
| 389 |
if ( is_wp_error( $form ) ) { |
| 390 |
// Initialize the API. |
| 391 |
$api = new ConvertKit_API_V4( |
| 392 |
CONVERTKIT_OAUTH_CLIENT_ID, |
| 393 |
CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, |
| 394 |
$settings->get_access_token(), |
| 395 |
$settings->get_refresh_token(), |
| 396 |
$settings->debug_enabled(), |
| 397 |
'output_form' |
| 398 |
); |
| 399 |
|
| 400 |
// Return Legacy Form HTML from the API, which bypasses any internal Plugin check to see if the Form ID exists. |
| 401 |
$form = $api->get_form_html( $form_id, $settings->get_api_key() ); |
| 402 |
} |
| 403 |
|
| 404 |
// Finally, if we still get an error, there's nothing more we can do. The Form ID isn't valid. |
| 405 |
if ( is_wp_error( $form ) ) { |
| 406 |
if ( $settings->debug_enabled() ) { |
| 407 |
return '<!-- ' . $form->get_error_message() . ' -->'; |
| 408 |
} |
| 409 |
|
| 410 |
return ''; |
| 411 |
} |
| 412 |
|
| 413 |
// Build HTML. |
| 414 |
// For the block editor, don't include compiled CSS classes and styles, |
| 415 |
// as the block editor will add these to the parent container. |
| 416 |
// Otherwise the block will render incorrectly with double padding, double margins etc. |
| 417 |
// If there's no Form HTML, it's a non-inline form, so don't render any output. |
| 418 |
if ( ! $this->is_block_editor_request() && ! empty( $form ) ) { |
| 419 |
$form = sprintf( |
| 420 |
'<div class="%s" style="%s">%s</div>', |
| 421 |
implode( ' ', map_deep( $this->get_css_classes(), 'sanitize_html_class' ) ), |
| 422 |
implode( ';', map_deep( $this->get_css_styles( $atts ), 'esc_attr' ) ), |
| 423 |
$form |
| 424 |
); |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Filter the block's content immediately before it is output. |
| 429 |
* |
| 430 |
* @since 1.9.6 |
| 431 |
* |
| 432 |
* @param string $form ConvertKit Form HTML. |
| 433 |
* @param array $atts Block Attributes. |
| 434 |
* @param int $form_id Form ID. |
| 435 |
*/ |
| 436 |
$form = apply_filters( 'convertkit_block_form_render', $form, $atts, $form_id ); |
| 437 |
|
| 438 |
/** |
| 439 |
* Backward compat. filter for < 1.9.6. Filter the block's content immediately before it is output. |
| 440 |
* |
| 441 |
* @since 1.0.0 |
| 442 |
* |
| 443 |
* @param string $form ConvertKit Form HTML. |
| 444 |
* @param array $atts Block Attributes. |
| 445 |
*/ |
| 446 |
$form = apply_filters( 'wp_convertkit_get_form_embed', $form, $atts ); |
| 447 |
|
| 448 |
return $form; |
| 449 |
|
| 450 |
} |
| 451 |
|
| 452 |
} |
| 453 |
|