Created
January 29, 2016 20:20
-
-
Save abhilash0001/1e8b65766c6c2468e42c to your computer and use it in GitHub Desktop.
Wordpress JSON API Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Controller name: Layouts & Components | |
Controller description: Custom layouts and components controller | |
*/ | |
class JSON_API_Custom_JSON_API_Controller { | |
// Get all components | |
public function get_components() { | |
global $json_api; | |
$index = 0; | |
$components = null; | |
$args = array( | |
'name' => $json_api->query->slug, | |
'post_type' => 'components', | |
'post_status' => 'publish', | |
'posts_per_page' => '-1' | |
); | |
$the_query = new WP_Query( $args ); | |
if ( $the_query->have_posts() ) { | |
while ( $the_query->have_posts() ) { | |
$the_query->the_post(); | |
$component = null; | |
$component->title = get_the_title(); | |
$component->about->description = get_field('description'); | |
$component->about->rules_description = get_field('rules_description'); | |
$component->about->component_vector = get_field('component_vector'); | |
$component->specs->length = get_field('length'); | |
$component->specs->width = get_field('width'); | |
$component->specs->height = get_field('height'); | |
$component->specs->weight = get_field('weight'); | |
$component->overlay_images = null; | |
$rows = get_field('views'); | |
if($rows){ | |
$overlay_index = 0; | |
foreach($rows as $row) { | |
$component->overlay_images[$overlay_index] = $row['view']; | |
$overlay_index++; | |
} | |
} | |
$component->requires_remote_control = get_field('requires_remote_control'); | |
$components[$index] = $component; | |
$index++; | |
} | |
} | |
return array( | |
'components' => $components | |
); | |
} | |
// Get all layouts | |
public function get_layouts() { | |
global $json_api; | |
$index = 0; | |
$layouts = null; | |
$args = array( | |
'name' => $json_api->query->slug, | |
'post_type' => 'layouts', | |
'post_status' => 'publish', | |
'posts_per_page' => '-1' | |
); | |
$the_query = new WP_Query( $args ); | |
if ( $the_query->have_posts() ) { | |
while ( $the_query->have_posts() ) { | |
$the_query->the_post(); | |
$layout = null; | |
$layout->title = get_the_title(); | |
$layout->layout_image = get_field('layout_image'); | |
$layout->description = get_field('description'); | |
$layouts[$index] = $layout; | |
$index++; | |
} | |
} | |
return array( | |
'layouts' => $layouts | |
); | |
} | |
public function emailer() { | |
global $json_api; | |
// Require the SwiftMailer | |
require_once('libs/swiftmailer/swift_required.php'); | |
//create a boundary string. It must be unique | |
//so we use the MD5 algorithm to generate a random hash | |
$random_hash = md5(date('r', time())); | |
// Grab Global Options | |
$contact_email = get_field('contact_email', 'option'); | |
$customer_subject = get_field('customer_subject', 'option'); | |
// Grab Form Params | |
$first_name = urldecode($json_api->query->fname); | |
$last_name = urldecode($json_api->query->lname); | |
$email = urldecode($json_api->query->email); | |
$phone = urldecode($json_api->query->phone); | |
$address = urldecode($json_api->query->address); | |
$city = urldecode($json_api->query->city); | |
$state = urldecode($json_api->query->state); | |
$zip = urldecode($json_api->query->zip); | |
$about = urldecode($json_api->query->about); | |
$comment = urldecode($json_api->query->comment); | |
// Base 64 String Image | |
// e.g. "data:image/png;base64,<string>"" | |
$image = urldecode($json_api->query->image); | |
// The message | |
$message .= "<html><body>"; | |
$message .= "<p>A new layout has been created by ".$first_name." ".$last_name.".</p>"; | |
$message .= "<table>"; | |
// Add the fields to the message | |
$message .= "<tr><td colspan='2'><b>Contact Information</b></td></tr>"; // title | |
$message .= "<tr> <td>First Name</td><td>".$first_name."</td></tr>"; | |
$message .= "<tr> <td>Last Name</td><td>".$last_name."</td></tr>"; | |
$message .= "<tr> <td>Email</td><td>".$email."</td></tr>"; | |
$message .= "<tr> <td>Phone</td><td>".$phone."</td></tr>"; | |
$message .= "<tr> <td>Address</td><td>".$address."</td></tr>"; | |
$message .= "<tr> <td>City</td><td>".$city."</td></tr>"; | |
$message .= "<tr> <td>State</td><td>".$state."</td></tr>"; | |
$message .= "<tr> <td>Zipcode</td><td>".$zip."</td></tr>"; | |
$message .= "<tr> <td>How did you hear about us?</td><td>".$about."</td></tr>"; | |
$message .= "<tr> <td>Comments/Questions</td><td>".$comment."</td></tr>"; | |
$message .= "<tr><td></td><td></td></tr>"; // empty to create space | |
// Additional Options | |
$options1 = urldecode($json_api->query->standard_silencer_models); | |
$options2 = urldecode($json_api->query->extended_silencer_models); | |
$options3 = urldecode($json_api->query->carriers); | |
$options4 = urldecode($json_api->query->scales); | |
if($options1 || $options2 || $options3 || $options4) $message .= "<tr><td colspan='2'><b>Addtional Options</b></td></tr>"; // title | |
if($options1) $message .= "<tr><td>Standard Silencer Models</td><td>".$options1."</td></tr>"; | |
if($options2) $message .= "<tr><td>Extended Silencer Models</td><td>".$options2."</td></tr>"; | |
if($options3) $message .= "<tr><td>Carriers</td><td>".$options3."</td></tr>"; | |
if($options4) $message .= "<tr><td>Scales</td><td>".$options4."</td></tr>"; | |
// Components In Layout | |
$componentsInLayout = urldecode($json_api->query>components_in_layout); | |
if($componentsInLayout){ | |
$message .= "<tr><td colspan='2'><b>Components In Layout</b></td></tr>"; // title | |
$message .= "<tr><td>Components</td><td>".$componentsInLayout."</td></tr>"; | |
} | |
// Add the image to the message | |
if($image) $message .= "<img src='".$image."'>"; | |
// Close Message | |
$message .= "</table>"; | |
$message .= "</body></html>"; | |
// Create Customer Message | |
$customer_message = get_field('customer_message', 'option'); | |
// Attach File to Customer Message | |
$customer_file_path = get_field('customer_attachment', 'option'); | |
// SEND EMAILS via Swift | |
$transport = Swift_MailTransport::newInstance(); | |
$mailer = Swift_Mailer::newInstance($transport); | |
// -- Client Email | |
if($contact_email){ | |
$clientEmail = Swift_Message::newInstance('Layout Customization System') | |
->setFrom(array($contact_email => 'Moly Support')) | |
->setTo( array($contact_email => 'Moly Support') ) | |
->setBody($message, 'text/html'); | |
$clientEmailResult = $mailer->send($clientEmail); | |
} | |
if($customer_subject && $contact_email && $first_name && $last_name){ | |
// -- Customer Email | |
$customerEmail = Swift_Message::newInstance($customer_subject) | |
->setFrom(array($contact_email => 'Moly Support')) | |
->setTo(array($email => "".$first_name." ".$last_name)) | |
->setBody($customer_message, 'text/html'); | |
$customerEmail->attach(Swift_Attachment::fromPath($customer_file_path)); | |
$customerEmailResult = $mailer->send($customerEmail); | |
} | |
return array( | |
'contact_email' => $contact_email, | |
'emailed_from' => $email, | |
'message' => $message | |
); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment