first commit

This commit is contained in:
Roman Pyrih
2023-07-24 08:30:51 +02:00
commit c2e100a763
7128 changed files with 1622619 additions and 0 deletions

View File

@@ -0,0 +1,208 @@
<?php
abstract class Brizy_Editor_Forms_AbstractIntegration extends Brizy_Admin_Serializable {
/**
* @var string
*/
protected $id;
/**
* @var bool
*/
protected $completed;
/**
* @var WP_Error
*/
protected $exception;
/**
* Brizy_Editor_Forms_AbstractIntegration constructor.
*
* @param $id
*/
public function __construct( $id ) {
$this->id = $id;
}
/**
* @param Brizy_Editor_Forms_Form $form
* @param $fields
*
* @return mixed
*/
abstract public function handleSubmit( Brizy_Editor_Forms_Form $form, $fields );
public function handleFailToSendMessage( WP_Error $error ) {
$this->exception = $error;
}
/**
* @return bool
*/
public function isCompleted() {
return $this->completed;
}
/**
* @param bool $completed
*
* @return Brizy_Editor_Forms_AbstractIntegration
*/
public function setCompleted( $completed ) {
$this->completed = $completed;
return $this;
}
/**
* @return string
*/
public function serialize() {
return serialize( $this->jsonSerialize() );
}
/**
* @return array|mixed
*/
public function jsonSerialize() {
$get_object_vars = array(
'id' => $this->getId(),
'completed' => $this->isCompleted(),
);
return $get_object_vars;
}
/**
* @param $json_obj
*
* @return Brizy_Editor_Forms_AbstractIntegration
* @throws Exception
*/
public static function createInstanceFromJson( $json_obj ) {
$instance = null;
if ( is_object( $json_obj ) ) {
switch ( $json_obj->id ) {
case 'wordpress':
$instance = Brizy_Editor_Forms_WordpressIntegration::createFromJson( $json_obj );
break;
case 'smtp':
if ( class_exists( 'Brizy_Editor_Forms_SmtpIntegration' ) ) {
$instance = Brizy_Editor_Forms_SmtpIntegration::createFromJson( $json_obj );
}
break;
case 'gmail_smtp':
if ( class_exists( 'Brizy_Editor_Forms_GmailSmtpIntegration' ) ) {
$instance = Brizy_Editor_Forms_GmailSmtpIntegration::createFromJson( $json_obj );
}
break;
default:
if ( class_exists( 'Brizy_Editor_Forms_ServiceIntegration' ) ) {
$instance = Brizy_Editor_Forms_ServiceIntegration::createFromJson( $json_obj );
}
break;
}
if ( $instance ) {
$instance->setId( $json_obj->id );
if ( isset( $json_obj->completed ) ) {
$instance->setCompleted( $json_obj->completed );
}
}
add_action( 'wp_mail_failed', array( $instance, 'handleFailToSendMessage' ) );
}
return $instance;
}
/**
* @param $data
*
* @param null $instance
*
* @return Brizy_Editor_Forms_ServiceIntegration|Brizy_Editor_Forms_WordpressIntegration|void|null
*/
public static function createFromSerializedData( $data, $instance = null ) {
if ( $data instanceof Brizy_Editor_Forms_WordpressIntegration ||
$data instanceof Brizy_Editor_Forms_ServiceIntegration ) {
return $data;
}
if ( is_array( $data ) ) {
switch ( $data['id'] ) {
case 'wordpress':
$instance = Brizy_Editor_Forms_WordpressIntegration::createFromSerializedData( $data, $instance );
break;
case 'smtp':
if ( class_exists( 'Brizy_Editor_Forms_SmtpIntegration' ) ) {
$instance = Brizy_Editor_Forms_SmtpIntegration::createFromSerializedData( $data );
}
break;
case 'gmail_smtp':
if ( class_exists( 'Brizy_Editor_Forms_GmailSmtpIntegration' ) ) {
$instance = Brizy_Editor_Forms_GmailSmtpIntegration::createFromSerializedData( $data );
}
break;
default:
if ( class_exists( 'Brizy_Editor_Forms_ServiceIntegration' ) ) {
$instance = Brizy_Editor_Forms_ServiceIntegration::createFromSerializedData( $data );
}
break;
}
if ( $instance ) {
$instance->setId( $data['id'] );
$instance->setCompleted( $data['completed'] );
}
}
return $instance;
}
/**
* @return array|mixed
*/
public function convertToOptionValue() {
return $this->jsonSerialize();
}
/**
* @return string
*/
public function getId() {
return $this->id;
}
/**
* @param string $id
*
* @return Brizy_Editor_Forms_AbstractIntegration
*/
public function setId( $id ) {
$this->id = $id;
return $this;
}
/**
* @return WP_Error
*/
public function getException() {
return $this->exception;
}
public function __destruct() {
remove_action( 'wp_mail_failed', array( $this, 'handleFailToSendMessage' ) );
}
}

View File

