• Resolved Jonas Lundman

    (@intervik)


    Hi

    Great plugin. We left CF7, Grav, any many other years ago when this AF plugin arrived as we prefer ACF as engine to handle as much as we can instead of thousends API.

    There are plenty of filter for the email variable, still we missing one for many years, and each time we update the plugin we have to add the filter again.

    We extended the plugin with the option “Send as plain text”. In order to send some notifications in plain text, and others as default html, we must be able to filter the html output.

    It would be great if you added 4 lines as below:

    
    
    		
    		// Construct email HTML
    		$html = sprintf(
    			'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    			<html xmlns="https://www.w3.org/1999/xhtml">
    				<head>
    					<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    					<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    					<title>%s</title>
    					<style>%s</style>
    				</head>
    				<body>
    					%s
    				</body>
    			</html>',
    			$subject,
    			$style,
    			$content
    		);
    
    		// CHANGES START:
    		// Filter each html - needed
    		$html = apply_filters( 'af/form/email/html', $html, $email, $form, $fields, $subject, $style, $content);
    		$html = apply_filters( 'af/form/email/html/id=' . $form['post_id'], $html, $email, $form, $fields, $subject, $style, $content );
    		$html = apply_filters( 'af/form/email/html/key=' . $form['key'], $html, $email, $form, $fields, $subject, $style, $content );
    		
    		// CHANGES END
    

    We alse extended the plugin to use Askimet to validate any email field, added “Reply-to” field, custom column information from the form on saved entries, and more.

    Happy to email the whole php file, or screenshots if you wanna use some ideas.

    / Intervik Development Team

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author fabianlindfors

    (@fabianlindfors)

    Hej, Jonas!

    I’ve been considering adding a filter to disable the default template. Would that work for you?

    Sorry about the late reply by the way!

    Thread Starter Jonas Lundman

    (@intervik)

    Hi

    I dont think so, as we need the placeholders from the templates, and we also send html to customers, but only plain to admins. The conditionals are therefor done by the filters for each mail as in core-emails.php, but the header output has no filter. Adding this works for us:

    		// Filter each html - needed
    		$html = apply_filters( 'af/form/email/html', $html, $email, $form, $fields, $subject, $style, $content);
    		$html = apply_filters( 'af/form/email/html/id=' . $form['post_id'], $html, $email, $form, $fields, $subject, $style, $content );
    		$html = apply_filters( 'af/form/email/html/key=' . $form['key'], $html, $email, $form, $fields, $subject, $style, $content );

    As a start… But a checkbox “send as plain” for the notification email setup would be great in core version, but There are more functions in your plugin that needs to be fixed/ filtered, so that may be big step for you to do.

    / INtervik

    Plugin Author fabianlindfors

    (@fabianlindfors)

    The reason I don’t want to include that filter is that the exposed API wouldn’t be very nice. Mostly because the current template uses string formatting to place the CSS and content. This means that developers overriding the template would have to include %s in their templates and also be aware of the ordering. For example if don’t want to use the default CSS they would have to do: %2$s.

    My idea would instead be to expose a simple true/false filter to disable the default template, as I mentioned before. For people who want to use their own templates they can then use af/form/email/content to wrap the email contents in their own template.

    Regarding your concern about differing between admin and regular users, the new filter should still have the email info making it possible to turn off the templates only for some emails.

    Regarding the filters you have introduced, what part of the default template are you changing and how are you doing it? I’m sure we should be able to work something out here as well.

    I hope I’m making sense. If you have suggestions for other actions and filters I’d love to hear them as well!

    Thread Starter Jonas Lundman

    (@intervik)

    The “exposed API wouldn’t be very nice” – true – but the reason is an architecture question, and the method in use, is no good, with placeholders and function calls inside the sprintf. You also getting a lot of tabbed spaces inside the email headers, as the “template” becomes one string from the protocol core-emails.php itself.

    Well/ And, any filter and developers in WordPress can doing it wrong, so it is up to developers to make shure they doing the %s ordering correct.

    Try your idea of a “filter out the whole template”, and see what comes out of that.

    Case study:

    In our case, we cant enter the final html without suggested filter. We need to get rid of everything, and replace it with the $email['content'] only, but only if our added conditional if(!$email['plain']) return $html; is met.

    As we added an extra checkbox to array ('key' => 'field_form_email_plain', ....... INSIDE FILTER af/form/notification_settings_fields

    As the validation on submitted form is done “twice” – first by ACF default ajax API and second by AF actions (and filters), Sending plain text requires more cleanup then expected. When $_POST[‘acf’] is returning to the AF API, there is already a lot of wpautop() and dirt added to the textarea field. There is actually dubbled <p><p> sometimes in the chain but clears out in the end.

    From our developers/ notes / example:

    /* Sanitize outgoing email for textareas */
    /* Used by '_af_render_field_include()' by api-helpers.php */
    /* Replaces the global $output = htmlspecialchars( $rendered_value ); */
    /* Otherwise any html tags will be visible in the messages */
    /* Remark: uses 'strip_tags_content()' PHP extension: */
    /* https://php.net/manual/en/function.strip-tags.php */
    
    function advanced_form_validate_textarea($output, $field, $value){
    	if(!$value) return $value;
    	if($field['type'] == 'textarea') return trim(wp_kses_post(wpautop(strip_tags(strip_tags_content($value, '<p>')))));
    		else return $output;
    }
    add_filter('af/field/render_include', 'advanced_form_validate_textarea', 10, 3 );

    Other:

    
    /* remove hardcoded 'the_content' filter */
    /* some themes adding front end stuff and we don want them in our emails */
    function advanced_form_email_clean_content($content, $email, $form, $fields){
    	return $email['content'];
    }
    add_filter('af/form/email/content', 'advanced_form_email_clean_content', 10, 4);

    And more…

    So adding a filter to remove the default template string, please provide a lot of arguments from the original func, so we can do stuff…

    Thread Starter Jonas Lundman

    (@intervik)

    So as we speak as conclusion, I suggest you try your idea of custom template filter, and we can try to adapt our needs from there.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Suggest to a filter’ is closed to new replies.