public static function sendNotifications($post_id, $form_id, $notificationsToSent) { // custom action hooks here, for 3rd-party integration //do_action('cred_before_send_notifications_'.$form_id, $post_id, $form_id, $notificationsToSent); //do_action('cred_before_send_notifications', $post_id, $form_id, $notificationsToSent); // get Mailer $mailer = CRED_Loader::get('CLASS/Mail_Handler'); // get current user $user = self::getCurrentUserData(); $is_user_form = self::get_form_type($form_id) == CRED_USER_FORMS_CUSTOM_POST_NAME; // get Model $model = $is_user_form ? CRED_Loader::get('MODEL/UserForms') : CRED_Loader::get('MODEL/Forms'); //user created/updated $the_user = $is_user_form ? get_userdata($post_id)->data : null; // get some data for placeholders $form_post = get_post($form_id); $form_title = $form_post ? $form_post->post_title : ''; $link = get_permalink($post_id); $title = get_the_title($post_id); $admin_edit_link = CRED_CRED::getPostAdminEditLink($post_id); //get_edit_post_link( $post_id ); //$date=date('d/m/Y H:i:s'); $date = date('Y-m-d H:i:s', current_time('timestamp')); // placeholder codes, allow to add custom $data_subject = apply_filters('cred_subject_notification_codes', array('%%USER_USERID%%' => isset($the_user) && isset($the_user->ID) ? $the_user->ID : '', '%%USER_EMAIL%%' => isset($the_user) && isset($the_user->user_email) ? $the_user->user_email : '', '%%USER_USERNAME%%' => isset(StaticClass::$_username_generated) ? StaticClass::$_username_generated : '', '%%USER_PASSWORD%%' => isset(StaticClass::$_password_generated) ? StaticClass::$_password_generated : '', '%%USER_NICKNAME%%' => isset(StaticClass::$_nickname_generated) ? StaticClass::$_nickname_generated : '', '%%USER_LOGIN_NAME%%' => $user->login, '%%USER_DISPLAY_NAME%%' => $user->display_name, '%%POST_ID%%' => $post_id, '%%POST_TITLE%%' => $title, '%%FORM_NAME%%' => $form_title, '%%DATE_TIME%%' => $date), $form_id, $post_id); // placeholder codes, allow to add custom $data_body = apply_filters('cred_body_notification_codes', array('%%USER_USERID%%' => isset($the_user) && isset($the_user->ID) ? $the_user->ID : '', '%%USER_EMAIL%%' => isset($the_user) && isset($the_user->user_email) ? $the_user->user_email : '', '%%USER_USERNAME%%' => isset(StaticClass::$_username_generated) ? StaticClass::$_username_generated : '', '%%USER_PASSWORD%%' => isset(StaticClass::$_password_generated) ? StaticClass::$_password_generated : '', '%%USER_NICKNAME%%' => isset(StaticClass::$_nickname_generated) ? StaticClass::$_nickname_generated : '', '%%USER_LOGIN_NAME%%' => $user->login, '%%USER_DISPLAY_NAME%%' => $user->display_name, '%%POST_ID%%' => $post_id, '%%POST_TITLE%%' => $title, '%%POST_LINK%%' => $link, '%%POST_ADMIN_LINK%%' => $admin_edit_link, '%%FORM_NAME%%' => $form_title, '%%DATE_TIME%%' => $date), $form_id, $post_id); //cred_log(array($post_id, $form_id, $data_subject, $data_body, $notificationsToSent)); foreach ($notificationsToSent as $notification) { // bypass if nothing if (!$notification || empty($notification) || !(isset($notification['to']['type']) || isset($notification['to']['author']))) { continue; } // reset mail handler $mailer->reset(); $mailer->setHTML(true, false); $recipients = array(); if (isset($notification['to']['author']) && 'author' == $notification['to']['author']) { $author_post_id = isset($_POST['form_' . $form_id . '_referrer_post_id']) ? $_POST['form_' . $form_id . '_referrer_post_id'] : 0; if (0 == $author_post_id && $post_id) { $mypost = get_post($post_id); $author_id = $mypost->post_author; } else { $mypost = get_post($author_post_id); $author_id = $user->ID; if (!isset($author_id)) { $author_id = $mypost->post_author; } } if ($author_id) { $_to_type = 'to'; $user_info = get_userdata($author_id); $_addr_name = isset($user_info) && isset($user_info->user_firstname) && !empty($user_info->user_firstname) ? $user_info->user_firstname : false; $_addr_lastname = isset($user_info) && isset($user_info->user_lasttname) && !empty($user_info->user_lasttname) ? $user_info->user_lastname : false; $_addr = $user_info->user_email; if (isset($_addr)) { $recipients[] = array('to' => $_to_type, 'address' => $_addr, 'name' => $_addr_name, 'lastname' => $_addr_lastname); } } } // parse Notification Fields if (!isset($notification['to']['type'])) { $notification['to']['type'] = array(); } if (!is_array($notification['to']['type'])) { $notification['to']['type'] = (array) $notification['to']['type']; } // notification to a mail field (which is saved as post meta) if (in_array('mail_field', $notification['to']['type']) && isset($notification['to']['mail_field']['address_field']) && !empty($notification['to']['mail_field']['address_field'])) { $_to_type = 'to'; $_addr = false; $_addr_name = false; $_addr_lastname = false; if ($is_user_form) { $_addr = $the_user->user_email; } else { $_addr = $model->getPostMeta($post_id, $notification['to']['mail_field']['address_field']); } if (isset($notification['to']['mail_field']['to_type']) && in_array($notification['to']['mail_field']['to_type'], array('to', 'cc', 'bcc'))) { $_to_type = $notification['to']['mail_field']['to_type']; } if (isset($notification['to']['mail_field']['name_field']) && !empty($notification['to']['mail_field']['name_field']) && '###none###' != $notification['to']['mail_field']['name_field']) { $_addr_name = $is_user_form ? $model->getUserMeta($post_id, $notification['to']['mail_field']['name_field']) : $model->getPostMeta($post_id, $notification['to']['mail_field']['name_field']); } if (isset($notification['to']['mail_field']['lastname_field']) && !empty($notification['to']['mail_field']['lastname_field']) && '###none###' != $notification['to']['mail_field']['lastname_field']) { $_addr_lastname = $is_user_form ? $model->getUserMeta($post_id, $notification['to']['mail_field']['lastname_field']) : $model->getPostMeta($post_id, $notification['to']['mail_field']['lastname_field']); } // add to recipients $recipients[] = array('to' => $_to_type, 'address' => $_addr, 'name' => $_addr_name, 'lastname' => $_addr_lastname); } // notification to an exisiting wp user if (in_array('wp_user', $notification['to']['type'])) { $_to_type = 'to'; $_addr = false; $_addr_name = false; $_addr_lastname = false; if (isset($notification['to']['wp_user']['to_type']) && in_array($notification['to']['wp_user']['to_type'], array('to', 'cc', 'bcc'))) { $_to_type = $notification['to']['wp_user']['to_type']; } $_addr = $notification['to']['wp_user']['user']; $user_id = email_exists($_addr); if ($user_id) { $user_info = get_userdata($user_id); $_addr_name = isset($user_info->user_firstname) && !empty($user_info->user_firstname) ? $user_info->user_firstname : false; $_addr_lastname = isset($user_info->user_lasttname) && !empty($user_info->user_lasttname) ? $user_info->user_lastname : false; // add to recipients $recipients[] = array('to' => $_to_type, 'address' => $_addr, 'name' => $_addr_name, 'lastname' => $_addr_lastname); } } // notification to an exisiting wp user if (in_array('user_id_field', $notification['to']['type'])) { $_to_type = 'to'; $_addr = false; $_addr_name = false; $_addr_lastname = false; if (isset($notification['to']['user_id_field']['to_type']) && in_array($notification['to']['user_id_field']['to_type'], array('to', 'cc', 'bcc'))) { $_to_type = $notification['to']['user_id_field']['to_type']; } //$user_id = $is_user_form ? @trim($model->getUserMeta($post_id, $notification['to']['user_id_field']['field_name'])) : @trim($model->getPostMeta($post_id, $notification['to']['user_id_field']['field_name'])); $user_id = $is_user_form ? $post_id : @trim($model->getPostMeta($post_id, $notification['to']['user_id_field']['field_name'])); if ($user_id) { $user_info = get_userdata($user_id); if ($user_info) { $_addr = isset($user_info->user_email) && !empty($user_info->user_email) ? $user_info->user_email : false; $_addr_name = isset($user_info->user_firstname) && !empty($user_info->user_firstname) ? $user_info->user_firstname : false; $_addr_lastname = isset($user_info->user_lasttname) && !empty($user_info->user_lasttname) ? $user_info->user_lastname : false; // add to recipients $recipients[] = array('to' => $_to_type, 'address' => $_addr, 'name' => $_addr_name, 'lastname' => $_addr_lastname); } } } // notification to specific recipients if (in_array('specific_mail', $notification['to']['type']) && isset($notification['to']['specific_mail']['address'])) { $tmp = explode(',', $notification['to']['specific_mail']['address']); foreach ($tmp as $aa) { $recipients[] = array('address' => $aa, 'to' => false, 'name' => false, 'lastname' => false); } unset($tmp); } // add custom recipients by 3rd-party //cred_log(array('cred_notification_recipients', $recipients, $notification, $form_id, $post_id)); //$recipients=apply_filters('cred_notification_recipients', $recipients, array('form_id'=>$form_id, 'post_id'=>$post_id, 'notification'=>$notification)); $recipients = apply_filters('cred_notification_recipients', $recipients, $notification, $form_id, $post_id); if (!$recipients || empty($recipients)) { continue; } // build recipients foreach ($recipients as $ii => $recipient) { // nowhere to send, bypass if (!isset($recipient['address']) || !$recipient['address']) { unset($recipients[$ii]); continue; } if (false === $recipient['to']) { // this is already formatted $recipients[$ii] = $recipient['address']; continue; } $tmp = ''; $tmp .= $recipient['to'] . ': '; $tmp2 = array(); if ($recipient['name']) { $tmp2[] = $recipient['name']; } if ($recipient['lastname']) { $tmp2[] = $recipient['lastname']; } if (!empty($tmp2)) { $tmp .= implode(' ', $tmp2) . ' <' . $recipient['address'] . '>'; } else { $tmp .= $recipient['address']; } $recipients[$ii] = $tmp; } //cred_log($recipients); $mailer->addRecipients($recipients); if (isset($_POST[StaticClass::PREFIX . 'cred_container_id'])) { $notification['mail']['body'] = str_replace("[cred-container-id]", StaticClass::$_cred_container_id, $notification['mail']['body']); } global $current_user_id; $current_user_id = $user_id; if (!$user_id && $is_user_form) { $current_user_id = $post_id; } // build SUBJECT $_subj = ''; if (isset($notification['mail']['subject'])) { $_subj = $notification['mail']['subject']; } // build BODY $_bod = ''; if (isset($notification['mail']['body'])) { $_bod = $notification['mail']['body']; } // replace placeholders $_subj = self::replacePlaceholders($_subj, $data_subject); // replace placeholders $_bod = self::replacePlaceholders($_bod, $data_body); //fixing https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/188538611/comments if (defined('WPCF_EMBEDDED_ABSPATH') && WPCF_EMBEDDED_ABSPATH) { require_once WPCF_EMBEDDED_ABSPATH . '/frontend.php'; } // provide WPML localisation if (isset($notification['_cred_icl_string_id']['subject'])) { $notification_subject_string_translation_name = self::getNotification_translation_name($notification['_cred_icl_string_id']['subject']); if ($notification_subject_string_translation_name) { $_subj = cred_translate($notification_subject_string_translation_name, $_subj, 'cred-form-' . $form_title . '-' . $form_id); } } // provide WPML localisation if (isset($notification['_cred_icl_string_id']['body'])) { $notification_body_string_translation_name = self::getNotification_translation_name($notification['_cred_icl_string_id']['body']); if ($notification_body_string_translation_name) { $_bod = cred_translate($notification_body_string_translation_name, $_bod, 'cred-form-' . $form_title . '-' . $form_id); } } // parse shortcodes if necessary relative to $post_id $_subj = CRED_Helper::renderWithPost(stripslashes($_subj), $post_id, false); $mailer->setSubject($_subj); // parse shortcodes/rich text if necessary relative to $post_id $_bod = CRED_Helper::renderWithPost($_bod, $post_id); //https://icanlocalize.basecamphq.com/projects/11629195-toolset-peripheral-work/todo_items/195775787/comments#310779109 $_bod = stripslashes($_bod); $mailer->setBody($_bod); // build FROM address / name, independantly $_from = array(); if (isset($notification['from']['address']) && !empty($notification['from']['address'])) { $_from['address'] = $notification['from']['address']; } if (isset($notification['from']['name']) && !empty($notification['from']['name'])) { $_from['name'] = $notification['from']['name']; } if (!empty($_from)) { $mailer->setFrom($_from); } // send it $_send_result = $mailer->send(); if ($_send_result !== true) { update_option('_' . $form_id . '_last_mail_error', $_send_result); } } // custom action hooks here, for 3rd-party integration //do_action('cred_after_send_notifications_'.$form_id, $post_id); //do_action('cred_after_send_notifications', $post_id); }
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 }
public static function thankyouMessage($atts, $content) { $added = array(); $cred_data = self::$handler->getCredData(); $message = ''; $model = CREDC_Loader::get('MODEL/Main'); foreach ($cred_data as $ii => $data) { if (isset($data['cred_form_id'])) { $form = $model->getForm($data['cred_form_id'], false); if (isset($form->commerce['messages']['thankyou']) && !in_array($form->commerce['messages']['thankyou'], $added)) { $added[] = $form->commerce['messages']['thankyou']; // allow WPML string localization $message .= do_shortcode(cred_translate('cred_commerce_thankyou_message', $form->commerce['messages']['thankyou'], 'cred-form-' . $form->ID)); $message .= " "; } } } //return '<pre>'.print_r($cred_data, true).'</pre>'; return $message; }
public function translate_field($name, &$field, $additional_options = array()) { // allow multiple submit buttons static $_count_ = array('submit' => 0); static $wpExtensions = false; // get refs here $globals =& self::friendGetStatic('CRED_Form_Builder', '&_staticGlobal'); if (false === $wpExtensions) { $wpMimes = $globals['MIMES']; $wpExtensions = implode(',', array_keys($wpMimes)); } // get refs here $form =& $this->friendGet($this->_formBuilder, '&_formData'); $supported_date_formats =& $this->friendGet($this->_formBuilder, '&_supportedDateFormats'); $out_ =& $this->friendGet($this->_formBuilder, '&out_'); $postData =& $this->friendGet($this->_formBuilder, '&_postData'); $zebraForm = $this->friendGet($this->_formBuilder, '_zebraForm'); // 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, 'generic_type' => null, 'urlparam' => ''), $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 not taxonomy field if ($placeholder && $placeholder !== null && !empty($placeholder) && is_string($placeholder)) { // use translated value by WPML if exists $placeholder = cred_translate('Value: ' . $placeholder, $placeholder, 'cred-form-' . $form->getForm()->post_title . '-' . $form->getForm()->ID); } if ($preset_value && null !== $preset_value && 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-' . $form->getForm()->post_title . '-' . $form->getForm()->ID); } elseif (is_string($urlparam) && !empty($urlparam) && isset($_GET[$urlparam])) { // use translated value by WPML if exists $data_value = urldecode($_GET[$urlparam]); } elseif ($postData && isset($postData->fields[$name_orig])) { if (is_array($postData->fields[$name_orig]) && count($postData->fields[$name_orig]) > 1) { $data_value = $postData->fields[$name_orig]; } else { $data_value = $postData->fields[$name_orig][0]; } } elseif ($_POST && isset($_POST) && isset($_POST[$name_orig])) { $data_value = $_POST[$name_orig]; } else { $data_value = null; } $value = ''; // save a map between options / actual values for these types to be used later if (in_array($field['type'], array('checkboxes', 'radio', 'select', 'multiselect'))) { //cred_log($field); $tmp = array(); foreach ($field['data']['options'] as $optionKey => $optionData) { //https://onthegosystems.myjetbrains.com/youtrack/issue/cred-113 //added !== if ($optionKey !== 'default' && is_array($optionData)) { $tmp[$optionKey] = isset($optionData['set_value']) && 'checkboxes' == $field['type'] ? $optionData['set_value'] : isset($optionData['value']) ? $optionData['value'] : ""; } } $out_['field_values_map'][$field['slug']] = $tmp; unset($tmp); unset($optionKey); unset($optionData); } switch ($field['type']) { case 'form_messages': $type = 'messages'; break; case 'form_submit': $type = 'submit'; if (null !== $data_value) { $value = $data_value; } // allow multiple submit buttons $name .= '_' . ++$_count_['submit']; 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 (false !== $globals['RECAPTCHA']) { $attributes['public_key'] = $globals['RECAPTCHA']['public_key']; $attributes['private_key'] = $globals['RECAPTCHA']['private_key']; } if (1 == $out_['count']) { $attributes['open'] = true; } // used to load additional js script $out_['has_recaptcha'] = true; break; case 'audio': case 'video': 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($postData->extra['featured_img_html'])) { $attributes['display_featured_html'] = $value = $postData->extra['featured_img_html']; } } break; case 'date': $type = 'date'; $format = $zebraForm->getDateFormat(); $format .= " h:i:s"; $value = array(); $attributes['format'] = $format; $attributes['readonly_element'] = false; if (null !== $data_value && !empty($data_value) && (is_numeric($data_value) || is_int($data_value) || is_long($data_value))) { //$value = $data_value; $value['timestamp'] = $data_value; $value['datepicker'] = adodb_date($format, $data_value); } break; case 'multiselect': case 'select': $type = 'select'; $value = array(); $attributes = array(); $attributes['options'] = array(); $titles = array(); $default = array(); foreach ($field['data']['options'] as $key => $option) { $index = $key; //$option['value']; //https://onthegosystems.myjetbrains.com/youtrack/issue/cred-113 //added !== if ('default' === $key) { if ('select' == $field['type']) { $default[] = $option; } else { $default = (array) $option; } } else { if (is_admin()) { //register strings on form save cred_translate_register_string('cred-form-' . $form->getForm()->post_title . '-' . $form->getForm()->ID, $option['title'], $option['title'], false); } $option = $this->translate_option($option, $key, $form, $field); $attributes['options'][$index] = $option['title']; if (null !== $data_value && $data_value == $option['value'] || is_array($data_value) && in_array($option['value'], $data_value)) { if ('select' == $field['type']) { $titles[] = $key; $value = $key; } else { $value[] = $key; } } if (isset($option['dummy']) && $option['dummy']) { $attributes['dummy'] = $key; } } } if ('select' == $field['type']) { if (empty($titles) && !empty($default)) { $titles = $default; } $attributes['actual_value'] = $titles; } else { $attributes['multiple'] = 'multiple'; if (empty($value) && !empty($default)) { $value = $default; } } if (isset($out_['field_values_map'][$field['slug']])) { $attributes['actual_options'] = $out_['field_values_map'][$field['slug']]; } break; case 'radio': $type = 'radios'; $value = array(); $titles = array(); $attributes = ''; $default = ''; foreach ($field['data']['options'] as $key => $option) { $index = $key; //$option['display_value']; if ('default' == $key) { $default = $option; } else { if (is_admin()) { //register strings on form save cred_translate_register_string('cred-form-' . $form->getForm()->post_title . '-' . $form->getForm()->ID, $option['title'], $option['title'], false); } $option = $this->translate_option($option, $key, $form, $field); $titles[$index] = $option['title']; if ($data_value !== null && $data_value == $option['value']) { $attributes = $key; $value = $key; } } } if ($data_value === null && !empty($default)) { $attributes = $default; } $def = $attributes; $attributes = array('default' => $def); $attributes['actual_titles'] = $titles; if (isset($out_['field_values_map'][$field['slug']])) { $attributes['actual_values'] = $out_['field_values_map'][$field['slug']]; } break; case 'checkboxes': $type = 'checkboxes'; //$name.='[]'; //$value=array(); $value = array(); //Fixed https://onthegosystems.myjetbrains.com/youtrack/issue/cred-211 if (isset($data_value) && !empty($data_value)) { foreach ($data_value as $v => $v1) { $value[$v] = 1; } } $titles = array(); $attributes = array(); /* if (is_array($data_value)) $data_value=array_keys($data_value); else */ if (!is_array($data_value) && null !== $data_value) { $data_value = array($data_value); } foreach ($field['data']['options'] as $key => $option) { if (is_admin()) { //register strings on form save cred_translate_register_string('cred-form-' . $form->getForm()->post_title . '-' . $form->getForm()->ID, $option['title'], $option['title'], false); } $option['title'] = cred_translate($option['title'], $option['title'], 'cred-form-' . $form->getForm()->post_title . '-' . $form->getForm()->ID); $index = $key; $titles[$index] = $option['title']; if (empty($value)) { if (isset($data_value) && !empty($data_value) && isset($data_value[$index])) { $value[$index] = $data_value[$index]; } else { $value[$index] = 0; } } if (isset($option['checked']) && $option['checked'] && null === $data_value) { $attributes[] = $index; } elseif (null !== $data_value && isset($data_value[$index])) { if (!('yes' == $field['data']['save_empty'] && (0 === $data_value[$index] || '0' === $data_value[$index]))) { $attributes[] = $index; } } } $def = $attributes; $attributes = array('default' => $def); $attributes['actual_titles'] = $titles; if (isset($out_['field_values_map'][$field['slug']])) { $attributes['actual_values'] = $out_['field_values_map'][$field['slug']]; } break; case 'checkbox': $type = 'checkbox'; $value = $field['data']['set_value']; if (null !== $data_value && $data_value == $value) { $attributes = array('checked' => 'checked'); } break; case 'textarea': $type = 'textarea'; if (null !== $data_value) { $value = $data_value; } if ($placeholder && null !== $placeholder && !empty($placeholder)) { $attributes['placeholder'] = $placeholder; } break; case 'wysiwyg': $type = 'wysiwyg'; if (null !== $data_value) { $value = $data_value; } $attributes = array('disable_xss_filters' => true); //cred_log($form->fields); if ('post_content' == $name && isset($form->fields['form_settings']->form['has_media_button']) && $form->fields['form_settings']->form['has_media_button']) { $attributes['has_media_button'] = true; } break; case 'numeric': $type = 'numeric'; if (null !== $data_value) { $value = $data_value; } break; case 'phone': $type = 'phone'; if (null !== $data_value) { $value = $data_value; } break; case 'embed': case 'url': $type = 'url'; if (null !== $data_value) { $value = $data_value; } break; case 'email': $type = 'email'; if (null !== $data_value) { $value = $data_value; } break; case 'colorpicker': $type = 'colorpicker'; if (null !== $data_value) { $value = $data_value; } break; case 'textfield': $type = 'textfield'; if (null !== $data_value) { $value = $data_value; } if ($placeholder && null !== $placeholder && !empty($placeholder)) { $attributes['placeholder'] = $placeholder; } break; case 'password': $type = 'password'; if (null !== $data_value) { $value = $data_value; } if ($placeholder && null !== $placeholder && !empty($placeholder)) { $attributes['placeholder'] = $placeholder; } break; case 'hidden': $type = 'hidden'; if (null !== $data_value) { $value = $data_value; } break; case 'skype': $type = 'skype'; if (null !== $data_value && is_string($data_value)) { $data_value = array('skypename' => $data_value, 'style' => ''); } if (null !== $data_value) { $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; // everything else defaults to a simple text field // everything else defaults to a simple text field default: $type = 'textfield'; if (null !== $data_value) { $value = $data_value; } break; } if ($make_readonly) { if (!is_array($attributes)) { $attributes = array(); } $attributes['readonly'] = 'readonly'; } // repetitive field (special care) if (isset($field['data']['repetitive']) && $field['data']['repetitive']) { $value = isset($postData->fields[$name_orig]) ? $postData->fields[$name_orig] : array(); $objs = $zebraForm->noadd($type, $name, $value, $attributes, $field); } else { $objs = $zebraForm->noadd($type, $name, $value, $attributes, $field); } } else { // taxonomy field or auxilliary taxonomy field (eg popular terms etc..) if (!array_key_exists('master_taxonomy', $field)) { // taxonomy field if ($field['hierarchical']) { if (in_array($preset_value, array('checkbox', 'select'))) { $tax_display = $preset_value; } else { $tax_display = 'checkbox'; } } if ($postData && isset($postData->taxonomies[$name_orig])) { if (!$field['hierarchical']) { $data_value = array('terms' => $postData->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, 'show_popular_text' => $this->getLocalisedMessage('show_popular'), 'hide_popular_text' => $this->getLocalisedMessage('hide_popular')); } else { $data_value = array('terms' => $postData->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, 'show_popular_text' => $this->getLocalisedMessage('show_popular'), 'hide_popular_text' => $this->getLocalisedMessage('hide_popular')); } else { $data_value = array('all' => $field['all'], 'type' => $tax_display, 'single_select' => $single_select); } } // if not hierarchical taxonomy if (!$field['hierarchical']) { $objs = $zebraForm->add('taxonomy', $name, $value, $data_value); } else { $objs = $zebraForm->add('taxonomyhierarchical', $name, $value, $data_value); } // register this taxonomy field for later use by auxilliary taxonomy fields $out_['taxonomy_map']['taxonomy'][$name_orig] =& $objs; // if a taxonomy auxiliary field exists attached to this taxonomy, add this taxonomy id to it if (isset($out_['taxonomy_map']['aux'][$name_orig])) { $out_['taxonomy_map']['aux'][$name_orig]->set_attributes(array('master_taxonomy_id' => $objs->attributes['id'])); } } else { // taxonomy auxilliary field (eg most popular etc..) if ($preset_value && null !== $preset_value) { // use translated value by WPML if exists $data_value = cred_translate('Value: ' . $preset_value, $preset_value, 'cred-form-' . $form->form->post_title . '-' . $form->form->ID); } else { $data_value = null; } } } //$out_=&$this->friendGet($this->_formBuilder, '&out_'); $count = $field['type'] == 'form_submit' ? '_' . $_count_['submit']++ : ""; $f = ""; if ($field['type'] == 'taxonomy_hierarchical' || $field['type'] == 'taxonomy_plain') { $f = "_" . $field['name']; } else { if (isset($field['master_taxonomy']) && isset($field['type'])) { $f = "_" . $field['master_taxonomy'] . "_" . $field['type']; } else { if (isset($field['id'])) { $f = "_" . $field['id']; } else { } } } return array("cred_form_" . $out_['prg_id'] . $f . $count); }
public static function cred_child_link_form($form, $parent_id = null, $text = '', $class = '', $style = '', $target = '', $attributes = '') { global $post; if (empty($form) || !is_numeric($form)) { return '<strong>' . __('No Child Form Page specified', 'wp-cred') . '</strong>'; } $form = intval($form); $link = get_permalink($form); if ($parent_id !== null) { $parent_id = intval($parent_id); if ($parent_id < 0) { $parent_id = $post->ID; } /* elseif ($parent_id<0) $parent_id=null; */ } if ($parent_id !== null) { $parent_type = get_post_type($parent_id); // localise the ID $parent_id = self::getLocalisedID($parent_id, $parent_type); if ($parent_type === false) { return __('Unknown Parent Type', 'wp-cred'); } $link = esc_url(add_query_arg(array('parent_' . $parent_type . '_id' => $parent_id), $link)); } $_atts = array(); if (!empty($class)) { $_atts[] = 'class="' . esc_attr(str_replace('"', "'", $class)) . '"'; } if (!empty($style)) { $_atts[] = 'style="' . esc_attr(str_replace('"', "'", $style)) . '"'; } if (!empty($target)) { $_atts[] = 'target="' . esc_attr(str_replace('"', "'", $target)) . '"'; } if (!empty($attributes)) { $_atts[] = str_replace(array('%eq%', '%dbquo%', '%quot%'), array("=", '"', "'"), $attributes); } // provide WPML localization for hardcoded texts return "<a href='{$link}' " . implode(' ', $_atts) . ">" . cred_translate('Child Link Text', $text, 'CRED Shortcodes') . "</a>"; }
/** * fixCredCustomFieldMessages * Fix CRED controlled custom fields validation message * replace with cred form settings messages and localize messages * @param type $field * @return type */ public function fixCredCustomFieldMessages(&$field) { if (!isset($field['cred_custom']) || isset($field['cred_custom']) && !$field['cred_custom']) { return; } $cred_messages = $this->extra_parameters->messages; foreach ($field['data']['validate'] as $a => &$b) { $idmessage = $this->typeMessage2textMessage($a); $b['message'] = $cred_messages[$idmessage]; $b['message'] = cred_translate(CRED_Form_Builder_Helper::MSG_PREFIX . $idmessage, $cred_messages[$idmessage], 'cred-form-' . $this->_formData->getForm()->post_title . '-' . $this->_formData->getForm()->ID); } }