Last active
August 30, 2022 17:16
-
-
Save wpmudev-sls/d63c187b44af6274ba653d9e615d7a06 to your computer and use it in GitHub Desktop.
[Forminator] - Custom Post Data Field Type
This file contains hidden or 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 | |
/** | |
* Plugin Name: [Forminator] - Custom Post Data Field Type | |
* Description: [Forminator] - Custom Post Data Field Type - change content field type from wp-editor to textarea and add support advanced custom field (text, url, number, email type). | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
add_action( 'plugins_loaded', 'wpmudev_forminator_custom_post_data_fields_func', 100 ); | |
function wpmudev_forminator_custom_post_data_fields_func() { | |
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) { | |
class WPMUDEV_FM_Custom_Postdata_Field{ | |
private $using_acf; | |
private $acf_fields = []; | |
public function __construct(){ | |
add_action( 'forminator_after_form_render', array( $this, 'custom_scripts_after_form' ) ); | |
add_action( 'forminator_before_field_render', array( $this, 'before_render_postdata' ) ); | |
add_filter( 'forminator_field_postdata_markup', array( $this, 'field_postdata_markup' ) ); | |
add_filter( 'forminator_field_create_input', array( $this, 'custom_postdata_custom_field' ), 10, 4 ); | |
add_filter( 'forminator_field_create_wp_editor', array( $this, 'field_textarea' ), 10, 4 ); | |
add_filter('get_post_metadata', array( $this, 'custom_meta_value_forminator_form_meta' ), 10, 3); | |
// fix the custom form not update custom fields | |
add_action( 'forminator_post_data_field_post_saved', array( $this, 'save_custom_fields_data' ), 10, 3 ); | |
$this->using_acf = class_exists('acf'); | |
} | |
public function custom_scripts_after_form(){ | |
$custom_styles = '<style> | |
.wpmudev-fm-separated-postdata-field .forminator-row--inner{ | |
display:block !important; | |
} | |
.wpmudev-fm-separated-postdata-field .forminator-row--inner .forminator-col{ | |
margin-bottom:30px!important; | |
} | |
</style>'; | |
echo $custom_styles; | |
} | |
public function before_render_postdata( $field ){ | |
if( $this->using_acf && isset( $field['type'] ) && 'postdata' === $field['type'] ){ | |
$groups = acf_get_field_groups(array('post_type' => $field['post_type'])); | |
if( $groups ){ | |
foreach( $groups as $group ){ | |
$fields = acf_get_fields($group['key']); | |
if( $fields ){ | |
foreach( $fields as $field ){ | |
$this->acf_fields[ $field['name'] ] = $field; | |
} | |
} | |
} | |
} | |
} | |
} | |
public function field_postdata_markup( $html ){ | |
// $this->post_type = $field_obj->field['post_type']; | |
$html = str_replace('forminator-col forminator-col', 'forminator-col forminator-field forminator-col', $html ); | |
return $html; | |
} | |
public function custom_postdata_custom_field( $html, $attr, $label, $description ){ | |
static $current_field; | |
if( ! $this->using_acf || $current_field === $attr['id'] ) return $html; | |
if( $pos = strpos( $attr['id'], '-post_meta-') ){ | |
$current_field = $attr['id']; | |
$field_key = substr( $attr['id'], $pos + 11); | |
$support_field_types = ['text','url','number','email','date_picker']; | |
if( isset( $this->acf_fields[ $field_key ] ) && in_array( $this->acf_fields[ $field_key ]['type'], $support_field_types ) ){ | |
$acf_field = $this->acf_fields[ $field_key ]; | |
$attr['type'] = $acf_field['type']; | |
if( isset( $acf_field['placeholder'] ) ){ | |
$attr['placeholder'] = $acf_field['placeholder']; | |
}else{ | |
$attr['placeholder'] = $label; | |
} | |
if( $acf_field['class'] ){ | |
$attr['class'] .= ' '. $acf_field['class']; | |
} | |
$icon_markup = false; | |
$required = false; | |
if( ! empty( $acf_field['required'] ) ){ | |
$attr['required'] = 'true'; | |
$required = true; | |
} | |
if( ! empty( $acf_field['default_value'] ) ){ | |
$attr['value'] = $acf_field['default_value']; | |
} | |
if( 'number' === $attr['type'] ){ | |
if( '' !== $acf_field['min'] ){ | |
$attr['min'] = $acf_field['min']; | |
} | |
if( '' !== $acf_field['max'] ){ | |
$attr['max'] = $acf_field['max']; | |
} | |
}elseif( 'date_picker' === $attr['type'] ){ | |
// check Forminator_Date::markup for detail | |
$icon_markup = true; | |
$attr['type'] = 'text'; | |
$attr['size'] = 1; | |
$datepicker_format = ! empty( $acf_field['display_format'] ) ? $acf_field['display_format'] : 'm/d/Y'; | |
$date_format = acf_convert_date_to_js( $datepicker_format ); | |
$default_value = date( $datepicker_format ); | |
$attr['value'] = $default_value; | |
// $attr['placeholder'] = $placeholder; | |
// $attr['id'] = 'forminator-field-' . $id . '-picker-' . uniqid(); | |
$attr['class' ] .= ' forminator-datepicker'; | |
$attr['data-required' ] = $required; | |
$attr['data-format' ] = $date_format; | |
$restrict_type = ''; //null || week || custom | |
$attr['data-restrict-type'] = $restrict_type; | |
$restrict = array(); | |
// if ( 'week' === $restrict_type ) { | |
// $days = forminator_week_days(); | |
// $i = 0; | |
// foreach ( $days as $k => $day ) { | |
// if ( ! self::get_property( $k, $field ) ) { | |
// $restrict[] = $i; | |
// } | |
// $i ++; | |
// } | |
// } elseif ( 'custom' === $restrict_type ) { | |
// $dates = self::get_property( 'date_multiple', $field ); | |
// if ( ! empty( $dates ) ) { | |
// foreach ( $dates as $k => $date ) { | |
// $restrict[] = $date['value']; | |
// } | |
// } | |
// } | |
$attr['data-restrict'] = implode( ";", $restrict ); | |
$current_year = date('Y'); | |
$attr['data-start-year'] = $current_year - 100; | |
$attr['data-end-year'] = $current_year + 100; | |
} | |
$design = ''; | |
$html = Forminator_Field::create_input( | |
$attr, | |
$label, | |
$description, | |
$required, | |
$design, | |
$icon_markup | |
); | |
}else{ | |
switch ( $field_key ) { | |
case 'your_url_field_key': | |
$required = 'false'; | |
$attr = array( | |
'type' => 'url', | |
'name' => $attr['name'], | |
'value' => $attr['value'], | |
'placeholder' => $label, | |
'id' => $attr['id'], | |
'class' => 'forminator-input forminator-website--field', | |
'required' => $required | |
); | |
$html = Forminator_Field::create_input( | |
$attr, | |
$label, | |
$description, | |
(bool) $required | |
); | |
break; | |
default: | |
# code... | |
break; | |
} | |
} | |
} | |
return $html; | |
} | |
public static function normalize_date_format( $date_format ) { | |
$date_format = str_replace( 'dd', 'd', $date_format ); | |
$date_format = str_replace( 'mm', 'm', $date_format ); | |
$date_format = str_replace( 'yy', 'Y', $date_format ); | |
return $date_format; | |
} | |
public function field_textarea( $html, $attr, $label, $description ){ | |
if( isset( $attr['type'] ) && 'wp_editor' === $attr['type'] ){ | |
$required = !empty( $attr['required'] ) ? true : false; | |
$html = Forminator_Postdata::create_textarea( | |
$attr, | |
$label, | |
$description, | |
$required | |
); | |
} | |
return $html; | |
} | |
public function custom_meta_value_forminator_form_meta( $check, $object_id, $meta_key ){ | |
if( 'forminator_form_meta' !== $meta_key ){ | |
return $check; | |
} | |
$meta_type = 'post'; | |
$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' ); | |
if ( ! $meta_cache ) { | |
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); | |
$meta_cache = $meta_cache[ $object_id ]; | |
} | |
if ( ! $meta_key ) { | |
return $meta_cache; | |
} | |
if ( isset( $meta_cache[ $meta_key ] ) ) { | |
$meta_value = maybe_unserialize( $meta_cache[ $meta_key ][0] ); | |
if( ! empty( $meta_value['fields'] ) ){ | |
foreach( $meta_value['fields'] as $k => $field ){ | |
if( isset( $field['type'] ) && 'postdata' === $field['type'] ){ | |
if( ! empty( $field['options'] ) && ! empty( $field['post_custom_fields'] ) ){ | |
$field['post_custom'] = $field['post_custom_fields']; | |
$field['custom_vars'] = $field['options']; | |
$meta_value['fields'][ $k ] = $field; | |
} | |
} | |
} | |
} | |
return [$meta_value]; | |
} | |
return ''; | |
} | |
public function save_custom_fields_data( $post_id, $field, $data ){ | |
if( isset( $field['custom_vars'], $data['post-custom'] ) ){ | |
$values1 = wp_list_pluck( $field['custom_vars'], 'value' ); | |
$values2 = wp_list_pluck( $data['post-custom'], 'value' ); | |
if( $values1 && $values2 ){ | |
if( $values1 !== $values2 ){ | |
$values1 = array_map('trim', $values1); | |
} | |
if( $values1 === $values2 ){ | |
$field_id = Forminator_Field::get_property( 'element_id', $field ); | |
foreach( $data['post-custom'] as $key => $meta_field ){ | |
if( empty( $meta_field['value'] ) ){ | |
$meta_field['value'] = sanitize_title( $meta_field['key'] ); | |
} | |
$mod_field_id = $field_id .'-post_meta-'. $meta_field['value']; | |
if( isset( $_POST[ $mod_field_id ] ) ){ | |
if( $meta_field['key'] ){ | |
delete_post_meta( $post_id, $meta_field['key'], $data['post-custom'][ $key ]['value'] ); | |
} | |
// check advanced custom field first | |
if( $this->using_acf && $field_obj = get_field_object( $meta_field['value'], $post_id ) && $field_obj->ID ){ | |
if( 'date_picker' === $field_obj->type ){ | |
$_POST[ $mod_field_id ] = acf_format_date( $_POST[ $mod_field_id ], 'Ymd' ); | |
} | |
update_field($meta_field['value'], $_POST[ $mod_field_id ], $post_id); | |
}else{ | |
update_post_meta( $post_id, $meta_field['value'], $_POST[ $mod_field_id ] ); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
$run = new WPMUDEV_FM_Custom_Postdata_Field(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment