<?php
if (!defined('ABSPATH')) {
exit;
}
if (!class_exists('acf_form_front')) {
class acf_form_front
{
private $forms = array();
public $fields = array();
function __construct()
{
$this->fields = array('_post_title' => array('prefix' => 'acf', 'name' => '_post_title', 'key' => '_post_title', 'label' => __('Title', 'acf'), 'type' => 'text', 'required' => true), '_post_content' => array('prefix' => 'acf', 'name' => '_post_content', 'key' => '_post_content', 'label' => __('Content', 'acf'), 'type' => 'wysiwyg'), '_validate_email' => array('prefix' => 'acf', 'name' => '_validate_email', 'key' => '_validate_email', 'label' => __('Validate Email', 'acf'), 'type' => 'text', 'value' => '', 'wrapper' => array('style' => 'display:none !important;')));
add_action('acf/validate_save_post', array($this, 'validate_save_post'), 1);
add_filter('acf/pre_save_post', array($this, 'pre_save_post'), 5, 2);
}
function validate_form($args)
{
$args = wp_parse_args($args, array('id' => 'acf-form', 'post_id' => false, 'new_post' => false, 'field_groups' => false, 'fields' => false, 'post_title' => false, 'post_content' => false, 'form' => true, 'form_attributes' => array(), 'return' => add_query_arg('updated', 'true', acf_get_current_url()), 'html_before_fields' => '', 'html_after_fields' => '', 'submit_value' => __('Update', 'acf'), 'updated_message' => __('Post updated', 'acf'), 'label_placement' => 'top', 'instruction_placement' => 'label', 'field_el' => 'div', 'uploader' => 'wp', 'honeypot' => true, 'html_updated_message' => '<div id="message" class="updated"><p>%s</p></div>', 'html_submit_button' => '<input type="submit" class="acf-button button button-primary button-large" value="%s" />', 'html_submit_spinner' => '<span class="acf-spinner"></span>', 'kses' => true));
$args['form_attributes'] = wp_parse_args($args['form_attributes'], array('id' => $args['id'], 'class' => 'acf-form', 'action' => '', 'method' => 'post'));
$args['post_id'] = acf_get_valid_post_id($args['post_id']);
if ($args['post_id'] === 'new_post') {
$args['new_post'] = wp_parse_args($args['new_post'], array('post_type' => 'post', 'post_status' => 'draft'));
}
$args = apply_filters('acf/validate_form', $args);
return $args;
}
function add_form($args = array())
{
$args = $this->validate_form($args);
$this->forms[$args['id']] = $args;
}
function get_form($id = '')
{
if (!isset($this->forms[$id])) {
return false;
}
return $this->forms[$id];
}
function validate_save_post()
{
foreach ($this->fields as $k => $field) {
if (!isset($_POST['acf'][$k])) {
continue;
}
acf_add_local_field($field);
}
if (!empty($_POST['acf']['_validate_email'])) {
acf_add_validation_error('', __('Spam Detected', 'acf'));
}
}
function pre_save_post($post_id, $form)
{
$save = array('ID' => 0);
if (is_numeric($post_id)) {
$save['ID'] = $post_id;
} elseif ($post_id == 'new_post') {
$save = array_merge($save, $form['new_post']);
} else {
return $post_id;
}
if (isset($_POST['acf']['_post_title'])) {
$save['post_title'] = acf_extract_var($_POST['acf'], '_post_title');
}
if (isset($_POST['acf']['_post_content'])) {
$save['post_content'] = acf_extract_var($_POST['acf'], '_post_content');
}
if (!empty($_POST['acf']['_validate_email'])) {
return false;
}
if (count($save) == 1) {
return $post_id;
}
if ($save['ID']) {
wp_update_post($save);
} else {
$post_id = wp_insert_post($save);
}
return $post_id;
}
function enqueue_form()
{
$this->check_submit_form();
acf_enqueue_scripts();
}
function check_submit_form()
{
if (!acf_verify_nonce('acf_form')) {
return false;
}
if (!isset($_POST['_acf_form'])) {
return false;
}
$form = $this->get_form($_POST['_acf_form']);
if (!$form) {
$form = json_decode(acf_decrypt($_POST['_acf_form']), true);
if (!$form) {
return false;
}
}
if ($form['kses'] && isset($_POST['acf'])) {
$_POST['acf'] = wp_kses_post_deep($_POST['acf']);
}
acf_validate_save_post(true);
$this->submit_form($form);
}
function submit_form($form)
{
$form = apply_filters('acf/pre_submit_form', $form);
$post_id = acf_maybe_get($form, 'post_id', 0);
$GLOBALS['acf_form'] = $form;
$post_id = apply_filters('acf/pre_save_post', $post_id, $form);
acf_save_post($post_id);
$form = $GLOBALS['acf_form'];
do_action('acf/submit_form', $form, $post_id);
$return = acf_maybe_get($form, 'return', '');
if ($return) {
$return = str_replace('%post_id%', $post_id, $return);
$return = str_replace('%post_url%', get_permalink($post_id), $return);
wp_redirect($return);
exit;
}
}
function render_form($args = array())
{
$is_registered = false;
$field_groups = array();
$fields = array();
if (is_array($args)) {
$args = $this->validate_form($args);
} else {
$is_registered = true;
$args = $this->get_form($args);
if (!$args) {
return false;
}
}
$post_id = $args['post_id'];
if ($post_id === 'new_post') {
$post_id = false;
}
acf_update_setting('uploader', $args['uploader']);
foreach ($this->fields as $k => $field) {
acf_add_local_field($field);
}
if ($args['post_title']) {
$_post_title = acf_get_field('_post_title');
$_post_title['value'] = $post_id ? get_post_field('post_title', $post_id) : '';
$fields[] = $_post_title;
}
if ($args['post_content']) {
$_post_content = acf_get_field('_post_content');
$_post_content['value'] = $post_id ? get_post_field('post_content', $post_id) : '';
$fields[] = $_post_content;
}
if ($args['fields']) {
foreach ($args['fields'] as $selector) {
$fields[] = acf_maybe_get_field($selector, $post_id, false);
}
} elseif ($args['field_groups']) {
foreach ($args['field_groups'] as $selector) {
$field_groups[] = acf_get_field_group($selector);
}
} elseif ($args['post_id'] == 'new_post') {
$field_groups = acf_get_field_groups($args['new_post']);
} else {
$field_groups = acf_get_field_groups(array('post_id' => $args['post_id']));
}
if ($field_groups) {
foreach ($field_groups as $field_group) {
$_fields = acf_get_fields($field_group);
if ($_fields) {
foreach ($_fields as $_field) {
$fields[] = $_field;
}
}
}
}
if ($args['honeypot']) {
$fields[] = acf_get_field('_validate_email');
}
if (!empty($_GET['updated']) && $args['updated_message']) {
printf($args['html_updated_message'], $args['updated_message']);
}
if ($args['form']) {
?>
<form <?php
echo acf_esc_attrs($args['form_attributes']);
?>>
<?php
}
acf_form_data(array('screen' => 'acf_form', 'post_id' => $args['post_id'], 'form' => $is_registered ? $args['id'] : acf_encrypt(json_encode($args))));
?>
<div class="acf-fields acf-form-fields -<?php
echo esc_attr($args['label_placement']);
?>">
<?php
echo $args['html_before_fields'];
?>
<?php
acf_render_fields($fields, $post_id, $args['field_el'], $args['instruction_placement']);
?>
<?php
echo $args['html_after_fields'];
?>
</div>
<?php
if ($args['form']) {
?>
<div class="acf-form-submit">
<?php
printf($args['html_submit_button'], $args['submit_value']);
?>
<?php
echo $args['html_submit_spinner'];
?>
</div>
</form>
<?php
}
}
}
acf()->form_front = new acf_form_front();
}
function acf_form_head()
{
acf()->form_front->enqueue_form();
}
function acf_form($args = array())
{
acf()->form_front->render_form($args);
}
function acf_get_form($id = '')
{
return acf()->form_front->get_form($id);
}
function acf_register_form($args)
{
acf()->form_front->add_form($args);
}