app = $app;
}
/**
* Get all the available components
* @return void
* @throws \Exception
* @throws \FluentForm\Framework\Exception\UnResolveableEntityException
*/
public function index()
{
Acl::verify('fluentform_forms_manager');
$this->app->doAction(
'fluent_editor_init',
$components = $this->app->make('components')
);
$editorCompnents = $components->sort();
$editorCompnents = $this->app->applyFilters('fluent_editor_components', $editorCompnents);
$countries = $this->app->load($this->app->appPath('Services/FormBuilder/CountryNames.php'));
wp_send_json_success(array(
'components' => $editorCompnents,
'countries' => $countries,
'disabled_components' => $this->getDisabledComponents()
));
exit();
}
/**
* Get disabled components
* @return array
*/
private function getDisabledComponents()
{
$isReCaptchaDisabled = !get_option('_fluentform_reCaptcha_keys_status', false);
$disabled = array(
'recaptcha' => array(
'contentComponent' => 'recaptcha',
'disabled' => $isReCaptchaDisabled
),
'input_image' => array(
'disabled' => true
),
'input_file' => array(
'disabled' => true
),
'input_repeat' => array(
'disabled' => true
),
'shortcode' => array(
'disabled' => true
),
'action_hook' => array(
'disabled' => true
),
'form_step' => array(
'disabled' => true
),
);
return $this->app->applyFilters('disabled_components', $disabled);
}
/**
* Get available shortcodes for editor
* @return void
* @throws \Exception
*/
public function getEditorShortcodes()
{
Acl::verify('fluentform_forms_manager');
$editor_shortcodes = fluentFormEditorShortCodes();
wp_send_json_success(array('shortcodes' => $editor_shortcodes), 200);
exit();
}
/**
* Register the form renderer shortcode
* @return void
*/
public function addFluentFormShortCode() {
$this->app->addShortCode( 'fluentform', function ( $atts, $content ) {
$atts = shortcode_atts( array(
'id' => null,
'title' => null
), $atts );
$form_id = $atts['id'];
if ( $form_id ) {
$form = wpFluent()->table( 'fluentform_forms' )->find( $form_id );
} else if ( $formTitle = $atts['title'] ) {
$form = wpFluent()->table( 'fluentform_forms' )->where( 'title', $formTitle )->first();
} else {
return;
}
if ( ! $form ) {
return;
}
$formSettings = wpFluent()
->table( 'fluentform_form_meta' )
->where( 'form_id', $form_id )
->where( 'meta_key', 'formSettings' )
->first();
$form->fields = json_decode( $form->form_fields, true );
if ( ! $form->fields['fields'] ) {
return;
}
$form->settings = json_decode( $formSettings->value, true );
$form = $this->app->applyFilters( 'fluentform_rendering_form', $form );
$isRenderable = array(
'status' => true,
'message' => ''
);
$isRenderable = $this->app->applyFilters( 'fluentform_is_form_renderable', $isRenderable, $form );
if ( is_array( $isRenderable ) && ! $isRenderable['status'] ) {
return "
{$isRenderable['message']}
";
}
$formBuilder = $this->app->make( 'formBuilder' );
$output = $formBuilder->build( $form );
wp_enqueue_style(
'fluent-form-styles',
$this->app->publicUrl( 'css/fluent-forms-public.css' )
);
wp_enqueue_style(
'fluentform-public-default',
$this->app->publicUrl( 'css/fluentform-public-default.css' )
);
wp_enqueue_script(
'fluent-form-submission',
$this->app->publicUrl( 'js/form-submission.js' ),
array( 'jquery' ),
false,
true
);
$form_vars = array(
'id' => $form->id,
'settings' => $form->settings,
'rules' => $formBuilder->validationRules,
'do_analytics' => $this->app->applyFilters( 'fluentform_do_analytics', true )
);
if ( $conditionals = $formBuilder->conditions ) {
wp_enqueue_script(
'fluent-form-conditionals',
$this->app->publicUrl( 'js/form-conditionals.js' ),
array( 'jquery' ),
false,
true
);
$form_vars['conditionals'] = $conditionals;
}
wp_localize_script( 'fluent-form-submission', 'fluentFormVars', array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'forms' => (Object) array()
) );
$this->addInlineVars( json_encode( $form_vars ), $form->id );
return $output;
} );
}
/**
* Register renderer actions for compiling each element
* @return void
*/
public function addRendererActions()
{
$actionMappings = [
'Select@compile' => ['render_item_select'],
'Address@compile' => ['render_item_address'],
'Name@compile' => ['render_item_input_name'],
'TextArea@compile' => ['render_item_textarea'],
'DateTime@compile' => ['render_item_input_date'],
'Recaptcha@compile' => ['render_item_recaptcha'],
'Container@compile' => ['render_item_container'],
'CustomHtml@compile' => ['render_item_custom_html'],
'SectionBreak@compile' => ['render_item_section_break'],
'SubmitButton@compile' => ['render_item_submit_button'],
'SelectCountry@compile' => ['render_item_select_country'],
'TermsAndConditions@compile' => ['render_item_terms_and_condition'],
'Checkable@compile' => [
'render_item_input_radio',
'render_item_input_checkbox',
],
'Text@compile' => [
'render_item_input_url',
'render_item_input_text',
'render_item_input_email',
'render_item_input_number',
'render_item_input_hidden',
'render_item_input_password',
],
];
$path = 'FluentForm\App\Services\FormBuilder\Components\\';
foreach ($actionMappings as $handler => $actions) {
foreach ($actions as $action) {
$this->app->addAction($action, function() use ($path, $handler) {
list($class, $method) = $this->app->parseHandler($path.$handler);
call_user_func_array(array($class, $method), func_get_args());
}, 10, 2);
}
}
}
/**
* Register dynamic value shortcode parser (filter default value)
* @return void
*/
public function addFluentFormDefaultValueParser()
{
$this->app->addFilter('fluentform_parse_default_value', function($value, $form) {
return EditorShortcodeParser::filter($value, $form);
}, 10, 2);
}
/**
* Register filter to check whether the form is renderable
* @return mixed
*/
public function addIsRenderableFilter()
{
$this->app->addFilter('fluentform_is_form_renderable', function($isRenderable, $form) {
$checkables = array('limitNumberOfEntries', 'scheduleForm', 'requireLogin');
foreach ($form->settings['restrictions'] as $key => $restrictions) {
if (in_array($key, $checkables)) {
if (!($isRenderable['status'] = $this->{$key}($restrictions, $form, $isRenderable))) {
return $isRenderable;
}
}
}
return $isRenderable;
}, 10, 2);
}
/**
* Check if limit is set on form submits and it's valid yet
* @param array $restrictions
* @return bool
*/
private function limitNumberOfEntries($restrictions, $form, &$isRenderable)
{
if (!$restrictions['enabled']) {
return true;
}
$col = 'created_at';
$period = $restrictions['period'];
$maxAllowedEntries = $restrictions['numberOfEntries'];
$query = wpFluent()->table('fluentform_submissions')
->where('form_id', $form->id)
->where('status', '!=', 'trashed');
if ($period == 'day') {
$year = "YEAR(`{$col}`) = YEAR(NOW())";
$month = "MONTH(`{$col}`) = MONTH(NOW())";
$day = "DAY(`{$col}`) = DAY(NOW())";
$query->where(wpFluent()->raw("{$year} AND {$month} AND {$day}"));
} elseif ($period == 'week') {
$query->where(
wpFluent()->raw("YEARWEEK(`{$col}`, 1) = YEARWEEK(CURDATE(), 1)")
);
} elseif ($period == 'month') {
$year = "YEAR(`{$col}`) = YEAR(NOW())";
$month = "MONTH(`{$col}`) = MONTH(NOW())";
$query->where(wpFluent()->raw("{$year} AND {$month}"));
} elseif ($period == 'year') {
$query->where(wpFluent()->raw("YEAR(`{$col}`) = YEAR(NOW())"));
}
if (!($isAllowed = ($query->count() < $maxAllowedEntries))) {
$isRenderable['message'] = $restrictions['limitReachedMsg'];
}
return $isAllowed;
}
/**
* Check if form has scheduled date and open for submission
* @param array $restrictions
* @return bool
*/
private function scheduleForm($restrictions, $form, &$isRenderable)
{
if (!$restrictions['enabled']) {
return true;
}
$time = time();
$start = strtotime($restrictions['start']);
$end = strtotime($restrictions['end']);
if ($time < $start) {
$isRenderable['message'] = $restrictions['pendingMsg'];
return false;
}
if ($time >= $end) {
$isRenderable['message'] = $restrictions['expiredMsg'];
return false;
}
return true;
}
/**
* * Check if form requires loged in user and user is logged in
* @param array $restrictions
* @return bool
*/
private function requireLogin($restrictions, $form, &$isRenderable)
{
if (!$restrictions['enabled']) {
return true;
}
if (!($isLoggedIn = is_user_logged_in())) {
$isRenderable['message'] = $restrictions['requireLoginMsg'];
}
return $isLoggedIn;
}
/**
* Register fluentform_submission_inserted action
* @return void
*/
public function addFluentformSubmissionInsertedFilter()
{
$this->app->addAction(
'fluentform_submission_inserted', function($insertId, $data, $form) {
$notifications = wpFluent()
->table('fluentform_form_meta')
->where('form_id', $form->id)
->where('meta_key', 'notifications')
->get();
$enabledNotifications = array();
foreach ($notifications as $key => $notification) {
$notification = json_decode($notification->value, true);
if ($notification['enabled'] && ConditionAssesor::evaluate($notification, $data)) {
$enabledNotifications[] = $notification;
}
}
if($enabledNotifications) {
$enabledNotifications = MessageShortCodeParser::parseMessageShortCode(
$enabledNotifications, $insertId, $data, $form
);
$notifier = $this->app->make(
'FluentForm\App\Services\FormBuilder\Notifications\EmailNotification'
);
foreach ($enabledNotifications as $notification) {
$notifier->notify($notification, $data, $form);
}
}
}, 10, 3);
}
/**
* Add inline scripts [Add localized script using same var]
* @param array $vars
* @param int $form_id
* @return void
*/
private function addInlineVars($vars, $form_id) {
if (function_exists('wp_add_inline_script')) {
wp_add_inline_script(
'fluent-form-submission',
'window.fluentFormVars.forms["fluentform_'.$form_id.'"] = '.$vars.';'
);
} else {
add_action('wp_footer', function () use ($vars, $form_id) {
?>