Skip to content

Instantly share code, notes, and snippets.

@danielpataki
Last active October 7, 2018 17:58
Show Gist options
  • Save danielpataki/cb790e16c4aae22f91e32407c4dcd9c7 to your computer and use it in GitHub Desktop.
Save danielpataki/cb790e16c4aae22f91e32407c4dcd9c7 to your computer and use it in GitHub Desktop.
OOP plugins
function __construct( $config ) {
$this->providers = $config['providers'];
$this->provider = $config['provider'];
// Actions and filters here
}
add_filter( 'msf/form_fields', 'age_field_extension' );
function age_field_extension( $fields ) {
$fields[] = '<input type="text" placeholder="Age" name="age">';
return $fields;
}
function __construct( $config ) {
$this->api_key = $config['api_key'];
add_filter( 'the_content', array( $this, 'form' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'assets' ) );
add_action( 'wp_ajax_msf_form_submit', array( $this, 'submissionHandler' ) );
add_action( 'wp_ajax_nopriv_msf_form_submit', array( $this, 'submissionHandler' ) );
}
function form( $content ) {
if( ! is_singular() ) {
return $content;
}
wp_enqueue_style( 'msf-style' );
$nonce_field = wp_nonce_field( 'msf_form_submit', 'msf_nonce', true, false );
if( !empty( $_GET['msf_success'] ) ) {
$msf_display = '<div class="msf-success">Thanks for subscribing</div>';
}
else {
$defaults = array(
'email' => '<input type="email" placeholder="Email" required="required" name="email">'
);
$msf_fields = apply_filters( 'msf/form_fields', $defaults );
$msf_display = '
<h4>Get Free Awesome!</h4>
<form method="post" class="msf-form" action="' . admin_url( 'admin-ajax.php' ) . '">
' . implode( "", $msf_fields ) . '
<input type="submit" value="Subscribe Now">
<input type="text" name="name">
<input type="hidden" name="action" value="msf_form_submit">
' . $nonce_field . '
</form>
';
}
return $content . $msf_display;
}
$config = array(
'provider' => 'mailchimp',
'providers' => array(
'mailchimp' => array(
'api_key' => 'cd64539dd19283cdcc637f2ccddcd45-us6'
),
'madmimi' => array(
'api_key' => 'JBJiwneJKNJBEhbfwbj2983hu43e'
)
)
);
<?php
/*
Plugin Name: My Subscription Form
Plugin URI: http://danielpataki.com
Description: An OOP based project for WPMU DEV
Author: Daniel Pataki
Author URI: http://danielpataki.com
Version: 1.0.0
*/
$config = array(
'api_key' => 'cd64539dd19283cdcc637f2ccddcd45-us6'
);
class MySubscriptionForm {
var $api_key;
function __construct( $config ) {
$this->api_key = $config['api_key'];
add_filter( 'the_content', array( $this, 'form' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'assets' ) );
add_action( 'wp_ajax_msf_form_submit', array( $this, 'submissionHandler' ) );
add_action( 'wp_ajax_nopriv_msf_form_submit', array( $this, 'submissionHandler' ) );
}
function form( $content ) {
if( ! is_singular() ) {
return $content;
}
wp_enqueue_style( 'msf-style' );
$nonce_field = wp_nonce_field( 'msf_form_submit', 'msf_nonce', true, false );
if( !empty( $_GET['msf_success'] ) ) {
$msf_display = '<div class="msf-success">Thanks for subscribing</div>';
}
else {
$msf_display = '
<h4>Get Free Awesome!</h4>
<form method="post" class="msf-form" action="' . admin_url( 'admin-ajax.php' ) . '">
<input type="email" required="required" name="email">
<input type="submit" value="Subscribe Now">
<input type="text" name="name">
<input type="hidden" name="action" value="msf_form_submit">
' . $nonce_field . '
</form>
';
}
return $content . $msf_display;
}
function assets() {
wp_register_style( 'msf-style', plugin_dir_url( __FILE__ ) . 'msf-styles.css' );
}
function submissionHandler() {
if ( !empty( $_POST['name'] ) || !isset( $_POST['msf_nonce'] ) || ! wp_verify_nonce( $_POST['msf_nonce'], 'msf_form_submit' )
) {
die();
return;
}
wp_remote_post('https://us6.api.mailchimp.com/3.0/lists/bbcd6546db/members', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'mywebsite' . ':' . $this->api_key ),
'Content-Type' => 'application/json'
),
'body' => json_encode( array(
'email_address' => $_POST['email'],
'status' => 'subscribed'
))
));
$url = add_query_arg( 'msf_success', 'true', $_SERVER['HTTP_REFERER'] );
wp_redirect( $url );
die();
}
}
new MySubscriptionForm( $config );
<?php
/*
Plugin Name: My Subscription Form
Plugin URI: http://danielpataki.com
Description: An OOP based project for WPMU DEV
Author: Daniel Pataki
Author URI: http://danielpataki.com
Version: 1.0.0
*/
add_filter( 'the_content', 'msf_post_form' );
function msf_post_form( $content ) {
if( ! is_singular() ) {
return $content;
}
wp_enqueue_style( 'msf-style' );
$nonce_field = wp_nonce_field( 'msf_form_submit', 'msf_nonce', true, false );
if( !empty( $_GET['msf_success'] ) ) {
$msf_display = '<div class="msf-success">Thanks for subscribing</div>';
}
else {
$msf_display = '
<h4>Get Free Awesome!</h4>
<form method="post" class="msf-form" action="' . admin_url( 'admin-ajax.php' ) . '">
<input type="email" required="required" name="email">
<input type="submit" value="Subscribe Now">
<input type="text" name="name">
<input type="hidden" name="action" value="msf_form_submit">
' . $nonce_field . '
</form>
';
}
return $content . $msf_display;
}
add_action( 'wp_enqueue_scripts', 'msf_assets' );
function msf_assets() {
wp_register_style( 'msf-style', plugin_dir_url( __FILE__ ) . 'msf-styles.css' );
}
add_action( 'wp_ajax_msf_form_submit', 'msf_form_submit' );
add_action( 'wp_ajax_nopriv_msf_form_submit', 'msf_form_submit' );
function msf_form_submit() {
if ( !empty( $_POST['name'] ) || !isset( $_POST['msf_nonce'] ) || ! wp_verify_nonce( $_POST['msf_nonce'], 'msf_form_submit' )
) {
die();
return;
}
wp_remote_post('https://us6.api.mailchimp.com/3.0/lists/bbcd6546db/members', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'mywebsite' . ':' . 'cd64539dd19283cdcc637f2ccddcd45-us6' ),
'Content-Type' => 'application/json'
),
'body' => json_encode( array(
'email_address' => $_POST['email'],
'status' => 'subscribed'
))
));
$url = add_query_arg( 'msf_success', 'true', $_SERVER['HTTP_REFERER'] );
wp_redirect( $url );
die();
}
<?php
/*
Plugin Name: My Subscription Form
Plugin URI: http://danielpataki.com
Description: An OOP based project for WPMU DEV
Author: Daniel Pataki
Author URI: http://danielpataki.com
Version: 1.1.0
*/
$config = array(
'provider' => 'mailchimp',
'providers' => array(
'mailchimp' => array(
'api_key' => 'bdbdbdb4b4b4b4bdbdb4b4b-us6'
),
'madmimi' => array(
'api_key' => 'wiefsbjkHJBwhlabHJbflh34bwf'
)
)
);
class MySubscriptionForm {
var $providers;
var $provider;
function __construct( $config ) {
$this->providers = $config['providers'];
$this->provider = $config['provider'];
add_filter( 'the_content', array( $this, 'form' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'assets' ) );
add_action( 'wp_ajax_msf_form_submit', array( $this, 'submissionHandler' ) );
add_action( 'wp_ajax_nopriv_msf_form_submit', array( $this, 'submissionHandler' ) );
}
function form( $content ) {
if( ! is_singular() ) {
return $content;
}
wp_enqueue_style( 'msf-style' );
$nonce_field = wp_nonce_field( 'msf_form_submit', 'msf_nonce', true, false );
if( !empty( $_GET['msf_success'] ) ) {
$msf_display = '<div class="msf-success">Thanks for subscribing</div>';
}
else {
$defaults = array(
'email' => '<input type="email" placeholder="Email" required="required" name="email">'
);
$msf_fields = apply_filters( 'msf/form_fields', $defaults );
$msf_display = '
<h4>Get Free Awesome!</h4>
<form method="post" class="msf-form" action="' . admin_url( 'admin-ajax.php' ) . '">
' . implode( "", $msf_fields ) . '
<input type="submit" value="Subscribe Now">
<input type="text" name="name">
<input type="hidden" name="action" value="msf_form_submit">
' . $nonce_field . '
</form>
';
}
return $content . $msf_display;
}
function assets() {
wp_register_style( 'msf-style', plugin_dir_url( __FILE__ ) . 'msf-styles.css' );
}
function mailchimpHandler() {
if ( !empty( $_POST['name'] ) || !isset( $_POST['msf_nonce'] ) || ! wp_verify_nonce( $_POST['msf_nonce'], 'msf_form_submit' )
) {
die();
return;
}
wp_remote_post('https://us6.api.mailchimp.com/3.0/lists/bdbbdbdbd/members', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'mywebsite' . ':' . $this->providers['mailchimp']['api_key'] ),
'Content-Type' => 'application/json'
),
'body' => json_encode( array(
'email_address' => $_POST['email'],
'status' => 'subscribed'
))
));
$url = add_query_arg( 'msf_success', 'true', $_SERVER['HTTP_REFERER'] );
wp_redirect( $url );
die();
}
function submissionHandler() {
if( method_exists($this, $this->provider . 'Handler' ) ) {
call_user_func( array( $this, $this->provider . 'Handler' ) );
}
else {
echo $this->provider . 'Handler does not exist' ;
}
}
}
new MySubscriptionForm( $config );
var $providers;
function __construct( $config ) {
$this->providers = $config['providers'];
// rest of the constructor
}
wp_remote_post('https://us6.api.mailchimp.com/3.0/lists/bbcd6546db/members', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'mywebsite' . ':' . $this->providers['mailchimp']['api_key'] ),
'Content-Type' => 'application/json'
),
'body' => json_encode( array(
'email_address' => $_POST['email'],
'status' => 'subscribed'
))
));
$config = array(
'api_key' => 'cd64539dd19283cdcc637f2ccddcd45-us6'
);
class MySubscriptionForm {
var $api_key;
function __constructor( $config ) {
$this->api_key = $config['api_key'];
}
}
new MySubscriptionForm( $config );
$config = array(
'providers' => array(
'mailchimp' => array(
'api_key' => 'cd64539dd19283cdcc637f2ccddcd45-us6'
),
'madmimi' => array(
'api_key' => 'JBJiwneJKNJBEhbfwbj2983hu43e'
)
)
);
.msf-form {
display:flex;
}
.msf-success {
background: #ABFFAC;
color: #60C661;
text-align: center;
width:100%;
padding:22px;
}
function submissionHandler() {
if( method_exists($this, $this->provider . 'Handler' ) ) {
call_user_func( array( $this, $this->provider . 'Handler' ) );
}
else {
echo $this->provider . 'Handler does not exist' ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment