Skip to content

Instantly share code, notes, and snippets.

@kovshenin
Created November 27, 2012 10:30
Show Gist options
  • Save kovshenin/4153525 to your computer and use it in GitHub Desktop.
Save kovshenin/4153525 to your computer and use it in GitHub Desktop.
<?php
/**
* Campaign Monitor Shortcode & Reversal
*/
/**
* Embed Reversal
*
* Looks form a <form> that looks like a Campaign Monitor subscribe
* form during pre_kses, and converts it into a shortcode if possible.
*
* @see wcorg_campaign_monitor_shortcode
*/
function wcorg_campaign_monitor_embed_to_shortcode( $content ) {
/*
<form action="http://username.createsend.com/t/t/s/sjlk/" method="post" id="subForm">
<div>
<label for="name">Name:</label><br /><input type="text" name="cm-name" id="name" /><br />
<label for="sjlk-sjlk">Email:</label><br /><input type="text" name="cm-sjlk-sjlk" id="sjlk-sjlk" /><br />
<input type="submit" value="Subscribe" />
</div>
</form>
*/
if ( false === stripos( $content, '<form' ) && false === stripos( $content, '.createsend.com' ) )
return $content;
$matches = array();
$regexp = '#<form(.+?)action=("|\')(https?://[^\.]+\.createsend\.com/[^/]+/[^/]+/[^/]+/[^/]+/)\2([^<]|[<](?!/form))+</form>#is';
if ( ! preg_match_all( $regexp, $content, $matches ) || ! isset( $matches[3] ) )
return $content;
// Loop through found forms and replace them with shortcodes.
foreach ( $matches[3] as $key => $url ) {
$shortcode = sprintf( '[campaign-monitor url="%s"]', esc_url( $url ) );
$content = str_replace( $matches[0][ $key ], $shortcode, $content );
}
return $content;
}
add_filter( 'pre_kses', 'wcorg_campaign_monitor_embed_to_shortcode' );
/**
* Shortcode Callback
*
* Results in a form with the appropriate attributes and fields.
* The only required attribute is the URL, which is the action URL
* in a Campaign Monitor form.
*
* Example: [campaign-monitor url="http://username.createsend.com/t/t/s/sjlk/"]
*/
function wcorg_campaign_monitor_shortcode( $attr ) {
$attr = shortcode_atts( array(
'url'=> '',
'subscribe_label' => 'Subscribe',
), $attr );
$attr['url'] = trailingslashit( esc_url_raw( $attr['url'] ) );
$parsed_url = parse_url( $attr['url'] );
if ( empty( $parsed_url['host'] ) || ! preg_match( '#\.createsend\.com$#i', $parsed_url['host'] ) )
return 'Invalid Campaign Monitor URL.';
$matches = array();
if ( ! preg_match( '#^/[^/]+/[^/]+/[^/]+/([^/]+)/$#i', $parsed_url['path'], $matches ) )
return 'Invalid Campaign Monitor URL.';
$list_id = $matches[1];
$email_name_attr = sprintf( 'cm-%s-%s', $list_id, $list_id );
ob_start();
?>
<form class="wcorg-campaign-monitor" action="<?php echo esc_url( $attr['url'] ); ?>" method="post">
<label for="wcorg-cm-name">Name:</label>
<input type="text" name="cm-name" id="wcorg-cm-name" />
<label for="wcorg-cm-email">Email:</label>
<input type="text" name="<?php echo esc_attr( $email_name_attr ); ?>" id="wcorg-cm-email" />
<input type="submit" value="<?php echo esc_attr( $attr['subscribe_label'] ); ?>" />
</form>
<?php
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
add_shortcode( 'campaign-monitor', 'wcorg_campaign_monitor_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment