array( __CLASS__, 'render_block' ) ) ); } /** * @param array|string $atts Shortcode attributes. */ public static function shortcode( $atts ): string { $atts = shortcode_atts( array( 'id' => '', 'height' => '600', 'title' => '', 'mode' => 'inline', 'button' => '', ), is_array( $atts ) ? $atts : array(), self::SHORTCODE ); return self::html( Cast::string( $atts['id'] ), Cast::int( $atts['height'] ), Cast::string( $atts['title'] ), Cast::string( $atts['mode'] ), Cast::string( $atts['button'] ) ); } /** * @param array $attributes Block attributes. */ public static function render_block( array $attributes ): string { $html = self::html( Cast::string( $attributes['formUuid'] ?? '' ), Cast::int( $attributes['height'] ?? 600 ), Cast::string( $attributes['title'] ?? '' ), Cast::string( $attributes['mode'] ?? 'inline' ), Cast::string( $attributes['buttonText'] ?? '' ) ); return '' === $html ? '' : '
' . $html . '
'; } /** * @param mixed $value Candidate uuid. */ public static function is_uuid( $value ): bool { return 1 === preg_match( '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i', Cast::string( $value ) ); } public static function html( string $uuid, int $height = 600, string $title = '', string $mode = 'inline', string $button = '' ): string { if ( ! self::is_uuid( $uuid ) ) { return current_user_can( 'edit_posts' ) ? '

' . esc_html__( 'AwesomeFor.ms: choose a form for this block.', 'awesomeforms' ) . '

' : ''; } $height = max( 200, min( 5000, $height ) ); $title = '' !== $title ? $title : __( 'Form', 'awesomeforms' ); $base = Connection::service_url(); wp_enqueue_script( 'awesomeforms-front', AWESOMEFORMS_URL . 'assets/js/front.js', array(), AWESOMEFORMS_VERSION, true ); if ( 'popup' === $mode ) { wp_enqueue_script( 'awesomeforms-sdk', $base . '/embed/sdk.js', array(), AWESOMEFORMS_VERSION, true ); return sprintf( '', esc_attr( strtolower( $uuid ) ), esc_attr( $title ), esc_html( '' !== $button ? $button : __( 'Open the form', 'awesomeforms' ) ) ); } return sprintf( '', esc_url( $base . '/embed/' . strtolower( $uuid ) ), esc_attr( $title ), $height, esc_attr( strtolower( $uuid ) ) ); } }