public function dateLocales($a = null, $b = null, $c = null) { MyZebra_DateParser::setDateLocaleStrings($a, $b, $c); return $this; }
public function extractCustomFields($post_id, $track = false) { global $user_ID; // reference to the form submission method global ${'_' . StaticClass::METHOD}; $method =& ${'_' . StaticClass::METHOD}; // get refs here $globals =& self::friendGetStatic('CRED_Form_Builder', '&_staticGlobal'); $form =& $this->friendGet($this->_formBuilder, '&_formData'); $out_ =& $this->friendGet($this->_formBuilder, '&out_'); $form_id = $form->getForm()->ID; $form_fields = $form->getFields(); $form_type = $form_fields['form_settings']->form['type']; $post_type = $form_fields['form_settings']->post['post_type']; $supported_date_formats = $this->friendGet($this->_formBuilder, '_supportedDateFormats'); $_fields = $out_['fields']; $_form_fields = $out_['form_fields']; $_form_fields_info = $out_['form_fields_info']; $zebraForm = $this->friendGet($this->_formBuilder, '_zebraForm'); // custom fields $fields = array(); $removed_fields = array(); // taxonomies $taxonomies = array('flat' => array(), 'hierarchical' => array()); $fieldsInfo = array(); // files, require extra care to upload correctly $files = array(); foreach ($_fields['post_fields'] as $key => $field) { $field_label = $field['name']; $done_data = false; // use the key as was rendered (with potential prefix) $key11 = $key; if (isset($field['plugin_type_prefix'])) { $key = $field['plugin_type_prefix'] . $key; } // if this field was not rendered in this specific form, bypass it if (!array_key_exists($key11, $_form_fields)) { continue; } // if this field was discarded due to some conditional logic, bypass it if (isset($zebraForm->controls) && $zebraForm->controls[$_form_fields[$key11][0]]->isDiscarded()) { continue; } $fieldsInfo[$key] = array('save_single' => false); if ('audio' == $field['type'] || 'video' == $field['type'] || 'file' == $field['type'] || 'image' == $field['type']) { if (!array_key_exists($key, $method)) { // remove the fields $removed_fields[] = $key; unset($fieldsInfo[$key]); } else { /* $_hasFile=false; foreach ($method[$key] as $_file_) { if (''!=trim($method[$key])) $_hasFile=true; } // repetitive field and all values are empty, remove if (!$_hasFile) { // remove the fields $removed_fields[]=$key; unset($fieldsInfo[$key]); } */ $fields[$key] = $method[$key]; } } if ('checkboxes' == $field['type'] && isset($field['data']['save_empty']) && 'yes' == $field['data']['save_empty'] && !array_key_exists($key, $method)) { $values = array(); foreach ($field['data']['options'] as $optionkey => $optiondata) { $values[$optionkey] = '0'; } // let model serialize once, fix Types-CRED mapping issue with checkboxes $fieldsInfo[$key]['save_single'] = true; $fields[$key] = $values; } elseif ('checkboxes' == $field['type'] && (!isset($field['data']['save_empty']) || 'yes' != $field['data']['save_empty']) && !array_key_exists($key, $method)) { // remove the fields $removed_fields[] = $key; unset($fieldsInfo[$key]); } elseif ('checkbox' == $field['type'] && isset($field['data']['save_empty']) && 'yes' == $field['data']['save_empty'] && !array_key_exists($key, $method)) { $fields[$key] = '0'; } elseif ('checkbox' == $field['type'] && (!isset($field['data']['save_empty']) || 'yes' != $field['data']['save_empty']) && !array_key_exists($key, $method)) { // remove the fields $removed_fields[] = $key; unset($fieldsInfo[$key]); } elseif (array_key_exists($key, $method)) { // normalize repetitive values out of sequence // NOTE this seems deprecated as we are using the method above... why is this still here? if ($_form_fields_info[$key11]['repetitive']) { if (is_array($method[$key])) { $values = array_values($method[$key]); } else { $aux_value_array = array($method[$key]); $values = array_values($aux_value_array); } } else { $values = $method[$key]; } if ('audio' == $field['type'] || 'video' == $field['type'] || 'file' == $field['type'] || 'image' == $field['type']) { $files[$key] = $zebraForm->controls[$_form_fields[$key11][0]]->get_values(); //cred_log($files[$key]); $files[$key]['name_orig'] = $key11; $files[$key]['label'] = $field['name']; $files[$key]['repetitive'] = $_form_fields_info[$key11]['repetitive']; } elseif ('textarea' == $field['type'] || 'wysiwyg' == $field['type']) { // stripslashes for textarea, wysiwyg fields if (is_array($values)) { $values = array_map('stripslashes', $values); } else { $values = stripslashes($values); } } elseif ('textfield' == $field['type'] || 'text' == $field['type'] || 'date' == $field['type']) { // stripslashes for text fields if (is_array($values)) { $values = array_map('stripslashes', $values); } else { $values = stripslashes($values); } } // track form data for notification mail if ($track) { $tmp_data = null; if ('checkbox' == $field['type']) { if ('db' == $field['data']['display']) { $tmp_data = $values; } else { $tmp_data = $field['data']['display_value_selected']; } } elseif ('radio' == $field['type'] || 'select' == $field['type']) { $tmp_data = $field['data']['options'][$values]['title']; } elseif ('checkboxes' == $field['type'] || 'multiselect' == $field['type']) { $tmp_data = array(); foreach ($values as $tmp_val) { $tmp_data[] = $field['data']['options'][$tmp_val]['title']; } //$tmp_data=implode(', ',$tmp_data); unset($tmp_val); } if (null !== $tmp_data) { $this->trackData(array($field_label => $tmp_data)); $done_data = true; } } if ('checkboxes' == $field['type'] || 'multiselect' == $field['type']) { $result = array(); foreach ($field['data']['options'] as $optionkey => $optiondata) { /* if ( isset($field['data']['save_empty']) && 'yes'==$field['data']['save_empty'] && !in_array($optionkey, $values) ) $result[$optionkey]='0'; else */ if (in_array($optionkey, $values) && isset($optiondata['set_value'])) { $result[$optionkey] = $optiondata['set_value']; } } $values = $result; $fieldsInfo[$key]['save_single'] = true; } elseif ('radio' == $field['type'] || 'select' == $field['type']) { $values = $field['data']['options'][$values]['value']; } elseif ('date' == $field['type']) { $date_format = null; if (isset($field['data']) && isset($field['data']['validate'])) { $date_format = $field['data']['validate']['date']['format']; } if (!in_array($date_format, $supported_date_formats)) { $date_format = 'F j, Y'; } if (!is_array($values)) { $tmp = array($values); } else { $tmp = $values; } // track form data for notification mail if ($track) { $this->trackData(array($field_label => $tmp)); $done_data = true; } MyZebra_DateParser::setDateLocaleStrings($globals['LOCALES']['days'], $globals['LOCALES']['months']); foreach ($tmp as $ii => $val) { $val = MyZebra_DateParser::parseDate($val, $date_format); if (false !== $val) { // succesfull $val = $val->getNormalizedTimestamp(); } else { continue; } $tmp[$ii] = $val; } if (!is_array($values)) { $values = $tmp[0]; } else { $values = $tmp; } } elseif ('skype' == $field['type']) { if (array_key_exists('skypename', $values) && array_key_exists('style', $values)) { $new_values = array(); $values['skypename'] = (array) $values['skypename']; $values['style'] = (array) $values['style']; foreach ($values['skypename'] as $ii => $val) { $new_values[] = array('skypename' => $values['skypename'][$ii], 'style' => $values['style'][$ii]); } $values = $new_values; unset($new_values); if ($track) { $this->trackData(array($field_label => $values)); $done_data = true; } } } // dont track file/image data now but after we upload them.. if ($track && !$done_data && 'file' != $field['type'] && 'image' != $field['type']) { $this->trackData(array($field_label => $values)); } $fields[$key] = $values; } } // custom parents (Types feature) foreach ($_fields['parents'] as $key => $field) { $field_label = $field['name']; // overwrite parent setting by url, even though no fields might be set if (!array_key_exists($key, $_form_fields) && array_key_exists('parent_' . $field['data']['post_type'] . '_id', $_GET) && is_numeric($_GET['parent_' . $field['data']['post_type'] . '_id'])) { $fieldsInfo[$key] = array('save_single' => false); $fields[$key] = intval($_GET['parent_' . $field['data']['post_type'] . '_id']); continue; } // if this field was not rendered in this specific form, bypass it if (!array_key_exists($key, $_form_fields)) { continue; } // if this field was discarded due to some conditional logic, bypass it if ($zebraForm->controls[$_form_fields[$key][0]]->isDiscarded()) { continue; } if (array_key_exists($key, $method) && intval($method[$key]) >= -1) { $fieldsInfo[$key] = array('save_single' => false); $fields[$key] = intval($method[$key]); } } // taxonomies foreach ($_fields['taxonomies'] as $key => $field) { // if this field was not rendered in this specific form, bypass it if (!array_key_exists($key, $_form_fields)) { continue; } if (array_key_exists($key, $method) || $field['hierarchical'] && isset($method[$key . '_hierarchy'])) { if ($field['hierarchical']) { $values = isset($method[$key]) ? $method[$key] : array(); if (isset($method[$key . '_hierarchy'])) { $add_new = array(); preg_match_all("/\\{([^\\{\\}]+?),([^\\{\\}]+?)\\}/", $method[$key . '_hierarchy'], $tmp_a_n); for ($ii = 0; $ii < count($tmp_a_n[1]); $ii++) { $add_new[] = array('parent' => $tmp_a_n[1][$ii], 'term' => $tmp_a_n[2][$ii]); } unset($tmp_a_n); } else { $add_new = array(); } $taxonomies['hierarchical'][] = array('name' => $key, 'terms' => $values, 'add_new' => $add_new); // track form data for notification mail if ($track) { $tmp_data = array(); foreach ($field['all'] as $tmp_tax) { //if (in_array($tmp_tax['term_taxonomy_id'],$values)) if (in_array($tmp_tax['term_id'], $values)) { $tmp_data[] = $tmp_tax['name']; } } // add also new terms created foreach ($values as $val) { if (is_string($val) && !is_numeric($val)) { $tmp_data[] = $val; } } $this->trackData(array($field['label'] => $tmp_data)); unset($tmp_data); } } elseif (!$field['hierarchical']) { $values = $method[$key]; // find which to add and which to remove $tax_add = $values; $tax_remove = ""; // allow white space in tax terms $taxonomies['flat'][] = array('name' => $key, 'add' => $tax_add, 'remove' => $tax_remove); // track form data for notification mail if ($track) { $this->trackData(array($field['label'] => array('added' => $tax_add, 'removed' => $tax_remove))); } } } } return array($fields, $fieldsInfo, $taxonomies, $files, $removed_fields); }
private function translate_field($name, &$field, $additional_options = array()) { // extend additional_options with defaults extract(array_merge(array('preset_value' => null, 'placeholder' => null, 'value_escape' => false, 'make_readonly' => false, 'is_tax' => false, 'max_width' => null, 'max_height' => null, 'single_select' => false), $additional_options)); // add the "name" element // the "&" symbol is there so that $obj will be a reference to the object in PHP 4 // for PHP 5+ there is no need for it $type = 'text'; $attributes = array(); $value = ''; $name_orig = $name; if (!$is_tax) { if ($placeholder && $placeholder !== null && !empty($placeholder) && is_string($placeholder)) { // use translated value by WPML if exists $placeholder = cred_translate('Value: ' . $placeholder, $placeholder, 'cred-form-' . $this->_form->form->post_title . '-' . $this->_form->form->ID); } if ($preset_value && $preset_value !== null && is_string($preset_value) && !empty($preset_value)) { // use translated value by WPML if exists $data_value = cred_translate('Value: ' . $preset_value, $preset_value, 'cred-form-' . $this->_form->form->post_title . '-' . $this->_form->form->ID); } elseif ($this->_post_data && isset($this->_post_data['fields'][$name_orig])) { $data_value = $this->_post_data['fields'][$name_orig][0]; } else { $data_value = null; } $value = ''; // save a map between options / actual values for these types to be used later if ($field['type'] == 'checkboxes' || $field['type'] == 'radio' || $field['type'] == 'select' || $field['type'] == 'multiselect') { $tmp = array(); foreach ($field['data']['options'] as $optionKey => $optionData) { if ($optionKey != 'default' && is_array($optionData)) { $tmp[$optionKey] = $field['type'] == 'checkboxes' ? $optionData['set_value'] : $optionData['value']; } } $this->_field_values_map[$field['slug']] = $tmp; unset($tmp); unset($optionKey); unset($optionData); } switch ($field['type']) { case 'form_messages': $type = 'messages'; break; case 'recaptcha': $type = 'recaptcha'; $value = ''; $attributes = array('error_message' => $this->getLocalisedMessage('enter_valid_captcha'), 'show_link' => $this->getLocalisedMessage('show_captcha'), 'no_keys' => __('Enter your ReCaptcha keys at the CRED Settings page in order for ReCaptcha API to work', 'wp-cred')); if (self::$recaptcha_settings !== false) { $attributes['public_key'] = self::$recaptcha_settings['public_key']; $attributes['private_key'] = self::$recaptcha_settings['private_key']; } if ($this->_form_count == 1) { $attributes['open'] = true; } // used to load additional js script $this->hasRecaptcha = true; break; case 'file': $type = 'file'; if ($data_value !== null) { $value = $data_value; } break; case 'image': $type = 'file'; if ($data_value !== null) { $value = $data_value; } // show previous post featured image thumbnail if ('_featured_image' == $name) { $value = ''; if (isset($this->_post_data['extra']['featured_img_html'])) { $attributes['display_featured_html'] = $this->_post_data['extra']['featured_img_html']; } } break; case 'date': $type = 'date'; $format = ''; if (isset($field['data']) && isset($field['data']['validate'])) { $format = $field['data']['validate']['date']['format']; } if (!in_array($format, $this->supported_date_formats)) { $format = 'F j, Y'; } $attributes['format'] = $format; $attributes['readonly_element'] = false; //cred_log('Date time '.$data_value); if ($data_value !== null && !empty($data_value) && (is_numeric($data_value) || is_int($data_value) || is_long($data_value))) { MyZebra_DateParser::setDateLocaleStrings(self::$localized_strings['days'], self::$localized_strings['months']); //cred_log('Days '.print_r(self::$localized_strings['days'],true)); //$value = date($format,$data_value); // format localized date form timestamp $value = MyZebra_DateParser::formatDate($data_value, $format, true); //cred_log('Date value '.$value); } break; case 'select': $type = 'select'; $value = array(); $attributes = array(); $attributes['options'] = array(); $default = array(); foreach ($field['data']['options'] as $key => $option) { $index = $key; //$option['value']; if ($key == 'default') { $default[] = $option; } else { $attributes['options'][$index] = $option['title']; if ($data_value !== null && $data_value == $option['value']) { $value[] = $key; } if (isset($option['dummy']) && $option['dummy']) { $attributes['dummy'] = $key; } } } if (empty($value) && !empty($default)) { $value = $default; } if (isset($this->_field_values_map[$field['slug']])) { $attributes['actual_options'] = $this->_field_values_map[$field['slug']]; } break; case 'multiselect': $type = 'select'; $value = array(); $attributes = array(); $attributes['options'] = array(); $attributes['multiple'] = 'multiple'; $default = array(); foreach ($field['data']['options'] as $key => $option) { $index = $key; //$option['value']; if ($key == 'default') { $default = (array) $option; } else { $attributes['options'][$index] = $option['title']; if ($data_value !== null && $data_value == $option['value']) { $value[] = $key; } if (isset($option['dummy']) && $option['dummy']) { $attributes['dummy'] = $key; } } } if (empty($value) && !empty($default)) { $value = $default; } if (isset($this->_field_values_map[$field['slug']])) { $attributes['actual_options'] = $this->_field_values_map[$field['slug']]; } break; case 'radio': $type = 'radios'; $value = array(); $attributes = ''; $default = ''; foreach ($field['data']['options'] as $key => $option) { $index = $key; //$option['display_value']; if ($key == 'default') { $default = $option; } else { $value[$index] = $option['title']; if ($data_value !== null && $data_value == $option['value']) { $attributes = $key; } } } if ($data_value === null && !empty($default)) { $attributes = $default; } $def = $attributes; $attributes = array('default' => $def); if (isset($this->_field_values_map[$field['slug']])) { $attributes['actual_values'] = $this->_field_values_map[$field['slug']]; } break; case 'checkboxes': $type = 'checkboxes'; $name .= '[]'; $value = array(); $attributes = array(); if (is_array($data_value)) { $data_value = array_keys($data_value); } elseif ($data_value !== null) { $data_value = array($data_value); } foreach ($field['data']['options'] as $key => $option) { $index = $key; $value[$index] = $option['title']; if (isset($option['checked']) && $option['checked'] && $data_value === null) { $attributes[] = $index; } elseif ($data_value !== null && in_array($index, $data_value)) { $attributes[] = $index; } } $def = $attributes; $attributes = array('default' => $def); if (isset($this->_field_values_map[$field['slug']])) { $attributes['actual_values'] = $this->_field_values_map[$field['slug']]; } //print_r($attributes); //print_r($value); break; case 'checkbox': $type = 'checkbox'; $value = $field['data']['set_value']; if ($data_value !== null && $data_value == $value) { $attributes = array('checked' => 'checked'); } break; case 'textarea': $type = 'textarea'; if ($data_value !== null) { $value = $data_value; } break; case 'wysiwyg': $type = 'wysiwyg'; if ($data_value !== null) { $value = $data_value; } $attributes = array('disable_xss_filters' => true); if ($name == 'post_content' && isset($this->_form->fields['form_settings']->has_media_button) && $this->_form->fields['form_settings']->has_media_button) { $attributes['has_media_button'] = true; } break; case 'form_submit': $type = 'submit'; if ($data_value !== null) { $value = $data_value; } break; case 'numeric': $type = 'text'; if ($data_value !== null) { $value = $data_value; } break; case 'phone': $type = 'text'; if ($data_value !== null) { $value = $data_value; } break; case 'url': $type = 'text'; if ($data_value !== null) { $value = $data_value; } break; case 'email': $type = 'text'; if ($data_value !== null) { $value = $data_value; } break; case 'textfield': $type = 'text'; if ($data_value !== null) { $value = $data_value; } if ($placeholder && null !== $placeholder && !empty($placeholder)) { $attributes['placeholder'] = $placeholder; } break; case 'password': $type = 'password'; if ($data_value !== null) { $value = $data_value; } if ($placeholder && null !== $placeholder && !empty($placeholder)) { $attributes['placeholder'] = $placeholder; } break; case 'hidden': $type = 'hidden'; if ($data_value !== null) { $value = $data_value; } break; case 'skype': $type = 'skype'; if ($data_value !== null && is_string($data_value)) { $data_value = array('skypename' => $data_value, 'style' => ''); } if ($data_value !== null) { $value = $data_value; } else { $value = array('skypename' => '', 'style' => ''); } $attributes = array('ajax_url' => admin_url('admin-ajax.php'), 'edit_skype_text' => $this->getLocalisedMessage('edit_skype_button'), 'value' => $data_value, '_nonce' => wp_create_nonce('insert_skype_button')); break; default: $type = 'text'; if ($data_value !== null) { $value = $data_value; } break; } if ($make_readonly) { if (!is_array($attributes)) { $attributes = array(); } $attributes['readonly'] = 'readonly'; } // no extra escaping, just default framework XSS filter /*if ($value_escape) { if (!is_array($attributes)) $attributes=array(); $attributes['escape']=true; }*/ // repetitive field (special care) if (isset($field['data']['repetitive']) && $field['data']['repetitive'] == 1) { $name .= '[]'; $objs =& $this->_myzebra_form->add_repeatable($type, $name, $value, $attributes); if (isset($this->_post_data['fields'][$name_orig]) && count($this->_post_data['fields'][$name_orig]) > 1) { for ($ii = 1; $ii < count($this->_post_data['fields'][$name_orig]) && count($this->_post_data['fields'][$name_orig]); $ii++) { $data_value = $this->_post_data['fields'][$name_orig][$ii]; $atts = array(); switch ($type) { case 'skype': $atts = array('value' => $data_value); break; case 'date': $format = ''; if (isset($field['data']) && isset($field['data']['validate'])) { $format = $field['data']['validate']['date']['format']; } if (!in_array($format, $this->supported_date_formats)) { $format = 'F j, Y'; } $atts['format'] = $format; $atts['readonly_element'] = false; if (!empty($data_value)) { MyZebra_DateParser::setDateLocaleStrings(self::$localized_strings['days'], self::$localized_strings['months']); //$atts['value'] = date($format,$data_value); // format localized date form timestamp $atts['value'] = MyZebra_DateParser::formatDate($data_value, $format, true); //cred_log('Date value '.$atts['value']); } break; case 'file': $atts['value'] = $data_value; break; case 'text': $atts['value'] = $data_value; break; case 'wysiwyg': case 'textarea': $atts['value'] = $data_value; break; case 'checkbox': $value = $field['data']['set_value']; if ($data_value == $value) { $atts = array('checked' => 'checked'); } break; case 'select': $value = array(); foreach ($field['data']['options'] as $key => $option) { $index = $option['value']; //$option['set_value']; if ($key == 'default' && $data_value == '') { $value[] = $field['data']['options'][$option]['value']; } elseif ($data_value != '') { $value[] = $data_value; } } $atts['value'] = $value; break; default: $atts['value'] = $data_value; break; } $objs->addControl($atts); } } } else { $objs =& $this->_myzebra_form->add($type, $name, $value, $attributes); } if (!is_array($objs)) { $oob = array($objs); } else { $oob = $objs; } $ids = array(); // add validation rules if needed foreach ($oob as &$obj) { $obj->setPrimeName($name_orig); if ('hidden' == $type) { $obj->attributes['user_defined'] = true; } // field belongs to a container? if ($this->_current_group !== null) { $this->_current_group->addControl($obj); //$obj->setParent($this->_current_group); } $atts = $obj->get_attributes(array('id', 'type')); $ids[] = $atts['id']; if ($atts['type'] == 'label') { continue; } switch ($type) { case 'file': $upload = wp_upload_dir(); // set rules $obj->set_rule(array('upload' => array($upload['path'], $upload['url'], true, 'error', $this->getLocalisedMessage('upload_failed')))); $obj->set_attributes(array('external_upload' => true)); // we will handle actual upload if ($field['type'] == 'image') { // set rules $obj->set_rule(array('image' => array('error', $this->getLocalisedMessage('not_valid_image')))); } else { // if general file upload, restrict to Wordpress allowed file types $obj->set_rule(array('filetype' => array($this->wp_mimes, 'error', $this->getLocalisedMessage('file_type_not_allowed')))); } if (null !== $max_width && is_numeric($max_width)) { $max_width = intval($max_width); $obj->set_rule(array('image_max_width' => array($max_width, sprintf($this->getLocalisedMessage('image_width_larger'), $max_width)))); } if (null !== $max_height && is_numeric($max_height)) { $max_height = intval($max_height); $obj->set_rule(array('image_max_height' => array($max_height, sprintf($this->getLocalisedMessage('image_height_larger'), $max_height)))); } break; } if (isset($field['data']) && isset($field['data']['validate'])) { foreach ($field['data']['validate'] as $method => $validation) { if ($validation['active'] == 1) { switch ($method) { case 'required': // set rules $obj->set_rule(array('required' => array('error', $this->getLocalisedMessage('field_required')))); break; case 'hidden': // set rules $obj->set_rule(array('hidden' => array('error', $this->getLocalisedMessage('values_do_not_match')))); // default attribute to check against submitted value $obj->set_attributes(array('default' => $obj->attributes['value'])); break; case 'date': // set rules $obj->set_rule(array('date' => array('error', $this->getLocalisedMessage('enter_valid_date')))); break; case 'email': // set rules $obj->set_rule(array('email' => array('error', $this->getLocalisedMessage('enter_valid_email')))); break; case 'number': // set rules $obj->set_rule(array('number' => array('', 'error', $this->getLocalisedMessage('enter_valid_number')))); break; case 'image': case 'file': break; case 'url': // set rules $obj->set_rule(array('url' => array('error', $this->getLocalisedMessage('enter_valid_url')))); break; } } } } } } else { if (!array_key_exists('master_taxonomy', $field)) { if ($field['hierarchical']) { if (in_array($preset_value, array('checkbox', 'select'))) { $tax_display = $preset_value; } else { $tax_display = 'checkbox'; } } if ($this->_post_data && isset($this->_post_data['taxonomies'][$name_orig])) { if (!$field['hierarchical']) { $data_value = array('terms' => $this->_post_data['taxonomies'][$name_orig]['terms'], 'add_text' => $this->getLocalisedMessage('add_taxonomy'), 'remove_text' => $this->getLocalisedMessage('remove_taxonomy'), 'ajax_url' => admin_url('admin-ajax.php'), 'auto_suggest' => true); } else { $data_value = array('terms' => $this->_post_data['taxonomies'][$name_orig]['terms'], 'all' => $field['all'], 'type' => $tax_display, 'single_select' => $single_select); } } else { if (!$field['hierarchical']) { $data_value = array('add_text' => $this->getLocalisedMessage('add_taxonomy'), 'remove_text' => $this->getLocalisedMessage('remove_taxonomy'), 'ajax_url' => admin_url('admin-ajax.php'), 'auto_suggest' => true); } else { $data_value = array('all' => $field['all'], 'type' => $tax_display, 'single_select' => $single_select); } } // if not hierarchical taxonomy if (!$field['hierarchical']) { $objs =& $this->_myzebra_form->add('taxonomy', $name, $value, $data_value); } else { $objs =& $this->_myzebra_form->add('taxonomyhierarchical', $name, $value, $data_value); } // register this taxonomy field for later use by auxilliary taxonomy fields $this->_taxonomy_aux['taxonomy'][$name_orig] =& $objs; // if a taxonomy auxiliary field exists attached to this taxonomy, add this taxonomy id to it if (isset($this->_taxonomy_aux['aux'][$name_orig])) { $this->_taxonomy_aux['aux'][$name_orig]->set_attributes(array('master_taxonomy_id' => $objs->attributes['id'])); } if (!is_array($objs)) { $oob = array($objs); } else { $oob = $objs; } $ids = array(); foreach ($oob as &$obj) { $obj->setPrimeName($name_orig); // field belongs to a container? if ($this->_current_group !== null) { $this->_current_group->addControl($obj); //$obj->setParent($this->_current_group); } $atts = $obj->get_attributes(array('id', 'type')); $ids[] = $atts['id']; } } else { if ($preset_value && $preset_value !== null) { // use translated value by WPML if exists $data_value = cred_translate('Value: ' . $preset_value, $preset_value, 'cred-form-' . $this->_form->form->post_title . '-' . $this->_form->form->ID); } else { $data_value = null; } $ids = array(); if (in_array($field['type'], array('show_popular', 'add_new'))) { if ($field['type'] == 'show_popular') { $objs =& $this->_myzebra_form->add('taxonomypopular', $name, $value, array('popular' => $field['taxonomy']['most_popular'], 'show_popular_text' => $this->getLocalisedMessage('show_popular'), 'hide_popular_text' => $this->getLocalisedMessage('hide_popular'))); } elseif ($field['type'] == 'add_new') { $objs =& $this->_myzebra_form->add('taxonomyhierarchicaladdnew', $name, $value, array('add_new_text' => $this->getLocalisedMessage('add_new_taxonomy'), 'add_text' => $this->getLocalisedMessage('add_taxonomy'), 'parent_text' => __('-- Parent --', 'wp-cred'))); } // register this taxonomy auxilliary field for later use by taxonomy fields $this->_taxonomy_aux['aux'][$field['master_taxonomy']] =& $objs; // if a taxonomy field exists that this field is attached, link to its id here if (isset($this->_taxonomy_aux['taxonomy'][$field['master_taxonomy']])) { $objs->set_attributes(array('master_taxonomy_id' => $this->_taxonomy_aux['taxonomy'][$field['master_taxonomy']]->attributes['id'])); } if (!is_array($objs)) { $oob = array($objs); } else { $oob = $objs; } foreach ($oob as &$obj) { $atts = $obj->get_attributes(array('id', 'type')); $ids[] = $atts['id']; } } } } return $ids; // return the ids of the created fields }
function checkConditionals() { // get parser require_once dirname(__FILE__) . '/MyZebra_Parser.php'; // localize dates MyZebra_DateParser::setDateLocaleStrings($this->language['days'], $this->language['months']); // add parameters for parser, these must also be used in js parser if (isset($this->extra_parameters['parser_info'])) { MyZebra_Parser::setParams($this->extra_parameters['parser_info']); } foreach ($this->conditionals as $id => $conditional) { //print_r(sprintf('%s is %s <br />',$id,($conditional->isDiscarded())?'DISCARDED':'NOT DISCARDED')); // control already discarded if ($conditional->isDiscarded()) { continue; } $data = $conditional->getConditionData(); // condition is not valid, show group by default if (!$data['valid']) { continue; } $condition = $data['replaced_condition']; // get variable values from fields $vars = $data['var_field_map']['replace']; $fields = $data['var_field_map']['field_reference']; $vars_ok = true; $parser_vars = array(); foreach ($vars as $key => $vv) { // var refers to field control that not exist if (!isset($fields[$key]) || !isset($this->controls[$fields[$key]])) { $vars_ok = false; break; } $field = $this->controls[$fields[$key]]; $field_values = $field->get_values_for_conditions(); $isDate = false; $format = ''; if ($field->attributes['type'] == 'checkbox' || $field->attributes['type'] == 'checkboxes' || $field->attributes['type'] == 'select') { if (!is_array($field_values)) { $field_values = array($field_values); } } // date field if (isset($field->attributes['class']) && strpos($field->attributes['class'], 'myzebra-date') >= 0 && isset($field->attributes['format'])) { $isDate = true; $format = $field->attributes['format']; } $type = 'string'; if ($isDate) { $type = 'date'; } elseif (is_array($field_values)) { $type = 'array'; } $parser_vars[] = array('name' => $vv, 'val' => $field_values, 'withType' => $type, 'format' => $format); } // bypass this group, show by default if (!$vars_ok) { continue; } // evaluate condition try { //print_r($parser_vars); $parser = new MyZebra_Parser($condition); foreach ($parser_vars as $var) { $parser->addVar($var); } $showGroup = (bool) $parser->evaluate(); } catch (Exception $e) { $showGroup = false; } $data['result'] = $showGroup; $this->conditionals[$id]->setConditionData($data); // discard this group and its contents from further processing if (!$showGroup) { $this->conditionals[$id]->discard(); // print_r('<br />'.$id.' is Discarded<br />'); } //else // print_r('<br />'.$id.' is Shown<br />'); } /*foreach ($this->conditionals as $id=>$conditional) { //print_r(sprintf('%s is %s <br />',$id,($conditional->isDiscarded())?'DISCARDED':'NOT DISCARDED')); if ($conditional->getParent()!==null) { $parent=$conditional->getParent(); print_r(sprintf('Parent %s is %s <br />',$parent->attributes['id'],($parent->isDiscarded())?'DISCARDED':'NOT DISCARDED')); } foreach ($conditional->controls as $control) { print_r(sprintf('Child %s is %s <br />',$control->attributes['id'],($control->isDiscarded())?'DISCARDED':'NOT DISCARDED')); } }*/ }