@@ -0,0 +1,449 @@
<?php
/**
* @todo: Implement Brizy_Admin_AbstractApi here
*
* Class Brizy_Editor_Forms_Api
*/
class Brizy_Editor_Forms_Api {
const AJAX_GET_FORM = '_get_form';
const AJAX_CREATE_FORM = '_create_form';
const AJAX_UPDATE_FORM = '_update_form';
const AJAX_DELETE_FORM = '_delete_form';
const AJAX_SUBMIT_FORM = '_submit_form';
const AJAX_GET_INTEGRATION = '_get_integration';
const AJAX_CREATE_INTEGRATION = '_create_integration';
const AJAX_UPDATE_INTEGRATION = '_update_integration';
const AJAX_DELETE_INTEGRATION = '_delete_integration';
const AJAX_SET_RECAPTCHA_ACCOUNT = '_set_recaptcha_account';
const AJAX_GET_RECAPTCHA_ACCOUNT = '_get_recaptcha_account';
const AJAX_DELETE_RECAPTCHA_ACCOUNT = '_delete_recaptcha_account';
const AJAX_VALIDATE_RECAPTCHA_ACCOUNT = '_validate_recaptcha_account';
const AJAX_AUTHENTICATION_CALLBACK = '_authentication_callback';
/**
* @var Brizy_Editor_Post
*/
private $post;
/**
* @return Brizy_Editor_Post
*/
public function get_post() {
return $this->post;
}
/**
* Brizy_Editor_API constructor.
*
* @param Brizy_Editor_Post $post
*/
public function __construct( $post ) {
$this->post = $post;
$this->initialize();
}
private function initialize() {
$pref = 'wp_ajax_' . Brizy_Editor::prefix();
$pref_nopriv = 'wp_ajax_nopriv_' . Brizy_Editor::prefix();
if ( Brizy_Editor_User::is_user_allowed() ) {
add_action( $pref . self::AJAX_GET_FORM, array( $this, 'get_form' ) );
add_action( $pref . self::AJAX_CREATE_FORM, array( $this, 'create_form' ) );
add_action( $pref . self::AJAX_UPDATE_FORM, array( $this, 'update_form' ) );
add_action( $pref . self::AJAX_DELETE_FORM, array( $this, 'delete_form' ) );
add_action( $pref . self::AJAX_CREATE_INTEGRATION, array( $this, 'createIntegration' ) );
add_action( $pref . self::AJAX_GET_INTEGRATION, array( $this, 'getIntegration' ) );
add_action( $pref . self::AJAX_UPDATE_INTEGRATION, array( $this, 'updateIntegration' ) );
add_action( $pref . self::AJAX_DELETE_INTEGRATION, array( $this, 'deleteIntegration' ) );
}
add_filter( 'brizy_form_submit_data', array( $this, 'handleFileTypeFields' ), - 100, 2 );
add_action( $pref . self::AJAX_SUBMIT_FORM, array( $this, 'submit_form' ) );
add_action( $pref_nopriv . self::AJAX_SUBMIT_FORM, array( $this, 'submit_form' ) );
}
protected function error( $code, $message ) {
wp_send_json_error( array( 'code' => $code, 'message' => $message ), 200 );
}
protected function success( $data, $code = 200 ) {
wp_send_json_success( $data, $code );
}
private function authorize() {
if ( ! wp_verify_nonce( $_REQUEST['hash'], Brizy_Editor_API::nonce ) ) {
wp_send_json_error( array( 'code' => 400, 'message' => 'Bad request' ), 400 );
}
}
public function get_form() {
try {
$this->authorize();
$manager = new Brizy_Editor_Forms_FormManager( Brizy_Editor_Project::get() );
$form = $manager->getForm( $_REQUEST['formId'] );
if ( $form ) {
$this->success( $form );
}
$this->error( 404, 'Form not found' );
} catch ( Exception $exception ) {
Brizy_Logger::instance()->critical( $exception->getMessage(), array( $exception ) );
$this->error( $exception->getCode(), $exception->getMessage() );
exit;
}
}
public function create_form() {
try {
$this->authorize();
$manager = new Brizy_Editor_Forms_FormManager( Brizy_Editor_Project::get() );
$instance = Brizy_Editor_Forms_Form::createFromJson( json_decode( file_get_contents( 'php://input' ) ) );
$validation_result = $instance->validate();
if ( $validation_result === true ) {
$manager->addForm( $instance );
$this->success( $instance, 201 );
}
$this->error( 400, $validation_result );
} catch ( Exception $exception ) {
Brizy_Logger::instance()->critical( $exception->getMessage(), array( $exception ) );
$this->error( $exception->getCode(), $exception->getMessage() );
exit;
}
}
public function update_form() {
try {
$this->authorize();
$manager = new Brizy_Editor_Forms_FormManager( Brizy_Editor_Project::get() );
$form_json = json_decode( file_get_contents( 'php://input' ) );
if ( ! $form_json ) {
$this->error( 400, 'Invalid json object provided' );
}
if ( ! isset( $_REQUEST['formId'] ) ) {
$this->error( 400, 'Invalid form id provided' );
}
$form = $manager->getForm( $_REQUEST['formId'] );
if ( ! $form ) {
$this->error( 404, 'Form not found' );
}
$instance = Brizy_Editor_Forms_Form::updateFromJson( $form, $form_json );
$validation_result = $instance->validate();
if ( $validation_result === true ) {
$manager->addForm( $instance );
$this->success( $instance, 200 );
}
$this->error( 400, $validation_result );
} catch ( Exception $exception ) {
Brizy_Logger::instance()->critical( $exception->getMessage(), array( $exception ) );
$this->error( $exception->getCode(), $exception->getMessage() );
exit;
}
}
public function delete_form() {
try {
$this->authorize();
$manager = new Brizy_Editor_Forms_FormManager( Brizy_Editor_Project::get() );
$manager->deleteFormById( $_REQUEST['formId'] );
$this->success( array() );
} catch ( Exception $exception ) {
Brizy_Logger::instance()->critical( $exception->getMessage(), array( $exception ) );
$this->error( $exception->getCode(), $exception->getMessage() );
exit;
}
}
public function submit_form() {
try {
$manager = new Brizy_Editor_Forms_FormManager( Brizy_Editor_Project::get() );
/**
* @var Brizy_Editor_Forms_Form $form ;
*/
$form = $manager->getForm( isset($_REQUEST['form_id'])?$_REQUEST['form_id']:null );
if ( ! $form ) {
$this->error( 404, "Form not found" );
}
$fields = json_decode( stripslashes( $_REQUEST['data'] ) );
if ( ! $fields ) {
$this->error( 400, "Invalid form data" );
}
// validtate recaptha response if exists
$accountManager = new Brizy_Editor_Accounts_ServiceAccountManager( Brizy_Editor_Project::get() );
$recaptchaAccounts = $accountManager->getAccountsByGroup( Brizy_Editor_Accounts_AbstractAccount::RECAPTCHA_GROUP );
if ( count( $recaptchaAccounts ) > 0 ) {
$recaptchaField = null;
foreach ( $fields as $field ) {
if ( $field->name == 'g-recaptcha-response' ) {
$recaptchaField = $field;
}
}
if ( ! $recaptchaField ) {
Brizy_Logger::instance()->error( "The submitted data is invalid." );
$this->error( 400, "The submitted data is invalid." );
}
$recaptchaAccount = $recaptchaAccounts[0];
$http = new WP_Http();
$array = array(
'secret' => $recaptchaAccount->getSecretKey(),
'response' => $recaptchaField->value
);
$response = $http->post( 'https://www.google.com/recaptcha/api/siteverify', array(
'body' => $array
)
);
$body = wp_remote_retrieve_body( $response );
$responseJsonObject = json_decode( $body );
if ( ! is_object( $responseJsonObject ) || ! $responseJsonObject->success ) {
$this->error( 400, "Unable to validation request" );
}
}
$form = apply_filters( 'brizy_form', $form );
$fields = apply_filters( 'brizy_form_submit_data', $fields, $form );
$result = null;
foreach ( $form->getIntegrations() as $integration ) {
if ( ! $integration->isCompleted() ) {
continue;
}
try {
$result = $integration->handleSubmit( $form, $fields );
} catch ( Exception $e ) {
Brizy_Logger::instance()->exception( $e );
$this->error( 500, 'Member was not created.' );
} finally {
if ( ! $result && $integration->getException() ) {
throw new Exception( $integration->getException()->get_error_message() );
}
}
}
$this->success( 200 );
} catch ( Exception $exception ) {
Brizy_Logger::instance()->critical( $exception->getMessage(), array( $exception ) );
$this->error( 400, $exception->getMessage() );
exit;
}
}
public function handleFileTypeFields( $fields, $form ) {
foreach ( $fields as $field ) {
if ( $field->type == 'FileUpload' ) {
$uFile = $_FILES[ $field->name ];
foreach ( $_FILES[ $field->name ]['name'] as $index => $value ) {
$uFile = array(
'name' => $_FILES[ $field->name ]['name'][ $index ],
'type' => $_FILES[ $field->name ]['type'][ $index ],
'tmp_name' => $_FILES[ $field->name ]['tmp_name'][ $index ],
'error' => $_FILES[ $field->name ]['error'][ $index ],
'size' => $_FILES[ $field->name ]['size'][ $index ]
);
$uploadOverrides = array( 'test_form' => false );
$file = wp_handle_upload( $uFile, $uploadOverrides );
if ( ! $file || isset( $file['error'] ) ) {
Brizy_Logger::instance()->error( 'Failed to handle upload', $fields );
throw new Exception( 'Failed to handle upload' );
}
// create attachment
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $file['file'] ),
'post_mime_type' => $file['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file['file'] ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file['file'] );
if ( $attach_id instanceof WP_Error ) {
Brizy_Logger::instance()->critical( 'Failed to handle upload', array( $attach_id ) );
throw new Exception( 'Failed to handle upload' );
}
update_post_meta( $attach_id, 'brizy-form-upload', 1 );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $file['file'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
$field->value = $file['url'];
}
}
}
return $fields;
}
public function createIntegration() {
$this->authorize();
$manager = new Brizy_Editor_Forms_FormManager( Brizy_Editor_Project::get() );
$form = $manager->getForm( $_REQUEST['formId'] );
if ( ! $form ) {
$this->error( 400, "Invalid form id" );
}
try {
$integration = Brizy_Editor_Forms_AbstractIntegration::createInstanceFromJson( json_decode( file_get_contents( 'php://input' ) ) );
if ( $form->getIntegration( $integration->getid() ) ) {
$this->error( 400, "This integration is already created" );
}
$integration = apply_filters( 'brizy_create_integration', $integration, $form );
$integration = apply_filters( 'brizy_prepare_integration', $integration, $form );
$integration = apply_filters( 'brizy_add_integration_accounts', $integration, $form );
if ( $form->addIntegration( $integration ) ) {
$manager->addForm( $form );
$this->success( $integration );
}
} catch ( Exception $exception ) {
Brizy_Logger::instance()->critical( $exception->getMessage(), array( $exception ) );
$this->error( 400, $exception->getMessage() );
}
$this->error( 500, "Unable to create integration" );
}
public function getIntegration() {
$this->authorize();
$manager = new Brizy_Editor_Forms_FormManager( Brizy_Editor_Project::get() );
$form = $manager->getForm( $_REQUEST['formId'] );
if ( ! $form ) {
$this->error( 400, "Invalid form id" );
}
$integrationId = $_REQUEST['integration'];
if ( ! $integrationId ) {
$this->error( 400, "Invalid form integration" );
}
$integration = $form->getIntegration( $integrationId );
if ( $integration ) {
$integration = apply_filters( 'brizy_add_integration_accounts', $integration, $form );
$integration = apply_filters( 'brizy_prepare_integration', $integration, $form );
$this->success( $integration );
}
$this->error( 404, 'Integration not found' );
}
public function updateIntegration() {
$this->authorize();
$manager = new Brizy_Editor_Forms_FormManager( Brizy_Editor_Project::get() );
$form = $manager->getForm( $_REQUEST['formId'] );
if ( ! $form ) {
$this->error( 400, "Invalid form id" );
}
$integration = Brizy_Editor_Forms_AbstractIntegration::createInstanceFromJson( json_decode( file_get_contents( 'php://input' ) ) );
$integration = apply_filters( 'brizy_update_integration', $integration, $form );
$integration = apply_filters( 'brizy_prepare_integration', $integration, $form );
//------------------
if ( $integration && $form->updateIntegration( $integration ) ) {
$manager->addForm( $form );
$this->success( $integration );
}
$this->error( 404, 'Integration not found' );
}
public function deleteIntegration() {
$this->authorize();
$manager = new Brizy_Editor_Forms_FormManager( Brizy_Editor_Project::get() );
$form = $manager->getForm( $_REQUEST['formId'] );
if ( ! $form ) {
$this->error( 400, "Invalid form id" );
}
$integrationId = $_REQUEST['integration'];
if ( ! $integrationId ) {
$this->error( 400, "Invalid form integration" );
}
$integrationId = apply_filters( 'brizy_update_integration', $integrationId, $form );
$deleted = false;
if ( $integrationId ) {
$deleted = $form->deleteIntegration( $integrationId );
}
if ( $deleted ) {
$manager->addForm( $form );
$this->success( null );
}
$this->error( 404, 'Integration not found' );
}
}

View File

@@ -0,0 +1,67 @@
<?php
trait Brizy_Editor_Forms_DynamicPropsAware {
/**
* @var string[]
*/
protected $data;
/**
* @param $name
* @param $arguments
*
* @return mixed|null
* @throws Exception
*/
public function __call( $name, $arguments ) {
$method = substr( $name, 0, 3 );
$key = substr( $name, 3 );
if ( empty( $key ) ) {
throw new Exception( 'Invalid key. You method must look like this: setKey();' );
}
switch ( $method ) {
case 'set':
return $this->set( strtolower( $key ), $arguments[0] );
break;
case 'get':
return $this->get( strtolower( $key ) );
break;
}
}
/**
* @param $name
*
* @return null|mixed
*/
protected function get( $name ) {
if ( is_null( $name ) ) {
return;
}
if ( isset( $this->data[ $name ] ) ) {
return $this->data[ $name ];
}
return null;
}
/**
* @param $key
* @param $value
*
* @return null|mixed
*/
protected function set( $key, $value ) {
if ( is_null( $value ) ) {
return null;
}
return $this->data[ $key ] = $value;
}
}

View File

@@ -0,0 +1,72 @@
<?php
class Brizy_Editor_Forms_Field extends Brizy_Admin_Serializable {
use Brizy_Editor_Forms_DynamicPropsAware;
/**
* Brizy_Editor_Forms_Field constructor.
*
* @param \BrizyForms\Model\Field|null $field
*/
public function __construct( \BrizyForms\Model\Field $field = null ) {
$this->data = array();
if ( $field ) {
$this->data = array( 'slug' => $field->getSlug(),
'name' => $field->getName(),
'required' => $field->getRequired()
);
}
}
/**
* @return string
*/
public function serialize() {
return serialize( $this->jsonSerialize() );
}
public function unserialize( $serialized ) {
$this->data = unserialize( $serialized );
}
public function jsonSerialize() {
return $this->data;
}
public function convertToOptionValue() {
return $this->data;
}
static public function createFromSerializedData( $data ) {
$instance = new self();
foreach ( $data as $key => $val ) {
$instance->set( $key, $val );
}
return $instance;
}
/**
* @param $json_obj
*
* @return Brizy_Editor_Forms_Field|void
* @throws Exception
*/
public static function createFromJson( $json_obj ) {
if ( ! isset( $json_obj ) ) {
throw new Exception( 'Bad Request', 400 );
}
if ( is_object( $json_obj ) ) {
return self::createFromSerializedData( get_object_vars( $json_obj ) );
}
return new self();
}
}

View File

@@ -0,0 +1,63 @@
<?php
class Brizy_Editor_Forms_Folder extends Brizy_Admin_Serializable {
use Brizy_Editor_Forms_DynamicPropsAware;
/**
* Brizy_Editor_Forms_Folder constructor.
*
* @param \BrizyForms\Model\Folder|null $folder
*/
public function __construct( \BrizyForms\Model\Field $folder = null ) {
$this->data = array();
if ( $folder ) {
$this->data = array( 'id' => $folder->getId(), 'name' => $folder->getName() );
}
}
/**
* @return string
*/
public function serialize() {
return serialize( $this->jsonSerialize() );
}
public function unserialize( $serialized ) {
$this->data = unserialize( $serialized );
}
public function jsonSerialize() {
return $this->data;
}
public function convertToOptionValue() {
return $this->data;
}
static public function createFromSerializedData( $data ) {
$instance = new self();
foreach ( $data as $key => $val ) {
$instance->set( $key, $val );
}
return $instance;
}
public static function createFromJson( $json_obj ) {
if ( ! isset( $json_obj ) ) {
throw new Exception( 'Bad Request', 400 );
}
if ( is_object( $json_obj ) ) {
return self::createFromSerializedData( get_object_vars( $json_obj ) );
}
return new self();
}
}

View File

@@ -0,0 +1,105 @@
<?php
class Brizy_Editor_Forms_FormManager {
/**
* @var Brizy_Editor_Project
*/
private $project;
/**
* @var Brizy_Editor_Forms_Form[]
*/
private $forms;
/**
* Brizy_Editor_Forms_Manager constructor.
*
* @param Brizy_Editor_Project $project
*/
public function __construct( Brizy_Editor_Project $project ) {
$this->project = $project;
try {
$this->loadStorage();
} catch ( Exception $exception ) {
$this->forms = array();
}
}
/**
* @return array|Brizy_Editor_Forms_Form[]
*/
public function getAllForms() {
return $this->forms;
}
/**
* @param $form_id
*
* @return Brizy_Editor_Forms_Form
*/
public function getForm( $form_id ) {
if ( $form_id && isset($this->forms[ $form_id ])) {
return $this->forms[ $form_id ];
}
}
/**
* @param Brizy_Editor_Forms_Form $form
*/
public function addForm( $form ) {
$this->forms[ $form->getId() ] = $form;
$this->updateStorage();
}
/**
* @param Brizy_Editor_Forms_Form $form
*/
public function deleteForm( $form ) {
unset( $this->forms[ $form->getId() ] );
$this->updateStorage();
}
/**
* @param $formId
*/
public function deleteFormById( $formId ) {
unset( $this->forms[ $formId ] );
$this->updateStorage();
}
/**
* @throws Brizy_Editor_Exceptions_NotFound
*/
private function loadStorage() {
$data = $this->project->getMetaValue( 'forms' );
if ( ! $data ) {
$data = array();
}
foreach ( $data as $id => $form_data ) {
if ( $form_data instanceof Brizy_Editor_Forms_Form ) {
$this->forms[ $id ] = $form_data;
} elseif ( is_array( $form_data ) ) {
$this->forms[ $id ] = Brizy_Editor_Forms_Form::createFromSerializedData( $form_data );
}
}
}
private function updateStorage() {
$data = array();
foreach ( $this->forms as $id => $form ) {
$data[ $id ] = $form->convertToOptionValue();
}
$this->project->setMetaValue( 'forms', $data );
$this->project->saveStorage();
}
}

View File

@@ -0,0 +1,326 @@
<?php
class Brizy_Editor_Forms_Form extends Brizy_Admin_Serializable {
/**
* @var string
*/
protected $id;
/**
* @var Brizy_Editor_Forms_AbstractIntegration[]
*/
protected $integrations = array();
/**
* @var bool
*/
protected $hasEmailTemplate = false;
/**
* @var string
*/
protected $emailTemplate;
/**
* @return string
*/
public function serialize() {
return serialize( $this->convertToOptionValue() );
}
public function unserialize( $data ) {
$vars = unserialize( $data );
return self::createFromSerializedData( $vars );
}
/**
* @return array|mixed
*/
public function jsonSerialize() {
$get_object_vars = array(
'id' => $this->id,
'hasEmailTemplate' => $this->hasEmailTemplate(),
'emailTemplate' => $this->getEmailTemplate(),
'integrations' => array()
);
foreach ( $this->integrations as $integration ) {
$get_object_vars['integrations'][] = $integration->convertToOptionValue();
}
return $get_object_vars;
}
public function convertToOptionValue() {
$get_object_vars = array(
'id' => $this->id,
'hasEmailTemplate' => $this->hasEmailTemplate(),
'emailTemplate' => $this->getEmailTemplate(),
'integrations' => array()
);
foreach ( $this->integrations as $integration ) {
$get_object_vars['integrations'][] = $integration->convertToOptionValue();
}
return $get_object_vars;
}
static public function createFromSerializedData( $data ) {
$instance = new self();
$instance->id = $data['id'];
$instance->hasEmailTemplate = $data['hasEmailTemplate'];
$instance->emailTemplate = $data['emailTemplate'];
foreach ( $data['integrations'] as $integration ) {
$brizy_editor_forms_wordpress_integration = Brizy_Editor_Forms_AbstractIntegration::createFromSerializedData( $integration );
if ( $brizy_editor_forms_wordpress_integration ) {
$instance->integrations[] = $brizy_editor_forms_wordpress_integration;
}
}
return $instance;
}
/**
* @return string
*/
public function getId() {
return $this->id;
}
/**
* @param string $id
*
* @return Brizy_Editor_Forms_Form
*/
public function setId( $id ) {
$this->id = $id;
return $this;
}
/**
* @return Brizy_Editor_Forms_Form
* @throws Exception
*/
public static function createFromJson( $json_obj ) {
$formInstance = new self();
if ( ! isset( $json_obj ) ) {
throw new Exception( 'Bad Request', 400 );
}
if ( is_object( $json_obj ) ) {
$formInstance->setId( $json_obj->id );
// add uncompleted wordpress integration
$current_user = wp_get_current_user();
$an_integration = new Brizy_Editor_Forms_WordpressIntegration();
$an_integration->setEmailTo( $current_user->user_email );
$formInstance->addIntegration( $an_integration );
if ( ! empty( $json_obj->integrations ) ) {
foreach ( (array) $json_obj->integrations as $integration ) {
if ( is_object( $integration ) ) {
$formInstance->addIntegration( Brizy_Editor_Forms_AbstractIntegration::createInstanceFromJson( $integration ) );
}
}
}
}
return $formInstance;
}
/**
* @param Brizy_Editor_Forms_Form $instance
* @param $json_obj
*
* @return Brizy_Editor_Forms_Form
* @throws Exception
*/
public static function updateFromJson( Brizy_Editor_Forms_Form $instance, $json_obj ) {
if ( ! isset( $json_obj ) ) {
throw new Exception( 'Bad Request', 400 );
}
if ( is_object( $json_obj ) ) {
$instance->setHasEmailTemplate( $json_obj->hasEmailTemplate );
if ( $json_obj->hasEmailTemplate ) {
$instance->setEmailTemplate( $json_obj->emailTemplate );
} else {
$instance->setEmailTemplate( '' );
}
}
return $instance;
}
/**
* Target can be: create | update
*
* @param string $target
*
* @return array|bool
*/
public function validate( $target = 'create' ) {
$errors = array();
if ( ! $this->getId() ) {
$errors['id'] = 'Invalid form id';
}
if ( $this->hasEmailTemplate && $this->getEmailTemplate() == '' ) {
$errors['emailTemplate'] = 'Invalid email template content';
}
if ( count( $errors ) ) {
return $errors;
}
return true;
}
public function getIntegrations() {
return $this->integrations;
}
/**
* @param $id
*
* @return Brizy_Editor_Forms_AbstractIntegration|null
*/
public function getIntegration( $id ) {
foreach ( $this->integrations as $integration ) {
if ( $integration->getId() == $id ) {
return $integration;
}
}
return null;
}
public function addIntegration( Brizy_Editor_Forms_AbstractIntegration $anIntegration ) {
if ( ! $anIntegration ) {
return false;
}
if ( $this->getIntegration( $anIntegration->getId() ) ) {
return false;
}
$this->integrations[] = $anIntegration;
return true;
}
/**+
* @param $anIntegration
*
* @return bool
*/
public function updateIntegration( Brizy_Editor_Forms_AbstractIntegration $anIntegration ) {
if ( ! $anIntegration ) {
return false;
}
foreach ( $this->integrations as $k => $integration ) {
if ( $integration->getId() == $anIntegration->getId() ) {
$this->integrations[ $k ] = $anIntegration;
return true;
}
}
return false;
}
/**
* @param $id
*
* @return bool
*/
public function deleteIntegration( $id ) {
foreach ( $this->integrations as $k => $integration ) {
if ( $integration->getId() == $id ) {
unset( $this->integrations[ $k ] );
$this->integrations = array_values( $this->integrations );
return true;
}
}
return false;
}
/**
* @return bool
*/
public function hasEmailTemplate() {
return $this->hasEmailTemplate;
}
/**
* @param bool $hasEmailTemplate
*
* @return Brizy_Editor_Forms_Form
*/
public function setHasEmailTemplate( $hasEmailTemplate ) {
$this->hasEmailTemplate = $hasEmailTemplate;
return $this;
}
/**
* @return string
*/
public function getEmailTemplate() {
return $this->emailTemplate;
}
/**
* @param string $emailTemplate
*
* @return Brizy_Editor_Forms_Form
*/
public function setEmailTemplate( $emailTemplate ) {
$this->emailTemplate = $emailTemplate;
return $this;
}
/**
* @param $fields
*
* @return string
*/
public function getEmailTemplateContent( $fields ) {
$field_string = array();
foreach ( $fields as $field ) {
$field_string[] = "{$field->label}: " . esc_html( $field->value );
}
$content = implode( '<br>', $field_string );
return $content;
}
}

View File

@@ -0,0 +1,62 @@
<?php
/**
* Created by PhpStorm.
* User: alex
* Date: 11/20/18
* Time: 4:48 PM
*/
class Brizy_Editor_Forms_GmailSmtpIntegration extends Brizy_Editor_Forms_SmtpIntegration {
/**
* Brizy_Editor_Forms_WordpressIntegration constructor.
*/
public function __construct() {
$this->id = 'gmail_smtp';
$this->host = 'smtp.gmail.com';
$this->port = 465;
$this->encryption = 'ssl';
$this->authentication = true;
}
/**
* @param $json_obj
*
* @return Brizy_Editor_Forms_WordpressIntegration|null
*/
public static function createFromJson( $json_obj ) {
$instance = null;
if ( is_object( $json_obj ) ) {
$instance = new self();
self::populateInstanceDataFromJson( $instance, $json_obj );
if ( isset( $json_obj->emailTo ) ) {
$instance->setEmailTo( trim( $json_obj->emailTo ) );
}
if ( isset( $json_obj->subject ) ) {
$instance->setSubject( trim( $json_obj->subject ) );
}
if ( isset( $json_obj->username ) ) {
$instance->setUsername( trim( $json_obj->username ) );
}
if ( isset( $json_obj->password ) ) {
$instance->setPassword( trim( $json_obj->password ) );
}
}
return $instance;
}
public static function createFromSerializedData( $data, $instance = null ) {
if ( is_null( $instance ) ) {
$instance = new self();
}
$instance = parent::createFromSerializedData( $data, $instance );
return $instance;
}
}

View File

@@ -0,0 +1,63 @@
<?php
class Brizy_Editor_Forms_Group extends Brizy_Admin_Serializable {
use Brizy_Editor_Forms_DynamicPropsAware;
/**
* Brizy_Editor_Forms_Group constructor.
*
* @param \BrizyForms\Model\Group|null $group
*/
public function __construct( \BrizyForms\Model\Group $group = null ) {
$this->data = array();
if ( $group ) {
$this->data = array( 'id' => $group->getId(), 'name' => $group->getName() );
}
}
/**
* @return string
*/
public function serialize() {
return serialize( $this->jsonSerialize() );
}
public function unserialize( $serialized ) {
$this->data = unserialize( $serialized );
}
public function jsonSerialize() {
return $this->data;
}
public function convertToOptionValue() {
return $this->data;
}
static public function createFromSerializedData( $data ) {
$instance = new self();
foreach ( $data as $key => $val ) {
$instance->set( $key, $val );
}
return $instance;
}
public static function createFromJson( $json_obj ) {
if ( ! isset( $json_obj ) ) {
throw new Exception( 'Bad Request', 400 );
}
if ( is_object( $json_obj ) ) {
return self::createFromSerializedData( get_object_vars( $json_obj ) );
}
return new self();
}
}

View File

@@ -0,0 +1,479 @@
<?php
//class BrizyPro_Forms_ServiceIntegration extends Brizy_Editor_Forms_AbstractIntegration {
class Brizy_Editor_Forms_ServiceIntegration extends Brizy_Editor_Forms_AbstractIntegration {
/**
* @var array
*/
protected $accounts = array();
/**
* @var array
*/
protected $fields = array();
/**
* @var array
*/
protected $lists = array();
/**
* @var array
*/
protected $listProperties = array();
/**
* @var array
*/
protected $folders = array();
/**
* @var
*/
protected $usedAccount;
/**
* @var
*/
protected $fieldsMap = '[]';
/**
* @var
*/
protected $usedList;
/**
* @var bool
*/
protected $hasConfirmation = false;
/**
* @var bool
*/
protected $confirmationNeeded = false;
/**
* @var string
*/
protected $usedFolder;
/**
* @param $fields
*
* @return bool|mixed
* @throws Exception
*/
public function handleSubmit( Brizy_Editor_Forms_Form $form, $fields ) {
$this->exception = null;
/**
* @var \BrizyForms\Service\Service $service ;
*/
$service = \BrizyForms\ServiceFactory::getInstance( $this->getId() );
if ( ! ( $service instanceof \BrizyForms\Service\Service ) ) {
$this->error( 400, "Invalid integration service" );
}
do_action( 'brizy_submit_form', $service, $fields, $this );
}
/**
* @return array|mixed
*/
public function jsonSerialize() {
$get_object_vars = parent::jsonSerialize();
if ( ! is_null( $this->getFields() ) ) {
$get_object_vars['fields'] = $this->getFields();
}
if ( ! is_null( $this->getLists() ) ) {
$get_object_vars['lists'] = $this->getLists();
}
if ( ! is_null( $this->getListProperties() ) ) {
$get_object_vars['listProperties'] = $this->getListProperties();
}
if ( ! is_null( $this->getFolders() ) ) {
$get_object_vars['folders'] = $this->getFolders();
}
if ( ! is_null( $this->getUsedAccount() ) ) {
$get_object_vars['usedAccount'] = $this->getUsedAccount();
}
if ( ! is_null( $this->getUsedList() ) ) {
$get_object_vars['usedList'] = $this->getUsedList();
}
if ( ! is_null( $this->getUsedFolder() ) ) {
$get_object_vars['usedFolder'] = $this->getUsedFolder();
}
if ( ! is_null( $this->getFieldsMap() ) ) {
$get_object_vars['fieldsMap'] = $this->getFieldsMap();
}
if ( ! is_null( $this->getAccounts() ) ) {
$get_object_vars['accounts'] = $this->getAccounts();
}
if ( ! is_null( $this->isConfirmationNeeded() ) ) {
$get_object_vars['confirmationNeeded'] = $this->isConfirmationNeeded();
}
if ( ! is_null( $this->hasConfirmation() ) ) {
$get_object_vars['hasConfirmation'] = $this->hasConfirmation();
}
return $get_object_vars;
}
/**
* @return string
*/
public function serialize() {
$value = $this->jsonSerialize();
unset( $value['accounts'] );
unset( $value['folders'] );
unset( $value['lists'] );
unset( $value['fields'] );
unset( $value['listProperties'] );
return serialize( $value );
}
/**
* @param $json_obj
*
* @return Brizy_Editor_Forms_ServiceIntegration|null
* @throws Exception
*/
public static function createFromJson( $json_obj ) {
$instance = null;
if ( is_object( $json_obj ) ) {
$instance = new self( $json_obj->id );
if ( isset( $json_obj->fields ) ) {
foreach ( $json_obj->fields as $field ) {
$instance->addField( Brizy_Editor_Forms_Field::createFromJson( $field ) );
}
}
if ( isset( $json_obj->lists ) ) {
foreach ( $json_obj->lists as $list ) {
if ( ! $list instanceof Brizy_Editor_Forms_Group ) {
$instance->addList( Brizy_Editor_Forms_Group::createFromJson( $list ) );
} else {
$instance->addList( $list );
}
}
}
if ( isset( $json_obj->folders ) ) {
foreach ( $json_obj->folders as $folder ) {
if ( ! $folder instanceof Brizy_Editor_Forms_Folder ) {
$instance->addFolder( Brizy_Editor_Forms_Folder::createFromJson( $folder ) );
} else {
$instance->addFolder( $folder );
}
}
}
if ( isset( $json_obj->usedAccount ) ) {
$instance->setUsedAccount( $json_obj->usedAccount );
}
if ( isset( $json_obj->usedList ) ) {
$instance->setUsedList( $json_obj->usedList );
}
if ( isset( $json_obj->usedFolder ) ) {
$instance->setUsedFolder( $json_obj->usedFolder );
}
if ( isset( $json_obj->fieldsMap ) ) {
$instance->setFieldsMap( $json_obj->fieldsMap );
}
if ( isset( $json_obj->confirmationNeeded ) ) {
$instance->setConfirmationNeeded( $json_obj->confirmationNeeded );
}
if ( isset( $json_obj->hasConfirmation ) ) {
$instance->setHasConfirmation( $json_obj->hasConfirmation );
}
}
return $instance;
}
static public function createFromSerializedData( $data, $instance = null ) {
if ( is_null( $instance ) ) {
$instance = new self( $data['id'] );
}
if ( isset( $data['completed'] ) ) {
$instance->setCompleted( $data['completed'] );
}
if ( isset( $data['usedAccount'] ) ) {
$instance->setUsedAccount( $data['usedAccount'] );
}
if ( isset( $data['usedList'] ) ) {
$instance->setUsedList( $data['usedList'] );
}
if ( isset( $data['usedFolder'] ) ) {
$instance->setUsedFolder( $data['usedFolder'] );
}
if ( isset( $data['fieldsMap'] ) ) {
if ( is_array( $data['fieldsMap'] ) ) {
$instance->setFieldsMap( json_encode( $data['fieldsMap'] ) );
} elseif ( empty( $data['fieldsMap'] ) ) {
$instance->setFieldsMap( '[]' );
} else {
$instance->setFieldsMap( $data['fieldsMap'] );
}
}
if ( isset( $data['confirmationNeeded'] ) ) {
$instance->setConfirmationNeeded( $data['confirmationNeeded'] );
}
if ( isset( $data['hasConfirmation'] ) ) {
$instance->setHasConfirmation( $data['hasConfirmation'] );
}
return $instance;
}
/**
* @param Brizy_Editor_Forms_Group $list
*/
public function addList( Brizy_Editor_Forms_Group $list ) {
$this->lists[] = $list;
}
/**
* @param Brizy_Editor_Forms_Folder $folders
*/
public function addFolder( Brizy_Editor_Forms_Folder $folders ) {
$this->folders[] = $folders;
}
/**
* @param Brizy_Editor_Forms_Field $field
*/
public function addField( Brizy_Editor_Forms_Field $field ) {
$this->fields[] = $field;
}
/**
* @return array
*/
public function getFolders() {
return $this->folders;
}
/**
* @param array $folders
*
* @return Brizy_Editor_Forms_ServiceIntegration
*/
public function setFolders( $folders ) {
$this->folders = $folders;
return $this;
}
/**
* @return array
*/
public function getFields() {
return $this->fields;
}
/**
* @param array $fields
*
* @return self
*/
public function setFields( $fields ) {
$this->fields = $fields;
return $this;
}
/**
* @return array
*/
public function getLists() {
return $this->lists;
}
/**
* @param array $lists
*
* @return self
*/
public function setLists( $lists ) {
$this->lists = $lists;
return $this;
}
/**
* @return mixed
*/
public function getUsedAccount() {
return $this->usedAccount;
}
/**
* @param mixed $usedAccount
*
* @return self
*/
public function setUsedAccount( $usedAccount ) {
$this->usedAccount = $usedAccount;
return $this;
}
/**
* @return mixed
*/
public function getUsedListObject() {
$used_account = $this->getUsedList();
foreach ( (array) $this->lists as $list ) {
$var = $list->getId();
if ( $var == $used_account ) {
return $list;
}
}
return null;
}
/**
* @return mixed
*/
public function getUsedList() {
return $this->usedList;
}
/**
* @param mixed $usedList
*
* @return self
*/
public function setUsedList( $usedList ) {
$this->usedList = $usedList;
return $this;
}
/**
* @return mixed
*/
public function getFieldsMap() {
return $this->fieldsMap;
}
/**
* @param mixed $fieldsMap
*
* @return self
*/
public function setFieldsMap( $fieldsMap ) {
$this->fieldsMap = $fieldsMap;
return $this;
}
/**
* @return array
*/
public function getAccounts() {
return $this->accounts;
}
/**
* @param array $accounts
*
* @return Brizy_Editor_Forms_ServiceIntegration
*/
public function setAccounts( $accounts ) {
$this->accounts = $accounts;
return $this;
}
/**
* @return bool
*/
public function isConfirmationNeeded() {
return (bool) $this->confirmationNeeded;
}
/**
* @param bool $confirmationNeeded
*
* @return Brizy_Editor_Forms_ServiceIntegration
*/
public function setConfirmationNeeded( $confirmationNeeded ) {
$this->confirmationNeeded = (bool) $confirmationNeeded;
return $this;
}
/**
* @return bool
*/
public function HasConfirmation() {
return $this->hasConfirmation;
}
/**
* @param bool $hasConfirmation
*
* @return Brizy_Editor_Forms_ServiceIntegration
*/
public function setHasConfirmation( $hasConfirmation ) {
$this->hasConfirmation = $hasConfirmation;
return $this;
}
/**
* @return string
*/
public function getUsedFolder() {
return $this->usedFolder;
}
/**
* @param string $usedFolder
*
* @return Brizy_Editor_Forms_ServiceIntegration
*/
public function setUsedFolder( $usedFolder ) {
$this->usedFolder = $usedFolder;
return $this;
}
/**
* @return array
*/
public function getListProperties() {
return $this->listProperties;
}
/**
* @param array $listProperties
*
* @return Brizy_Editor_Forms_ServiceIntegration
*/
public function setListProperties( $listProperties ) {
$this->listProperties = $listProperties;
return $this;
}
}

View File

@@ -0,0 +1,278 @@
<?php
/**
* Created by PhpStorm.
* User: alex
* Date: 11/20/18
* Time: 4:48 PM
*/
class Brizy_Editor_Forms_SmtpIntegration extends Brizy_Editor_Forms_WordpressIntegration {
/**
* @var string
*/
protected $host;
/**
* @var string
*/
protected $port;
/**
* @var string
*/
protected $authentication;
/**
* @var string
*/
protected $username;
/**
* @var string
*/
protected $password;
/**
* @var string
*/
protected $encryption = 'ssl';
/**
* Brizy_Editor_Forms_WordpressIntegration constructor.
*/
public function __construct() {
$this->id = 'smtp';
}
/**
* @param Brizy_Editor_Forms_Form $form
* @param $fields
*
* @return bool|mixed
* @throws Exception
*/
public function handleSubmit( Brizy_Editor_Forms_Form $form, $fields ) {
add_action( 'phpmailer_init', array( $this, 'decoratePhpMailer' ) );
add_action( 'wp_mail_failed', array( $this, 'handleFailToSendMessage' ) );
$result = parent::handleSubmit( $form, $fields );
}
public function decoratePhpMailer( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = $this->getHost();
$phpmailer->SMTPSecure = $this->getEncryption();
$phpmailer->SMTPAuth = $this->getAuthentication();
$phpmailer->Port = $this->getPort();
$phpmailer->Username = $this->getUsername();
$phpmailer->Password = $this->getPassword();
}
/**
* @return array|mixed
*/
public function jsonSerialize() {
$get_object_vars = parent::jsonSerialize();
$get_object_vars['emailTo'] = $this->getEmailTo();
$get_object_vars['subject'] = $this->getSubject();
$get_object_vars['host'] = $this->getHost();
$get_object_vars['authentication'] = $this->getAuthentication();
$get_object_vars['port'] = $this->getPort();
$get_object_vars['username'] = $this->getUsername();
$get_object_vars['password'] = $this->getPassword();
$get_object_vars['encryption'] = $this->getEncryption();
return $get_object_vars;
}
/**
* @param $json_obj
*
* @return Brizy_Editor_Forms_WordpressIntegration|null
*/
public static function createFromJson( $json_obj ) {
$instance = null;
if ( is_object( $json_obj ) ) {
$instance = new self();
self::populateInstanceDataFromJson( $instance, $json_obj );
if ( isset( $json_obj->host ) ) {
$instance->setHost( trim( $json_obj->host ) );
}
if ( isset( $json_obj->port ) ) {
$instance->setPort( trim( $json_obj->port ) );
}
if ( isset( $json_obj->authentication ) ) {
$instance->setAuthentication( $json_obj->authentication );
}
if ( isset( $json_obj->username ) ) {
$instance->setUsername( trim( $json_obj->username ) );
}
if ( isset( $json_obj->password ) ) {
$instance->setPassword( trim( $json_obj->password ) );
}
if ( isset( $json_obj->encryption ) ) {
$instance->setEncryption( $json_obj->encryption );
}
}
return $instance;
}
static public function createFromSerializedData( $data, $instance = null ) {
if ( is_null( $instance ) ) {
$instance = new self();
}
$instance = parent::createFromSerializedData( $data, $instance );
if ( isset( $data['host'] ) ) {
$instance->setHost( $data['host'] );
}
if ( isset( $data['port'] ) ) {
$instance->setPort( $data['port'] );
}
if ( isset( $data['username'] ) ) {
$instance->setUsername( $data['username'] );
}
if ( isset( $data['password'] ) ) {
$instance->setPassword( $data['password'] );
}
if ( isset( $data['encryption'] ) ) {
$instance->setEncryption( $data['encryption'] );
}
if ( isset( $data['authentication'] ) ) {
$instance->setAuthentication( $data['authentication'] );
}
return $instance;
}
/**
* @return string
*/
public function getHost() {
return $this->host;
}
/**
* @param string $host
*
* @return Brizy_Editor_Forms_SmtpIntegration
*/
public function setHost( $host ) {
$this->host = $host;
return $this;
}
/**
* @return string
*/
public function getPort() {
return $this->port;
}
/**
* @param string $port
*
* @return Brizy_Editor_Forms_SmtpIntegration
*/
public function setPort( $port ) {
$this->port = $port;
return $this;
}
/**
* @return string
*/
public function getAuthentication() {
return $this->authentication;
}
/**
* @param string $authentication
*
* @return Brizy_Editor_Forms_SmtpIntegration
*/
public function setAuthentication( $authentication ) {
$this->authentication = (bool)$authentication;
return $this;
}
/**
* @return string
*/
public function getUsername() {
return $this->username;
}
/**
* @param string $username
*
* @return Brizy_Editor_Forms_SmtpIntegration
*/
public function setUsername( $username ) {
$this->username = $username;
return $this;
}
/**
* @return string
*/
public function getPassword() {
return $this->password;
}
/**
* @param string $password
*
* @return Brizy_Editor_Forms_SmtpIntegration
*/
public function setPassword( $password ) {
$this->password = $password;
return $this;
}
/**
* @return string
*/
public function getEncryption() {
return $this->encryption;
}
/**
* @param string $encryption
*
* @return Brizy_Editor_Forms_SmtpIntegration
*/
public function setEncryption( $encryption ) {
//this is a hack because we had bool values on this property
if ( is_bool( $encryption ) ) {
$this->encryption = $encryption ? 'ssl' : 'tls';
} else {
$this->encryption = $encryption;
}
return $this;
}
}

View File

@@ -0,0 +1,424 @@
<?php
class Brizy_Editor_Forms_WordpressIntegration extends Brizy_Editor_Forms_AbstractIntegration {
/**
* @var string
*/
protected $emailTo;
/**
* @var string
*/
protected $subject;
/**
* @var string
*/
protected $fromEmail;
/**
* @var string
*/
protected $fromName;
/**
* @var string
*/
protected $replayTo;
/**
* @var string
*/
protected $cc;
/**
* @var string
*/
protected $bcc;
/**
* @var string
*/
protected $metaData;
/**
* Brizy_Editor_Forms_WordpressIntegration constructor.
*/
public function __construct() {
parent::__construct( 'wordpress' );
}
/**
* @param Brizy_Editor_Forms_Form $form
* @param $fields
*
* @return bool|mixed
* @throws Exception
*/
public function handleSubmit( Brizy_Editor_Forms_Form $form, $fields ) {
$this->exception = null;
///$recipients = explode( ',', $this->getEmailTo() );
$headers = array();
$headers[] = 'Content-type: text/html; charset=UTF-8';
if ( $this->getCc() ) {
$headers[] = "Cc: {$this->getCc()}";
}
if ( $this->getBcc() ) {
$headers[] = "Bcc: {$this->getBcc()}";
}
if ( $this->getReplayTo() ) {
$headers[] = "Reply-To: {$this->getReplayTo()}";
}
if ( $this->getFromEmail() ) {
$fromName = '';
if ( $this->getFromName() ) {
$headers[] = "From: \"{$this->getFromName()}\" <{$this->getFromEmail()}>";
} else {
$headers[] = "From: {$this->getFromEmail()}";
}
}
// $field_string = array();
// foreach ( $fields as $field ) {
// $field_string[] = "{$field->label}: " . esc_html( $field->value );
// }
$email_body = $form->getEmailTemplateContent( $fields );
$headers = apply_filters( 'brizy_form_email_headers', $headers, $fields, $form );
$email_body = apply_filters( 'brizy_form_email_body', $email_body, $fields, $form );
$email_subject = apply_filters( 'brizy_form_email_subject', $this->getSubject(), $fields, $form );
$email_body = $this->insertMetaDataFields( $email_body );
if ( ! function_exists( 'wp_mail' ) ) {
throw new Exception( 'Please check your wordpress configuration.' );
}
return wp_mail(
$this->getEmailTo(),
$email_subject,
$email_body,
$headers
);
}
/**
* @return array|mixed
*/
public function jsonSerialize() {
$get_object_vars = parent::jsonSerialize();
$get_object_vars['emailTo'] = $this->getEmailTo();
$get_object_vars['subject'] = $this->getSubject();
$get_object_vars['fromEmail'] = $this->getFromEmail();
$get_object_vars['fromName'] = $this->getFromName();
$get_object_vars['replayTo'] = $this->getReplayTo();
$get_object_vars['cc'] = $this->getCc();
$get_object_vars['bcc'] = $this->getBcc();
$get_object_vars['metaData'] = $this->getMetaData();
return $get_object_vars;
}
static public function createFromSerializedData( $data, $instance = null ) {
if ( is_null( $instance ) ) {
$instance = new self();
}
if ( isset( $data['completed'] ) ) {
$instance->setCompleted( $data['completed'] );
}
if ( isset( $data['emailTo'] ) ) {
$instance->setEmailTo( $data['emailTo'] );
}
if ( isset( $data['subject'] ) ) {
$instance->setSubject( $data['subject'] );
}
if ( isset( $data['fromEmail'] ) ) {
$instance->setFromEmail( $data['fromEmail'] );
}
if ( isset( $data['fromName'] ) ) {
$instance->setFromName( $data['fromName'] );
}
if ( isset( $data['replayTo'] ) ) {
$instance->setReplayTo( $data['replayTo'] );
}
if ( isset( $data['cc'] ) ) {
$instance->setCc( $data['cc'] );
}
if ( isset( $data['bcc'] ) ) {
$instance->setBcc( $data['bcc'] );
}
if ( isset( $data['metaData'] ) ) {
$instance->setMetaData( $data['metaData'] );
}
return $instance;
}
/**
* @return string
*/
public function serialize() {
return serialize( $this->jsonSerialize() );
}
/**
* @return string
*/
public function getEmailTo() {
return $this->emailTo;
}
/**
* @param string $emailTo
*
* @return Brizy_Editor_Forms_WordpressIntegration
*/
public function setEmailTo( $emailTo ) {
$this->emailTo = $emailTo;
return $this;
}
/**
* @return string
*/
public function getSubject() {
return $this->subject;
}
/**
* @param string $subject
*
* @return Brizy_Editor_Forms_WordpressIntegration
*/
public function setSubject( $subject ) {
$this->subject = $subject;
return $this;
}
/**
* @return string
*/
public function getFromEmail() {
return $this->fromEmail;
}
/**
* @param string $fromEmail
*
* @return Brizy_Editor_Forms_WordpressIntegration
*/
public function setFromEmail( $fromEmail ) {
$this->fromEmail = $fromEmail;
return $this;
}
/**
* @return string
*/
public function getFromName() {
return $this->fromName;
}
/**
* @param string $fromName
*
* @return Brizy_Editor_Forms_WordpressIntegration
*/
public function setFromName( $fromName ) {
$this->fromName = $fromName;
return $this;
}
/**
* @return string
*/
public function getReplayTo() {
return $this->replayTo;
}
/**
* @param string $replayTo
*
* @return Brizy_Editor_Forms_WordpressIntegration
*/
public function setReplayTo( $replayTo ) {
$this->replayTo = $replayTo;
return $this;
}
/**
* @return string
*/
public function getCc() {
return $this->cc;
}
/**
* @param string $cc
*
* @return Brizy_Editor_Forms_WordpressIntegration
*/
public function setCc( $cc ) {
$this->cc = $cc;
return $this;
}
/**
* @return string
*/
public function getBcc() {
return $this->bcc;
}
/**
* @param string $bcc
*
* @return Brizy_Editor_Forms_WordpressIntegration
*/
public function setBcc( $bcc ) {
$this->bcc = $bcc;
return $this;
}
/**
* @return string
*/
public function getMetaData() {
return $this->metaData;
}
/**
* @param string $metaData
*
* @return Brizy_Editor_Forms_WordpressIntegration
*/
public function setMetaData( $metaData ) {
$this->metaData = $metaData;
return $this;
}
/**
* @param $json_obj
*
* @return Brizy_Editor_Forms_WordpressIntegration|null
*/
public static function createFromJson( $json_obj ) {
$instance = null;
if ( is_object( $json_obj ) ) {
$instance = new self();
self::populateInstanceDataFromJson( $instance, $json_obj );
}
return $instance;
}
private function insertMetaDataFields( $emailBody ) {
$metaFields = explode( ',', $this->getMetaData() );
$tests = array();
foreach ( $metaFields as $meta ) {
switch ( $meta ) {
case 'time':
$tests[] = 'Time: ' . date( 'Y-m-d H:i:s' );
break;
case 'pageUrl':
$tests[] = 'Page Url: ' . $_SERVER['HTTP_REFERER'];
break;
case 'userAgent':
$tests[] = 'User Agent: ' . $_SERVER['HTTP_USER_AGENT'];
break;
case 'remoteIp':
$ip = $_SERVER['REMOTE_ADDR'];
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
$tests[] = 'Remote IP: ' . $ip;
break;
case 'credit':
$tests[] = 'Powered by : ' . __bt( 'brizy', 'Brizy' );
break;
}
}
$tests = implode( '<br>', $tests );
return $emailBody . "<br>-------------------------<br>" . $tests;
}
protected static function populateInstanceDataFromJson( $instance, $json_obj ) {
if ( is_object( $json_obj ) ) {
if ( isset( $json_obj->emailTo ) ) {
$instance->setEmailTo( trim( $json_obj->emailTo ) );
} else {
// set the default email
$current_user = wp_get_current_user();
$instance->setEmailTo( $current_user->user_email );;
}
if ( isset( $json_obj->subject ) ) {
$instance->setSubject( trim( $json_obj->subject ) );
}
if ( isset( $json_obj->fromEmail ) ) {
$instance->setFromEmail( trim( $json_obj->fromEmail ) );
}
if ( isset( $json_obj->fromName ) ) {
$instance->setFromName( trim( $json_obj->fromName ) );
}
if ( isset( $json_obj->replayTo ) ) {
$instance->setReplayTo( trim( $json_obj->replayTo ) );
}
if ( isset( $json_obj->cc ) ) {
$instance->setCc( trim( $json_obj->cc ) );
}
if ( isset( $json_obj->bcc ) ) {
$instance->setBcc( trim( $json_obj->bcc ) );
}
if ( isset( $json_obj->metaData ) ) {
$instance->setMetaData( trim( $json_obj->metaData ) );
}
}
return $instance;
}
}