post_type ) ) {
return;
}
if ( 'auto-draft' == $post->post_status ) {
$all_fields = get_option ( 'wppb_manage_fields' );
$all_fields_new = array();
foreach ( $all_fields as $key => $value )
array_push( $all_fields_new, array( 'field' => wppb_field_format( $value['field-title'], $value['field'] ), 'id' => $value['id'] ) );
if ( 'wppb-rf-cpt' == $post->post_type ) {
// Remove "Display name publicly as" from register forms
foreach( $all_fields_new as $key => $value ) {
if( strpos( $value['field'], 'Display name publicly as' ) !== false ) {
unset( $all_fields_new[$key] );
}
}
$all_fields_new = array_values( $all_fields_new );
add_post_meta( $post->ID, 'wppb_rf_fields', $all_fields_new );
} elseif ( 'wppb-epf-cpt' == $post->post_type ){
// remove reCAPTCHA and Validation fields from the list
foreach ( $all_fields_new as $key => $value ){
if ( strpos ( $value['field'], '( reCAPTCHA )' ) !== false || strpos ( $value['field'], '( Validation )' ) !== false ) {
unset( $all_fields_new[$key] );
}
}
$all_fields_new = array_values( $all_fields_new );
add_post_meta( $post->ID, 'wppb_epf_fields', $all_fields_new );
}
}
}
add_action( 'wp_insert_post', 'wppb_add_prepopulated_default_fields', 10, 2 );
/**
* Function that adds the internal ID of the given field on the RF and EPF CPT pages, via AJAX
*
* @since v.2.0
*
* @return void
*/
function wppb_handle_rf_epf_id_change(){
// Only form editors should be able to resolve a field label to its global id.
check_ajax_referer( 'wppb-epf-rf-id-change' );
if( ! current_user_can( 'manage_options' ) )
die('');
if( !isset( $_POST['field'] ) || $_POST['field'] == '' )
die('');
$all_fields = get_option ( 'wppb_manage_fields', 'not_set' );
if ( ( $all_fields != 'not_set' ) && ( ( is_array( $all_fields ) ) && ( !empty( $all_fields ) ) ) ){
foreach ( $all_fields as $key => $value ){
if( wppb_field_format( $value['field-title'], $value['field'] ) == stripslashes( sanitize_text_field( $_POST['field'] ) ) ){
die( (string)$value['id'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
} else
die('');
}
add_action( 'wp_ajax_wppb_handle_rf_epf_id_change', 'wppb_handle_rf_epf_id_change' );
/**
* Function that iterates in both the EPF and RF CTP meta's on manage fields update
*
* @since v.2.0
*
* @param string $meta
* @param integer $id
* @param array $array_after_update
* @param integer $element_id
*
* @return void
*/
function wppb_check_epf_rf_cptpms_update( $ep_r_posts, $cpt, $cpt_meta, $internal_id, $array_after_update ){
foreach ( $ep_r_posts as $key => $value ){
if ( $value->post_type == $cpt ){
$post_meta = get_post_meta( $value->ID, $cpt_meta, true );
if ( !empty( $post_meta ) ){
foreach ( $post_meta as $this_post_meta_key => $this_post_meta_value ){
if ( $this_post_meta_value['id'] == $internal_id ){
$post_meta[$this_post_meta_key]['field'] = wppb_field_format( $array_after_update['field-title'], $array_after_update['field'] );
}
}
update_post_meta( $value->ID, $cpt_meta, $post_meta );
}
}
}
}
/**
* Function that checks if, after an update, a change has been made to the lists, and if we need to make those changes to all the postmeta's also
*
* @since v.2.0
*
* @param string $meta
* @param integer $id
* @param array $array_after_update
* @param integer $element_id
*
* @return void
*/
function wppb_fields_list_update( $meta, $id, $array_after_update, $element_id ){
if ( $meta == 'wppb_manage_fields' ){
$all_fields = get_option ( $meta );
$array_before_update = $all_fields[$element_id];
if ( ( trim( $array_before_update['field'] ) != trim( $array_after_update['field'] ) ) || ( trim( $array_before_update['field-title'] ) != trim( $array_after_update['field-title'] ) ) ){
$ep_r_posts = get_posts( array( 'posts_per_page' => -1, 'post_status' => apply_filters ( 'wppb_get_ep_r_posts', array( 'publish', 'pending', 'draft', 'future', 'private', 'trash' ) ), 'post_type' => array( 'wppb-epf-cpt', 'wppb-rf-cpt' ) ) );
wppb_check_epf_rf_cptpms_update( $ep_r_posts, 'wppb-rf-cpt', 'wppb_rf_fields', $array_before_update['id'], $array_after_update );
wppb_check_epf_rf_cptpms_update( $ep_r_posts, 'wppb-epf-cpt', 'wppb_epf_fields', $array_after_update['id'], $array_after_update );
}
}
}
add_action ( 'wck_before_update_meta', 'wppb_fields_list_update', 10, 4 );
/**
* Function that queries the 2 CPTs and checks whether or not the given cpt has <= 1 posts added
*
* @since v.2.0
*
* @return array
*/
function wppb_get_post_number ( $cpt, $action ){
$this_cpt = get_posts( array( 'posts_per_page' => -1, 'post_status' => apply_filters ( 'wppb_check_'.$cpt.$action, array( 'any' ) ) , 'post_type' => $cpt ) );
if ( count( $this_cpt ) <= 1 )
return true;
else
return false;
}
/**
* Function that prevents users to submit forms without a form title.
*
*/
function wppb_multiple_forms_publish_admin_hook(){
global $post;
if ( is_admin() && ( ( $post->post_type == 'wppb-epf-cpt' ) || ( $post->post_type == 'wppb-rf-cpt' ) ) ){
?>
'rf', 'edit_forms' => 'epf' );
foreach( $form_types as $form_type ){
$args = array(
'post_type' => 'wppb-'.$form_type.'-cpt',
'numberposts' => -1,
'posts_per_page' => -1,
'post_status' => 'any'
);
$all_forms = get_posts( $args );
if( !empty( $all_forms ) ){
foreach( $all_forms as $form ){
$fields_in_form = get_post_meta( $form->ID, 'wppb_'.$form_type.'_fields', true );
$delete_this_key = '';
if( !empty( $fields_in_form ) ){
foreach( $fields_in_form as $key => $form_field ){
if( $form_field['id'] == $deleted_element_id ){
$delete_this_key = $key;
break;
}
}
if( !empty( $delete_this_key ) ){
unset( $fields_in_form[$delete_this_key] );
$fields_in_form = array_values( $fields_in_form );
update_post_meta( $form->ID, 'wppb_'.$form_type.'_fields', $fields_in_form );
}
}
}
}
}
}
}
/**
* Function that adds the data-id attribute to each option of the add new field to the list select drop-down
*
* @since v.2.0.2
*
* @return void
*/
function wppb_rf_epf_set_field_ids_on_field_select() {
global $all_fields;
$all_fields = get_option ( 'wppb_manage_fields', 'not_set' );
// remove certain fields from the Field drop-down on edit profile form
if( ( isset( $_GET['post_type'] ) && $_GET['post_type'] == 'wppb-epf-cpt' ) || ( isset( $_GET['post'] ) && get_post_type( absint( $_GET['post'] ) ) == 'wppb-epf-cpt' ) || ( isset( $_POST['meta'] ) && $_POST['meta'] == 'wppb_epf_fields' ) ) {
foreach( $all_fields as $key => $field ) {
$unwanted_fields = array( 'reCAPTCHA', 'Validation' );
if( in_array( $field['field'], $unwanted_fields) ) {
unset( $all_fields[$key] );
}
}
$all_fields = array_values( $all_fields );
}
// remove certain fields from the Field drop-down on register form
if( ( isset( $_GET['post_type'] ) && $_GET['post_type'] == 'wppb-rf-cpt' ) || ( isset( $_GET['post'] ) && get_post_type( absint( $_GET['post'] ) ) == 'wppb-rf-cpt' ) || ( isset( $_POST['meta'] ) && $_POST['meta'] == 'wppb_rf_fields' ) ) {
foreach( $all_fields as $key => $field ) {
if( $field['field'] == 'Default - Display name publicly as' ) {
unset( $all_fields[$key] );
}
}
$all_fields = array_values( $all_fields );
}
if( $all_fields !== 'not_set ') {
if( !function_exists( 'wppb_rf_epf_set_field_id_select_option' ) ) {
function wppb_rf_epf_set_field_id_select_option( $content, $field_id ){
global $all_fields;
$output = str_replace( '