true, 'strategy' => 'defer', ) ); wp_add_inline_script( 'edonating-embed', 'window.edonatingOrigin = ' . wp_json_encode( EDONATING_ORIGIN ) . ';', 'before' ); } add_action( 'init', 'edonating_register_assets' ); /** * The choices a page can make about how a form looks, and the values each accepts. Anything else is ignored, and an * empty value means "as set in the edonating dashboard". * * @return array */ function edonating_look_options() { return array( 'design' => array( 'soft', 'outline', 'bold' ), 'font' => array( 'modern', 'classic', 'plain' ), 'layout' => array( 'narrow', 'wide' ), 'width' => array( 'narrow', 'wide', 'full' ), 'align' => array( 'left', 'center', 'right' ), ); } /** * Parts of the form a page can leave out. * * @return array */ function edonating_hideable_parts() { return array( 'title', 'impact', 'giftaid', 'fees', 'updates' ); } /** * The markup for one form. Used by the shortcode and the block. * * @param array $args form, mode (inline or button), label, color, design, font, layout, accent, hide, width, align. * @return string HTML. */ function edonating_render( $args ) { $form_id = isset( $args['form'] ) ? trim( (string) $args['form'] ) : ''; // Accept the form's address too (https://edonating.com/f/ID), not only the bare ID. if ( preg_match( '/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i', $form_id, $found ) ) { $form_id = $found[0]; } if ( ! edonating_is_form_id( $form_id ) ) { return current_user_can( 'edit_posts' ) ? '

' . esc_html__( 'edonating: add your form ID or its address. You can copy it from Forms in your edonating dashboard.', 'edonating' ) . '

' : ''; } wp_enqueue_script( 'edonating-embed' ); $is_button = isset( $args['mode'] ) && 'button' === $args['mode']; $label = isset( $args['label'] ) && '' !== trim( (string) $args['label'] ) ? mb_substr( sanitize_text_field( $args['label'] ), 0, 40 ) : __( 'Donate', 'edonating' ); $color = isset( $args['color'] ) ? sanitize_hex_color( $args['color'] ) : ''; $hosted = EDONATING_ORIGIN . '/f/' . rawurlencode( strtolower( $form_id ) ); $html = '
$allowed ) { $value = isset( $args[ $key ] ) ? (string) $args[ $key ] : ''; if ( in_array( $value, $allowed, true ) ) { $html .= ' data-' . $key . '="' . esc_attr( $value ) . '"'; } } $accent = isset( $args['accent'] ) ? sanitize_hex_color( $args['accent'] ) : ''; $html .= $accent ? ' data-accent="' . esc_attr( $accent ) . '"' : ''; $hide = isset( $args['hide'] ) ? $args['hide'] : array(); $hide = is_array( $hide ) ? $hide : explode( ',', (string) $hide ); $hide = array_values( array_intersect( array_map( 'trim', array_map( 'strval', $hide ) ), edonating_hideable_parts() ) ); $html .= $hide ? ' data-hide="' . esc_attr( implode( ',', $hide ) ) . '"' : ''; $html .= '>'; // Works without JavaScript too: a plain link to the hosted donation page. $html .= ''; $html .= '
'; return $html; } /** * Shortcode: [edonating form="FORM_ID"] or [edonating form="FORM_ID" mode="button" label="Give now" color="#1d1707"]. * Looks: design="soft|outline|bold" font="modern|classic|plain" layout="narrow|wide" accent="#2f6b4f" * hide="title,impact,giftaid,fees,updates" width="narrow|wide|full" align="left|center|right". * * @param array|string $atts Shortcode attributes. * @return string HTML. */ function edonating_shortcode( $atts ) { $atts = shortcode_atts( array( 'form' => '', 'mode' => 'inline', 'label' => '', 'color' => '', 'design' => '', 'font' => '', 'layout' => '', 'accent' => '', 'hide' => '', 'width' => '', 'align' => '', ), $atts, 'edonating' ); return edonating_render( $atts ); } add_shortcode( 'edonating', 'edonating_shortcode' ); /** * The "edonating form" block for the block editor. It's rendered on the server with the same markup as the shortcode. */ function edonating_register_block() { register_block_type( __DIR__ . '/block', array( 'render_callback' => 'edonating_render' ) ); // The editor shows a live preview of the form, from the same service as the page. wp_add_inline_script( generate_block_asset_handle( 'edonating/form', 'editorScript' ), 'window.edonatingOrigin = ' . wp_json_encode( EDONATING_ORIGIN ) . ';', 'before' ); } add_action( 'init', 'edonating_register_block' ); /** * A link from the Plugins screen to the edonating dashboard, where forms are made. * * @param array $links Existing links. * @return array */ function edonating_action_links( $links ) { $links[] = '' . esc_html__( 'Your forms', 'edonating' ) . ''; return $links; } add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'edonating_action_links' );