Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active April 2, 2017 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shelob9/d30999bd14c716d596947f031b36f664 to your computer and use it in GitHub Desktop.
Save Shelob9/d30999bd14c716d596947f031b36f664 to your computer and use it in GitHub Desktop.
<?php
/**
* Example Caldera Forms Processor Class
*/
class CF_Apex_Processor extends Caldera_Forms_Processor_Processor {
/**
* Pre-process callback
*
* @param array $config Processor config
* @param array $form Form config
* @param string $proccesid Process ID
*
* @return array|null
*/
public function pre_processor( array $config, array $form, $proccesid ){
//Setup $this->data_object
$this->set_data_object_initial( $config, $form );
//Get errors and return if there are any
//At this point errors would be beacuse of missing requirments
$errors = $this->data_object->get_errors();
if ( ! empty( $errors ) ) {
return $errors;
}
//Get all processor field values as an array
$values = $this->data_object->get_values();
//Get one field's values
//First argument is processor field ID
//Second argument is default. This is optional. The default default is null.
$customer_id = $this->data_object->get_value( 'customer_id', 42 );
//You can add errors
$this->data_object->add_error( __( 'This is sub apex', 'text-domain' ) );
//Before ending this method, store the processor data in the transient
$this->setup_transata( $proccesid );
//Don't return anything if there are no errors!
}
/**
* Process callback
*
* @param array $config Processor config
* @param array $form Form config
* @param string $proccesid Process ID
*
* @return array
*/
public function processor( array $config, array $form, $proccesid ){
// TODO: Implement processor() method.
// NOTE: Doing anything with this method is totally optional, but you must define this method.
}
}
<?php
/**
* Example Caldera Forms mailing list processor
*/
class CF_MailingList_Processor extends Caldera_Forms_Processor_Newsletter {
/**
* Pre-process callback
*
* @param array $config Processor config
* @param array $form Form config
* @param string $proccesid Process ID
*
* @return array|null
*/
public function pre_processor( array $config, array $form, $proccesid ){
//Setup $this->data_object
$this->set_data_object_initial( $config, $form );
//Get errors and return if there are any
$errors = $this->data_object->get_errors();
if ( ! empty( $errors ) ) {
return $errors;
}
$result = $this->subscribe( array(
'email' => $this->data_object->get_value( 'email' ),
), $this->data_object->get_value( 'list' ) );
//all is good, record and return nothing
if( true === $result ){
//Before ending this method, store the processor data in the transient
$this->setup_transata( $proccesid );
return;
}
//set and return errors
$this->data_object->add_error( $result );
return $this->data_object->get_errors();
}
/**
* Add the newsletter client here
*
* @param $client
*/
public function set_client( $client ){
$this->client = $client;
}
public function subscribe( array $subscriber_data, $list_name ){
//This is super-psuedocode :)
return $this->client->subscribe( $subscriber_data, $list_name );
}
/**
* Process callback
*
* @param array $config Processor config
* @param array $form Form config
* @param string $proccesid Process ID
*
* @return array
*/
public function processor( array $config, array $form, $proccesid ){
// TODO: Implement processor() method.
// NOTE: Doing anything with this method is totally optional, but you must define this method.
}
}
<?php
/**
* Example Caldera Forms Payment Processor
*/
class CF_Pay_Processor extends Caldera_Forms_Processor_Payment {
/**
* Pre-process callback
*
* @param array $config Processor config
* @param array $form Form config
* @param string $proccesid Process ID
*
* @return array|null
*/
public function pre_processor( array $config, array $form, $proccesid ){
//Setup $this->data_object
$this->set_data_object_initial( $config, $form );
//Get errors and return if there are any
$errors = $this->data_object->get_errors();
if ( ! empty( $errors ) ) {
return $errors;
}
//Input is valid, make payment
//BTW I know it's silly that you have to pass arguments to this method
$result = $this->do_payment( $config, $form, $proccesid, $this->data_object );
//check for errors
if( true !== $result ){
if( is_string( $result ) ){
$this->data_object->add_error( $result );
}else{
$this->data_object->add_error( __( 'Unknown error', 'text-domain') );
}
}
//Get errors and return if there are any
$errors = $this->data_object->get_errors();
if ( ! empty( $errors ) ) {
return $errors;
}
//Before ending this method, store the processor data in the transient
$this->setup_transata( $proccesid );
//Don't return anything if there are no errors!
}
/**
* Do the payment
*
* @param array $config Processor config
* @param array $form Form config
* @param string $proccesid Process ID
* @param Caldera_Forms_Processor_Get_Data $data_object
*
* @return string|bool
*/
public function do_payment( array $config, array $form, $proccesid, Caldera_Forms_Processor_Get_Data $data_object ){
$api_key = $this->data_object->get_value( 'api_key' );
$amount = $this->data_object->get_value( 'amount' );
$response = wp_remote_post( 'https://apexpay.com/api/payment', array(
'body' => array(
'key' => $api_key,
'ammount' => $amount
)
));
if( ! is_wp_error( $response ) && 200 == wp_remote_retrieve_response_code( $response ) ){
return true;
}elseif ( is_wp_error( $response ) ){
return $response->get_error_message();
}elseif ( is_array( $response ) && isset( $response[ 'error' ] ) ){
return $response[ 'error' ];
}else{
return false;
}
}
/**
* Process callback
*
* @param array $config Processor config
* @param array $form Form config
* @param string $proccesid Process ID
*
* @return array
*/
public function processor( array $config, array $form, $proccesid ){
//Get transient data back
$this->setup_transata( $proccesid );
$this->set_data_object_from_transdata( $proccesid );
//save meta data in submission
global $transdata;
if ( ! isset( $transdata[ $proccesid ][ 'meta' ] ) ) {
$transdata[ $proccesid ][ 'meta' ] = array();
}
$transdata[ $proccesid ][ 'meta' ][ 'amount' ] = $this->data_object->get_value( 'amount' );
return $transdata[ $proccesid ][ 'meta' ];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment