/** * Filters groups on post edit page. * * @param type $groups * @param type $post * @return type */ function wpcf_cd_post_groups_filter($groups, $post, $context) { if ($context != 'group') { return $groups; } foreach ($groups as $key => &$group) { $conditions = null; if (array_key_exists('conditional_display', $group) && array_key_exists('conditions', $group['conditional_display'])) { $conditions = $group['conditional_display']; } else { $conditions = get_post_meta($group['id'], '_wpcf_conditional_display', true); } if (!empty($conditions['conditions'])) { $meta_box_id = "wpcf-group-{$group['slug']}"; $prefix = 'wpcf-'; $suffix = ''; $cond = array(); if (isset($post->ID)) { $cond_values = get_post_custom($post->ID); } else { $cond_values = array(); } $_cond_values = array(); foreach ($cond_values as $k => $v) { $v = maybe_unserialize($v[0]); $_cond_values[$k . $suffix] = is_array($v) ? strval(array_shift($v)) : $v; } unset($cond_values); $cond = array(); if (!empty($conditions['custom_use'])) { if (!empty($conditions['custom'])) { $custom = WPToolset_Types::getCustomConditional($conditions['custom']); $passed = WPToolset_Forms_Conditional::evaluateCustom($custom['custom'], $_cond_values); $cond = array('custom' => $custom['custom'], 'custom_use' => true); } } else { $cond = array('relation' => $conditions['relation'], 'conditions' => array(), 'values' => $_cond_values); foreach ($conditions['conditions'] as $d) { $c_field = types_get_field($d['field']); if (!empty($c_field)) { $_c = array('id' => wpcf_types_get_meta_prefix($c_field) . $d['field'] . $suffix, 'type' => $c_field['type'], 'operator' => $d['operation'], 'args' => array($d['value'])); $cond['conditions'][] = $_c; } } $passed = wptoolset_form_conditional_check(array('conditional' => $cond)); } $data = array('id' => $meta_box_id, 'conditional' => $cond); wptoolset_form_add_conditional('post', $data); if (!$passed) { $group['_conditional_display'] = 'failed'; } else { $group['_conditional_display'] = 'passed'; } } } return $groups; }
/** * Editor callback form. */ function wpcf_fields_embed_editor_callback($field, $data, $meta_type, $post) { // Get attachment $attachment_id = false; if (!empty($post->ID)) { $file = get_post_meta($post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true); if (empty($file)) { $user_id = wpcf_usermeta_get_user(); $file = get_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['slug'], true); } if (!empty($file)) { // Get attachment by guid global $wpdb; $attachment_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts}\n WHERE post_type = 'attachment' AND guid=%s", $file)); } } // Set data $data['attachment_id'] = $attachment_id; $data['file'] = !empty($file) ? $file : ''; return array('supports' => array(), 'tabs' => array('display' => array('menu_title' => __('Display', 'wpcf'), 'title' => __('Display', 'wpcf'), 'content' => WPCF_Loader::template('editor-modal-embed', $data))), 'settings' => $data); }
/** * Set current post and field. * * @param type $post * @param type $cf */ function set($user_id, $cf) { global $wpcf; /* * * Check if $cf is string */ if (is_string($cf)) { WPCF_Loader::loadInclude('fields'); $cf = wpcf_admin_fields_get_field($this->__get_slug_no_prefix($cf)); if (empty($cf)) { $this->_reset(); return false; } } $this->currentUID = $user_id; $this->ID = $cf['id']; $this->cf = $cf; $this->slug = wpcf_types_get_meta_prefix($this->cf) . $this->cf['slug']; $this->meta = $this->_get_meta(); $this->config = $this->_get_config(); $this->unique_id = wpcf_unique_id(serialize((array) $this)); $this->cf['value'] = $this->meta; // Debug $wpcf->debug->fieds[$this->unique_id] = $this->cf; $wpcf->debug->meta[$this->slug][] = $this->meta; // Load files if (isset($this->cf['type'])) { $file = WPCF_EMBEDDED_INC_ABSPATH . '/fields/' . $this->cf['type'] . '.php'; if (file_exists($file)) { include_once $file; } if (defined('WPCF_INC_ABSPATH')) { $file = WPCF_INC_ABSPATH . '/fields/' . $this->cf['type'] . '.php'; if (file_exists($file)) { include_once $file; } } } }
/** * Gets posts that belongs to current post. * * @global type $post * @param type $post_type * @param type $args * @return type */ function types_child_posts($post_type, $args = array()) { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; global $post, $wp_post_types; // WP allows querying inactive post types if (!isset($wp_post_types[$post_type]) || !$wp_post_types[$post_type]->publicly_queryable) { return array(); } $defaults = array('post_type' => $post_type, 'numberposts' => -1, 'post_status' => null, 'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id', 'meta_value' => $post->ID, 'suppress_filters' => false); $args = wp_parse_args($args, $defaults); $args = apply_filters('types_child_posts_args', $args); $child_posts = get_posts($args); foreach ($child_posts as $child_post_key => $child_post) { $child_posts[$child_post_key]->fields = array(); $groups = wpcf_admin_post_get_post_groups_fields($child_post); foreach ($groups as $group) { if (!empty($group['fields'])) { // Process fields foreach ($group['fields'] as $k => $field) { $child_posts[$child_post_key]->fields[$k] = get_post_meta($child_post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true); } } } } return $child_posts; }
/** * Set current post and field. * * @param type $post * @param type $cf */ function set($post, $cf) { global $wpcf; /* * * Check if $cf is string */ if (is_string($cf)) { WPCF_Loader::loadInclude('fields'); $_cf = wpcf_admin_fields_get_field($this->__get_slug_no_prefix($cf)); // Check if found without prefix if (empty($_cf)) { $_cf = wpcf_admin_fields_get_field($cf); } if (empty($_cf)) { /* * TODO Check what happens if field is not found */ $this->_reset(); return false; } $cf = $_cf; } $this->post = is_integer($post) ? get_post($post) : $post; // If empty post it is new if (empty($this->post->ID)) { $this->post = new stdClass(); $this->post->ID = 0; } $this->ID = $cf['id']; $this->cf = $cf; $this->slug = wpcf_types_get_meta_prefix($this->cf) . $this->cf['slug']; $this->meta = $this->_get_meta(); $this->config = $this->_get_config(); $this->unique_id = wpcf_unique_id(serialize((array) $this)); $this->cf['value'] = $this->meta; // Debug $wpcf->debug->fields[$this->unique_id] = $this->cf; $wpcf->debug->meta[$this->slug][] = $this->meta; // Load files $this->_include_file_by_field_type($this->cf['type']); if (defined('WPCF_INC_ABSPATH')) { $file = WPCF_INC_ABSPATH . '/fields/' . preg_replace('/[^\\w]+/', '', $this->cf['type']) . '.php'; if (file_exists($file)) { include_once $file; } } }
/** * Editor callback form. * * @global object $wpdb * */ function wpcf_fields_image_editor_callback($field, $data, $context, $post) { // Get post_ID $post_ID = !empty($post->ID) ? $post->ID : false; // Get attachment $image = false; $attachment_id = false; if ($post_ID) { $image = get_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true); if (empty($image)) { $user_id = wpcf_usermeta_get_user(); $image = get_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['slug'], true); } if (!empty($image)) { // Get attachment by guid global $wpdb; $attachment_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment' AND guid=%s", $image)); } } $data['image'] = $image; $data['attachment_id'] = $attachment_id; // Set post type $post_type = !empty($post->post_type) ? $post->post_type : ''; // Set image_data $image_data = wpcf_fields_image_get_data($image); if (!in_array($post_type, array('view', 'view-template'))) { // We must ignore errors here and treat image as outsider if (!empty($image_data['error'])) { $image_data['is_outsider'] = 1; $image_data['is_attachment'] = 0; } } else { if (!empty($image_data['error'])) { $image_data['is_outsider'] = 0; $image_data['is_attachment'] = 0; } } $data['preview'] = $attachment_id ? wp_get_attachment_image($attachment_id, 'thumbnail') : ''; // Title and Alt if ($attachment_id) { $alt = trim(strip_tags(get_post_meta($attachment_id, '_wp_attachment_image_alt', true))); $attachment_post = get_post($attachment_id); if (!empty($attachment_post)) { $title = trim(strip_tags($attachment_post->post_title)); } else { if (!empty($alt)) { $title = $alt; } } if (empty($alt)) { $alt = $title; } if (!isset($data['title'])) { $data['title'] = $title; } if (!isset($data['alt'])) { $data['alt'] = $alt; } } // Align options $data['alignment_options'] = array('none' => __('None', 'wpcf'), 'left' => __('Left', 'wpcf'), 'center' => __('Center', 'wpcf'), 'right' => __('Right', 'wpcf')); // Remote images settings $fetch_remote = (bool) wpcf_get_settings('images_remote'); $data['warning_remote'] = false; if (!types_is_repetitive($field) && $image_data['is_outsider'] && !$fetch_remote && !empty($data['image'])) { $data['warning_remote'] = true; } // Size settings $data['size_options'] = array('thumbnail' => sprintf(__('Thumbnail - %s', 'wpcf'), get_option('thumbnail_size_w') . 'x' . get_option('thumbnail_size_h')), 'medium' => sprintf(__('Medium - %s', 'wpcf'), get_option('medium_size_w') . 'x' . get_option('medium_size_h')), 'large' => sprintf(__('Large - %s', 'wpcf'), get_option('large_size_w') . 'x' . get_option('large_size_h')), 'full' => __('Original image', 'wpcf')); $wp_image_sizes = (array) get_intermediate_image_sizes(); foreach ($wp_image_sizes as $wp_size) { if ($wp_size != 'post-thumbnail' && !array_key_exists($wp_size, $data['size_options'])) { $data['size_options'][$wp_size] = $wp_size; } } $data['size_options']['wpcf-custom'] = __('Custom size...', 'wpcf'); // Get saved settings $data = array_merge(wpcf_admin_fields_get_field_last_settings($field['id']), $data); return array('supports' => array('styling', 'style'), 'tabs' => array('display' => array('menu_title' => __('Display options', 'wpcf'), 'title' => __('Display options for this field:', 'wpcf'), 'content' => WPCF_Loader::template('editor-modal-image', $data))), 'settings' => $data); }
/** * Gets all fields. * * @todo Move to WPCF_Fields * @param bool $only_active * @param bool $disabled_by_type * @param bool $strictly_active * @param string $option_name * @param bool $use_cache * @param bool $clear_cache * @return array * * added param $use_cache by Gen (used when adding new fields to group) * added param $use_cache by Gen (used when adding new fields to group) */ function wpcf_admin_fields_get_fields($only_active = false, $disabled_by_type = false, $strictly_active = false, $option_name = 'wpcf-fields', $use_cache = true, $clear_cache = false) { static $cache = array(); if ($clear_cache) { $cache = array(); } /** * Sanitize option name */ switch ($option_name) { case 'wpcf-usermeta': case 'wpcf-fields': case WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION: break; default: $option_name = 'wpcf-fields'; break; } $cache_key = md5($only_active . $disabled_by_type . $strictly_active . $option_name . $use_cache); if (isset($cache[$cache_key]) && $use_cache == true) { return $cache[$cache_key]; } $required_data = array('id', 'name', 'type', 'slug'); $fields = (array) get_option($option_name, array()); foreach ($fields as $k => $v) { $failed = false; foreach ($required_data as $required) { if (!isset($v[$required])) { $failed = true; continue; } if (is_numeric($v[$required]) === true) { $failed = true; continue; } } if (is_numeric($k) === true || $failed) { unset($fields[$k]); continue; } // This call loads config file $data = wpcf_fields_type_action($v['type']); if (empty($data)) { unset($fields[$k]); continue; } if (isset($data['wp_version']) && wpcf_compare_wp_version($data['wp_version'], '<')) { unset($fields[$k]); continue; } if ($strictly_active) { if (!empty($v['data']['disabled']) || !empty($v['data']['disabled_by_type'])) { unset($fields[$k]); continue; } } else { if ($only_active && !empty($v['data']['disabled'])) { unset($fields[$k]); continue; } if (!$disabled_by_type && !empty($v['data']['disabled_by_type'])) { unset($fields[$k]); continue; } } $v['id'] = $k; $v['meta_key'] = wpcf_types_get_meta_prefix($v) . $k; $option_name_to_meta_type = array('wpcf-fields' => 'postmeta', 'wpcf-usermeta' => 'usermeta', WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION => 'termmeta'); $v['meta_type'] = $option_name_to_meta_type[$option_name]; $fields[$k] = wpcf_sanitize_field($v); } $cache[$cache_key] = apply_filters('types_fields', $fields); return $cache[$cache_key]; }
/** * Custom Import function for Module Manager. * * Import selected items given by xmlstring. * * @global type $wpdb * @global type $iclTranslationManagement * @param type $data * @param type $_type * @return \WP_Error|boolean */ function wpcf_admin_import_data_from_xmlstring($data = '', $_type = 'types', $context = 'types') { global $wpdb, $wpcf; /* * * TODO Types 1.3 * Merge with wpcf_admin_import_data() */ $result = array('updated' => 0, 'new' => 0, 'failed' => 0, 'errors' => array()); libxml_use_internal_errors(true); $data = simplexml_load_string($data); if (!$data) { echo '<div class="message error"><p>' . __('Error parsing XML', 'wpcf') . '</p></div>'; foreach (libxml_get_errors() as $error) { return new WP_Error('error_parsing_xml', __('Error parsing XML', 'wpcf') . ' ' . $error->message); } libxml_clear_errors(); return false; } $errors = array(); $imported = false; // Process groups if (!empty($data->groups) && 'groups' == $_type) { $imported = true; $groups = array(); // Set Groups insert data from XML foreach ($data->groups->group as $group) { $group = (array) $group; // TODO 1.2.1 Remove // $_id = wpcf_modman_set_submitted_id( _GROUPS_MODULE_MANAGER_KEY_, // $group['ID'] ); $_id = $group['__types_id']; // If Types check if exists in $_POST if ($context == 'types' || $context == 'modman') { if (!isset($_POST['items']['groups'][$_id])) { continue; } } $group = wpcf_admin_import_export_simplexml2array($group); $group['add'] = true; $group['update'] = false; $groups[$_id] = $group; } // Insert groups foreach ($groups as $group) { $post = array('post_status' => $group['post_status'], 'post_type' => 'wp-types-group', 'post_title' => $group['post_title'], 'post_content' => !empty($group['post_content']) ? $group['post_content'] : ''); if (isset($group['add']) && $group['add']) { $post_to_update = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts}\n WHERE post_title = %s AND post_type = %s", $group['post_title'], 'wp-types-group')); // Update (may be forced by bulk action) if ($group['update'] || !empty($post_to_update)) { if (!empty($post_to_update)) { $post['ID'] = $post_to_update; /* * * Compare checksum to see if updated */ $_checksum = $wpcf->import->checksum('group', $post_to_update, $group['checksum']); $group_wp_id = wp_update_post($post); if (!$group_wp_id) { $errors[] = new WP_Error('group_update_failed', sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title'])); $result['errors'][] = sprintf(__('Group %s update failed', 'wpcf'), $group['post_title']); $result['failed'] += 1; } else { if (!$_checksum) { $result['updated'] += 1; } else { } } } else { $errors[] = new WP_Error('group_update_failed', sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title'])); } } else { // Insert $group_wp_id = wp_insert_post($post, true); if (is_wp_error($group_wp_id)) { $errors[] = new WP_Error('group_insert_failed', sprintf(__('Group "%s" insert failed', 'wpcf'), $group['post_title'])); $result['errors'][] = sprintf(__('Group %s insert failed', 'wpcf'), $group['post_title']); $result['failed'] += 1; } else { $result['new'] += 1; } } // Update meta if (!empty($group['meta'])) { foreach ($group['meta'] as $meta_key => $meta_value) { update_post_meta($group_wp_id, $meta_key, maybe_unserialize($meta_value)); } } $group_check[] = $group_wp_id; if (!empty($post_to_update)) { $group_check[] = $post_to_update; } } } // Process fields if (!empty($data->fields)) { $fields_existing = wpcf_admin_fields_get_fields(); $fields = array(); $fields_check = array(); // Set insert data from XML foreach ($data->fields->field as $field) { $field = wpcf_admin_import_export_simplexml2array($field); $fields[$field['id']] = $field; } // Insert fields foreach ($fields as $field_id => $field) { // If Types check if exists in $_POST // TODO Regular import do not have structure like this if ($context == 'types' || $context == 'modman') { if (!isset($_POST['items']['groups']['__fields__' . $field['slug']])) { continue; } } if (isset($field['add']) && !$field['add'] && !$overwrite_fields) { continue; } if (empty($field['id']) || empty($field['name']) || empty($field['slug'])) { continue; } $_new_field = !isset($fields_existing[$field_id]); if ($_new_field) { $result['new'] += 1; } else { $_checksum = $wpcf->import->checksum('field', $fields_existing[$field_id]['slug'], $field['checksum']); if (!$_checksum) { $result['updated'] += 1; } } $field_data = array(); $field_data['description'] = isset($field['description']) ? $field['description'] : ''; $field_data['data'] = isset($field['data']) && is_array($field['data']) ? $field['data'] : array(); foreach (array('id', 'name', 'type', 'slug', 'meta_key', 'meta_type') as $key) { if (array_key_exists($key, $field)) { $field_data[$key] = $field[$key]; } } $fields_existing[$field_id] = $field_data; $fields_check[] = $field_id; // WPML global $iclTranslationManagement; if (!empty($iclTranslationManagement) && isset($field['wpml_action'])) { $iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id] = $field['wpml_action']; $iclTranslationManagement->save_settings(); } } update_option('wpcf-fields', $fields_existing); } } // Process types if (!empty($data->types) && 'types' == $_type) { $imported = true; $types_existing = get_option('wpcf-custom-types', array()); $types = array(); $types_check = array(); // Set insert data from XML foreach ($data->types->type as $type) { $type = (array) $type; $type = wpcf_admin_import_export_simplexml2array($type); // TODO 1.2.1 Remove // $_id = wpcf_modman_get_submitted_id( _TYPES_MODULE_MANAGER_KEY_, // $type['id'] ); $_id = strval($type['__types_id']); // If Types check if exists in $_POST if ($context == 'types' || $context == 'modman') { if (!isset($_POST['items']['types'][$_id])) { continue; } } $types[$_id] = $type; } // Insert types foreach ($types as $type_id => $type) { if (isset($type['add']) && !$type['add']) { continue; } if (isset($types_existing[$type_id])) { /* * * Compare checksum to see if updated */ $_checksum = $wpcf->import->checksum('custom_post_type', $type_id, $type['checksum']); if (!$_checksum) { $result['updated'] += 1; } } else { $result['new'] += 1; } /* * Set type */ unset($type['add'], $type['update'], $type['checksum']); $types_existing[$type_id] = $type; $types_check[] = $type_id; } update_option('wpcf-custom-types', $types_existing); // Add relationships /** EMERSON: Restore Types relationships when importing modules */ if (!empty($data->post_relationships)) { $relationship_existing = get_option('wpcf_post_relationship', array()); /** * be sure, $relationship_existing is a array! */ if (!is_array($relationship_existing)) { $relationship_existing = array(); } $relationship = json_decode($data->post_relationships->data, true); if (is_array($relationship)) { $relationship = array_merge($relationship_existing, $relationship); update_option('wpcf_post_relationship', $relationship); } } } // Process taxonomies if (!empty($data->taxonomies) && 'taxonomies' == $_type) { $imported = true; $taxonomies_existing = get_option('wpcf-custom-taxonomies', array()); $taxonomies = array(); $taxonomies_check = array(); // Set insert data from XML foreach ($data->taxonomies->taxonomy as $taxonomy) { // TODO 1.2.1 Remove // $_id = wpcf_modman_get_submitted_id( _TAX_MODULE_MANAGER_KEY_, // $taxonomy['__types_id'] ); $_id = strval($taxonomy->__types_id); // If Types check if exists in $_POST if ($context == 'types' || $context == 'modman') { if (!isset($_POST['items']['taxonomies'][$_id])) { continue; } } $taxonomy = wpcf_admin_import_export_simplexml2array($taxonomy); $taxonomies[$_id] = $taxonomy; } // Insert taxonomies foreach ($taxonomies as $taxonomy_id => $taxonomy) { if (isset($taxonomy['add']) && !$taxonomy['add'] && !$overwrite_tax) { continue; } if (isset($taxonomies_existing[$taxonomy_id])) { /* * * Compare checksum to see if updated */ $_checksum = $wpcf->import->checksum('custom_taxonomy', $taxonomy_id, $taxonomy['checksum']); if (!$_checksum) { $result['updated'] += 1; } } else { $result['new'] += 1; } // Set tax unset($taxonomy['add'], $taxonomy['update'], $taxonomy['checksum']); $taxonomies_existing[$taxonomy_id] = $taxonomy; $taxonomies_check[] = $taxonomy_id; } update_option('wpcf-custom-taxonomies', $taxonomies_existing); } if ($imported) { // WPML bulk registration // TODO WPML move if (wpcf_get_settings('register_translations_on_import')) { wpcf_admin_bulk_string_translation(); } // Flush rewrite rules wpcf_init_custom_types_taxonomies(); flush_rewrite_rules(); } return $result; }
/** * Calls view function for specific usermeta field type. * * @global object $wpdb * * @param type $field * @param type $atts (additional attributes: user_id, user_name, user_is_author, user_current) * @return type */ function types_render_usermeta($field_id, $params, $content = null, $code = '') { global $wpcf, $post, $wpdb, $WP_Views; // HTML var holds actual output $html = ''; $current_user = wp_get_current_user(); $current_user_id = $current_user->ID; // Set post ID // user_id, user_name, user_is_author, user_current if (is_object($post)) { $post_id = $post->ID; } else { $post_id = 0; } if (isset($params['post_id']) && !empty($params['post_id'])) { $post_id = $params['post_id']; } //Get User id from views loop if (isset($WP_Views->users_data['term']->ID) && !empty($WP_Views->users_data['term']->ID)) { $params['user_id'] = $WP_Views->users_data['term']->ID; } //print_r($params);exit; //Get user By ID if (isset($params['user_id'])) { $user_id = $params['user_id']; } else { if (isset($params['user_name'])) { //Get user by login $user_id = $wpdb->get_var($wpdb->prepare("SELECT * FROM " . $wpdb->users . " WHERE user_login = %s", $params['user_name'])); } else { if (isset($params['user_is_author'])) { //Get Post author $user_id = $post->post_author; } else { if (isset($params['user_current'])) { //Get current logged user $user_id = $current_user_id; } else { //If empty get post author, if no post, return empty if (!empty($post_id)) { $user_id = $post->post_author; } else { return; } } } } } if (empty($user_id)) { return; } // Get field $field = types_get_field($field_id, 'usermeta'); // If field not found return empty string if (empty($field)) { // Log if (!function_exists('wplogger')) { require_once WPCF_EMBEDDED_TOOLSET_ABSPATH . '/toolset-common/wplogger.php'; } global $wplogger; $wplogger->log('types_render_field call for missing field \'' . $field_id . '\'', WPLOG_DEBUG); return ''; } // See if repetitive if (wpcf_admin_is_repetitive($field)) { $wpcf->usermeta_repeater->set($user_id, $field); $_meta = $wpcf->usermeta_repeater->_get_meta(); $meta = ''; if (isset($_meta['custom_order'])) { $meta = $_meta['custom_order']; } if (count($meta) == 1) { $meta_id = key($meta); $_temp = array_shift($meta); if (strval($_temp) == '') { return ''; } else { $params['field_value'] = $_temp; return types_render_field_single($field, $params, $content, $code, $meta_id); } } else { if (!empty($meta)) { $output = ''; if (isset($params['index'])) { $index = $params['index']; } else { $index = ''; } // Allow wpv-for-each shortcode to set the index $index = apply_filters('wpv-for-each-index', $index); if ($index === '') { $output = array(); foreach ($meta as $temp_key => $temp_value) { $params['field_value'] = $temp_value; $temp_output = types_render_field_single($field, $params, $content, $code, $temp_key); if (!empty($temp_output)) { $output[] = $temp_output; } } if (!empty($output) && isset($params['separator'])) { $output = implode(html_entity_decode($params['separator']), $output); } else { if (!empty($output)) { $output = implode('', $output); } else { return ''; } } } else { // Make sure indexed right $_index = 0; foreach ($meta as $temp_key => $temp_value) { if ($_index == $index) { $params['field_value'] = $temp_value; $output = types_render_field_single($field, $params, $content, $code, $temp_key); } $_index++; } } $html = $output; } else { return ''; } } } else { $params['field_value'] = get_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['slug'], true); if ($params['field_value'] == '' && $field['type'] != 'checkbox') { return ''; } $html = types_render_field_single($field, $params, $content, $code); } // API filter $wpcf->usermeta_field->set($user_id, $field); return $wpcf->usermeta_field->html($html, $params); }
/** * Calls view function for specific field type. * * @param type $field * @param type $atts * @return type */ function types_render_usermeta_field($field_id, $params, $content = null, $code = '') { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; global $wpcf, $post; // HTML var holds actual output $html = ''; // Set post ID $post_id = $post->ID; if (isset($params['post_id']) && !empty($params['post_id'])) { $post_id = $params['post_id']; } // Get field $field = wpcf_fields_get_field_by_slug($field_id, 'wpcf-usermeta'); // If field not found return empty string if (empty($field)) { // Log if (!function_exists('wplogger')) { require_once WPCF_EMBEDDED_ABSPATH . '/common/wplogger.php'; } global $wplogger; $wplogger->log('types_render_usermeta_field call for missing field \'' . $field_id . '\'', WPLOG_DEBUG); return ''; } //Get user By ID if (isset($params['user_id'])) { $user_id = $params['user_id']; } else { if (isset($params['user_name'])) { //Get user by login $user_id = $wpdb->get_var("SELECT * FROM " . $wpdb->prefix . "users WHERE user_login = '******'user_name'] . "'", 0, 0); } else { if (isset($params['user_is_author'])) { //Get Post author $user_id = $post->post_author; } else { if (isset($params['user_current'])) { //Get current logged user $user_id = wpcf_usermeta_get_user(); } else { //If empty get post author, if no post, return empty if (!empty($post_id)) { $user_id = $post->post_author; } else { return; } } } } } if (empty($user_id)) { return; } // Set field $wpcf->usermeta_field->set($user_id, $field); // See if repetitive if (wpcf_admin_is_repetitive($field)) { $wpcf->usermeta_repeater->set($user_id, $field); $_meta = $wpcf->usermeta_repeater->_get_meta(); $meta = $_meta['custom_order']; // $meta = get_post_meta( $post_id, // wpcf_types_get_meta_prefix( $field ) . $field['slug'], false ); // Sometimes if meta is empty - array(0 => '') is returned if (count($meta) == 1) { $meta_id = key($meta); $_temp = array_shift($meta); if (strval($_temp) == '') { return ''; } else { $params['field_value'] = $_temp; return types_render_field_single($field, $params, $content, $code, $meta_id); } } else { if (!empty($meta)) { $output = ''; if (isset($params['index'])) { $index = $params['index']; } else { $index = ''; } // Allow wpv-for-each shortcode to set the index $index = apply_filters('wpv-for-each-index', $index); if ($index === '') { $output = array(); foreach ($meta as $temp_key => $temp_value) { $params['field_value'] = $temp_value; $temp_output = types_render_field_single($field, $params, $content, $code, $temp_key); if (!empty($temp_output)) { $output[] = $temp_output; } } if (!empty($output) && isset($params['separator'])) { $output = implode(html_entity_decode($params['separator']), $output); } else { if (!empty($output)) { $output = implode('', $output); } else { return ''; } } } else { // Make sure indexed right $_index = 0; foreach ($meta as $temp_key => $temp_value) { if ($_index == $index) { $params['field_value'] = $temp_value; return types_render_field_single($field, $params, $content, $code, $temp_key); } $_index++; } // If missed index return ''; } $html = $output; } else { return ''; } } } else { $params['field_value'] = get_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['slug'], true); if ($params['field_value'] == '' && $field['type'] != 'checkbox') { return ''; } $html = types_render_field_single($field, $params, $content, $code, $wpcf->usermeta_field->meta_object->umeta_id); } // API filter // $wpcf->usermeta_field->set( $user_id, $field ); return $wpcf->usermeta_field->html($html, $params); }
/** * Returns meta prefix. * * @param array $field */ function types_meta_prefix($field = array()) { return wpcf_types_get_meta_prefix($field); }
/** * Deletes field. * * @param type $field_id */ function wpcf_admin_fields_delete_field($field_id) { global $wpdb; $fields = get_option('wpcf-fields', array()); if (isset($fields[$field_id])) { // Remove from groups $groups = wpcf_admin_fields_get_groups(); foreach ($groups as $key => $group) { wpcf_admin_fields_remove_field_from_group($group['id'], $field_id); } // Remove from posts if (!wpcf_types_cf_under_control('check_outsider', $field_id)) { $results = $wpdb->get_results("SELECT post_id, meta_key FROM {$wpdb->postmeta} WHERE meta_key = '" . wpcf_types_get_meta_prefix($fields[$field_id]) . strval($field_id) . "'"); foreach ($results as $result) { delete_post_meta($result->post_id, $result->meta_key); } } unset($fields[$field_id]); wpcf_admin_fields_save_fields($fields, true); return true; } else { return false; } }
/** * Initialize any url param controls that haven't been set already * This is for Views that were created before we had front end filters. * */ function _wpv_initialize_url_param_controls($view_settings) { if (function_exists('wpcf_admin_fields_get_fields')) { $fields = wpcf_admin_fields_get_fields(); } else { $fields = array(); } if (!isset($view_settings['filter_controls_param'])) { $view_settings['filter_controls_field_name'] = array(); $view_settings['filter_controls_param'] = array(); $view_settings['filter_controls_enable'] = array(); $view_settings['filter_controls_label'] = array(); $view_settings['filter_controls_values'] = array(); $view_settings['filter_controls_type'] = array(); $view_settings['filter_controls_mode'] = array(); } $url_params = wpv_custom_fields_get_url_params($view_settings); $url_params = array_merge($url_params, wpv_taxonomy_get_url_params($view_settings)); $search_param = wpv_search_get_url_params($view_settings); $url_params = array_merge($url_params, $search_param); foreach ($url_params as $url_param) { // see if it's already set $exists = false; foreach ($view_settings['filter_controls_param'] as $param) { if ($param == $url_param['param']) { $exists = true; break; } } if (!$exists) { // Doesn't exist so we add the control. $view_settings['filter_controls_field_name'][] = $url_param['name']; $view_settings['filter_controls_param'][] = $url_param['param']; $view_settings['filter_controls_enable'][] = 0; $label = $url_param['param']; $type = 'text'; switch ($url_param['mode']) { case 'cf': foreach ($fields as $field) { if ($url_param['name'] == wpcf_types_get_meta_prefix($field) . $field['slug']) { $label = $field['name']; $type = 'types-auto'; break; } } break; case 'tax': $label = $url_param['cat']->labels->name; break; case 'search': $label = $url_param['name']; break; } $view_settings['filter_controls_label'][] = $label; $view_settings['filter_controls_values'][] = ''; $view_settings['filter_controls_type'][] = $type; $view_settings['filter_controls_mode'][] = $url_param['mode']; } } return $view_settings; }
/** * Imports data from XML. * * @global object $wpdb * */ function wpcf_admin_import_data($data = '', $redirect = true, $context = 'types', $args = array()) { global $wpdb; $data_installer = false; libxml_use_internal_errors(true); $data = simplexml_load_string($data); if (!$data) { echo '<div class="message error"><p>' . __('Error parsing XML', 'wpcf') . '</p></div>'; foreach (libxml_get_errors() as $error) { echo '<div class="message error"><p>' . $error->message . '</p></div>'; } libxml_clear_errors(); return false; } $overwrite_settings = isset($_POST['overwrite-settings']); $overwrite_groups = isset($_POST['overwrite-groups']); $overwrite_fields = isset($_POST['overwrite-fields']); $overwrite_types = isset($_POST['overwrite-types']); $overwrite_tax = isset($_POST['overwrite-tax']); $delete_groups = isset($_POST['delete-groups']); $delete_fields = isset($_POST['delete-fields']); $delete_types = isset($_POST['delete-types']); $delete_tax = isset($_POST['delete-tax']); if ('wpvdemo' == $context && !empty($args)) { /** * allow overwrite */ $overwrite_groups = true; $overwrite_fields = true; $overwrite_types = true; $overwrite_tax = true; include_once dirname(__FILE__) . '/classes/class.types.data.installer.php'; $data_installer = new Types_Data_Installer($data, $args); $data = $data_installer->wpvdemo(); } /** * process settings */ if ($overwrite_settings && isset($data->settings)) { $wpcf_settings = wpcf_get_settings(); foreach (wpcf_admin_import_export_simplexml2array($data->settings) as $key => $value) { $wpcf_settings[$key] = $value; } wpcf_save_settings($wpcf_settings); wpcf_admin_message_store(__('Settings are updated.', 'wpcf')); } // Process groups $groups_check = array(); if (!empty($data->groups)) { $groups = array(); // Set insert data from XML foreach ($data->groups->group as $group) { $group = wpcf_admin_import_export_simplexml2array($group); $groups[$group['ID']] = $group; } // Set insert data from POST if (!empty($_POST['groups'])) { foreach ($_POST['groups'] as $group_id => $group) { if (empty($groups[$group_id])) { continue; } $groups[$group_id]['add'] = !empty($group['add']); $groups[$group_id]['update'] = isset($group['update']) && $group['update'] == 'update' ? true : false; } } else { foreach ($groups as $group_id => $group) { $groups[$group_id]['add'] = true; $groups[$group_id]['update'] = false; } } // Insert groups $show_import_fail_version_message = true; foreach ($groups as $group_id => $group) { $post = array('post_status' => $group['post_status'], 'post_type' => TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, 'post_title' => $group['post_title'], 'post_content' => !empty($group['post_content']) ? $group['post_content'] : ''); /** * preserve slug */ if (array_key_exists('__types_id', $group)) { $post['post_name'] = $group['__types_id']; } if (isset($group['add']) && $group['add']) { $post_to_update = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s", $group['post_title'], TYPES_CUSTOM_FIELD_GROUP_CPT_NAME)); // Update (may be forced by bulk action) if ($group['update'] || $overwrite_groups && !empty($post_to_update)) { if (!empty($post_to_update)) { $post['ID'] = $post_to_update; $group_wp_id = wp_update_post($post); if (!$group_wp_id) { wpcf_admin_message_store(sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" updated', 'wpcf'), $group['post_title'])); } } else { wpcf_admin_message_store(sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } } else { // Insert $group_wp_id = wp_insert_post($post, true); if (is_wp_error($group_wp_id)) { wpcf_admin_message_store(sprintf(__('Group "%s" insert failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" added', 'wpcf'), $group['post_title'])); } } // Update meta if (!empty($group['meta'])) { foreach ($group['meta'] as $meta_key => $meta_value) { if ('_wpcf_conditional_display' == $meta_key) { if (!empty($meta_value)) { $meta_value = wpcf_admin_import_export_simplexml2array($meta_value); if (!is_array($meta_value)) { $meta_value = array(); if ($show_import_fail_version_message) { $show_import_fail_version_message = false; wpcf_admin_message_store(__('The Types settings were not fully imported because it contained unsecured data. You should re-export your Types settings using the latest version of Types', 'wpcf'), 'error'); } } } } update_post_meta($group_wp_id, $meta_key, $meta_value); } } $groups_check[] = $group_wp_id; if (!empty($post_to_update)) { $groups_check[] = $post_to_update; } } } // Delete groups (forced, set in bulk actions) } if ($delete_groups) { $groups_to_delete = get_posts(array('post_type' => TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, 'post_status' => 'any', 'posts_per_page' => -1)); if (!empty($groups_to_delete)) { foreach ($groups_to_delete as $group_to_delete) { if (!in_array($group_to_delete->ID, $groups_check)) { $deleted = wp_delete_post($group_to_delete->ID, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" deleted', 'wpcf'), $group_to_delete->post_title)); } } } } } else { // If not forced, look in POST if (!empty($_POST['groups-to-be-deleted'])) { foreach ($_POST['groups-to-be-deleted'] as $group_to_delete) { $group_to_delete_post = get_post($group_to_delete); if (!empty($group_to_delete_post) && $group_to_delete_post->post_type == TYPES_CUSTOM_FIELD_GROUP_CPT_NAME) { $deleted = wp_delete_post($group_to_delete, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete_post->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" deleted', 'wpcf'), $group_to_delete_post->post_title)); } } else { wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete), 'error'); } } } } // Process fields $fields_check = array(); $fields_existing = wpcf_admin_fields_get_fields(); if (!empty($data->fields)) { $fields = array(); // Set insert data from XML foreach ($data->fields->field as $field) { $field = wpcf_admin_import_export_simplexml2array($field); // Set if submitted in 'types' context if ($context == 'types') { // Process only if marked if (isset($_POST['fields'][$field['id']])) { $fields[$field['id']] = $field; } } else { $fields[$field['id']] = $field; } } // Set insert data from POST if (!empty($_POST['fields'])) { foreach ($_POST['fields'] as $field_id => $field) { if (empty($fields[$field_id])) { continue; } $fields[$field_id]['add'] = !empty($field['add']); $fields[$field_id]['update'] = isset($field['update']) && $field['update'] == 'update' ? true : false; } } // Insert fields foreach ($fields as $field_id => $field) { if (isset($field['add']) && !$field['add'] && !$overwrite_fields) { continue; } if (empty($field['id']) || empty($field['name']) || empty($field['slug'])) { continue; } $field_data = array(); $field_data['description'] = isset($field['description']) ? $field['description'] : ''; $field_data['data'] = isset($field['data']) && is_array($field['data']) ? $field['data'] : array(); foreach (array('id', 'name', 'type', 'slug', 'meta_key', 'meta_type') as $key) { if (array_key_exists($key, $field)) { $field_data[$key] = $field[$key]; } } $fields_existing[$field_id] = $field_data; $fields_check[] = $field_id; // WPML global $iclTranslationManagement; if (!empty($iclTranslationManagement) && isset($field['wpml_action'])) { $iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id] = $field['wpml_action']; $iclTranslationManagement->save_settings(); } wpcf_admin_message_store(sprintf(__('Field "%s" added/updated', 'wpcf'), $field['name'])); } } // Delete fields if ($delete_fields) { foreach ($fields_existing as $k => $v) { if (!empty($v['data']['controlled'])) { continue; } if (!in_array($k, $fields_check)) { wpcf_admin_message_store(sprintf(__('Field "%s" deleted', 'wpcf'), $fields_existing[$k]['name'])); unset($fields_existing[$k]); } } } else { if (!empty($_POST['fields-to-be-deleted'])) { foreach ($_POST['fields-to-be-deleted'] as $field_to_delete) { wpcf_admin_message_store(sprintf(__('Field "%s" deleted', 'wpcf'), $fields_existing[$field_to_delete]['name'])); unset($fields_existing[$field_to_delete]); } } } update_option('wpcf-fields', $fields_existing); // Process user groups //print_r($data->user_groups);exit; $groups_check = array(); if (!empty($data->user_groups) && isset($data->user_groups->group)) { $groups = array(); // Set insert data from XML foreach ($data->user_groups->group as $group) { $group = wpcf_admin_import_export_simplexml2array($group); $groups[$group['ID']] = $group; } // Set insert data from POST if (!empty($_POST['user_groups'])) { foreach ($_POST['user_groups'] as $group_id => $group) { if (empty($groups[$group_id])) { continue; } $groups[$group_id]['add'] = !empty($group['add']); $groups[$group_id]['update'] = isset($group['update']) && $group['update'] == 'update' ? true : false; } } else { foreach ($groups as $group_id => $group) { $groups[$group_id]['add'] = true; $groups[$group_id]['update'] = false; } } // Insert groups foreach ($groups as $group_id => $group) { $post = array('post_status' => $group['post_status'], 'post_type' => TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'post_title' => $group['post_title'], 'post_content' => !empty($group['post_content']) ? $group['post_content'] : ''); if (isset($group['add']) && $group['add']) { $post_to_update = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s", $group['post_title'], TYPES_USER_META_FIELD_GROUP_CPT_NAME)); // Update (may be forced by bulk action) if ($group['update'] || $overwrite_groups && !empty($post_to_update)) { if (!empty($post_to_update)) { $post['ID'] = $post_to_update; $group_wp_id = wp_update_post($post); if (!$group_wp_id) { wpcf_admin_message_store(sprintf(__('User group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('User group "%s" updated', 'wpcf'), $group['post_title'])); } } else { wpcf_admin_message_store(sprintf(__('User group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } } else { // Insert $group_wp_id = wp_insert_post($post, true); if (is_wp_error($group_wp_id)) { wpcf_admin_message_store(sprintf(__('User group "%s" insert failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('User group "%s" added', 'wpcf'), $group['post_title'])); } } // Update meta if (!empty($group['meta'])) { foreach ($group['meta'] as $meta_key => $meta_value) { update_post_meta($group_wp_id, $meta_key, wpcf_admin_import_export_simplexml2array($meta_value)); } } $groups_check[] = $group_wp_id; if (!empty($post_to_update)) { $groups_check[] = $post_to_update; } } } } // Delete groups (forced, set in bulk actions) if ($delete_groups) { $groups_to_delete = get_posts(array('post_type' => TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'post_status' => 'any', 'posts_per_page' => -1)); if (!empty($groups_to_delete)) { foreach ($groups_to_delete as $group_to_delete) { if (!in_array($group_to_delete->ID, $groups_check)) { $deleted = wp_delete_post($group_to_delete->ID, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('User group "%s" deleted', 'wpcf'), $group_to_delete->post_title)); } } } } } else { // If not forced, look in POST if (!empty($_POST['user-groups-to-be-deleted'])) { foreach ($_POST['user-groups-to-be-deleted'] as $group_to_delete) { $group_to_delete_post = get_post($group_to_delete); if (!empty($group_to_delete_post) && $group_to_delete_post->post_type == TYPES_USER_META_FIELD_GROUP_CPT_NAME) { $deleted = wp_delete_post($group_to_delete, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete_post->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('User group "%s" deleted', 'wpcf'), $group_to_delete_post->post_title)); } } else { wpcf_admin_message_store(sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete), 'error'); } } } } // Process fields $fields_existing = wpcf_admin_fields_get_fields(false, false, false, 'wpcf-usermeta'); $fields_check = array(); if (!empty($data->user_fields)) { $fields = array(); // Set insert data from XML foreach ($data->user_fields->field as $field) { $field = wpcf_admin_import_export_simplexml2array($field); // Set if submitted in 'types' context if ($context == 'types') { // Process only if marked if (isset($_POST['user_fields'][$field['id']])) { $fields[$field['id']] = $field; } } else { $fields[$field['id']] = $field; } } // Set insert data from POST if (!empty($_POST['user_fields'])) { foreach ($_POST['user_fields'] as $field_id => $field) { if (empty($fields[$field_id])) { continue; } $fields[$field_id]['add'] = !empty($field['add']); $fields[$field_id]['update'] = isset($field['update']) && $field['update'] == 'update' ? true : false; } } // Insert fields foreach ($fields as $field_id => $field) { if (isset($field['add']) && !$field['add'] && !$overwrite_fields) { continue; } if (empty($field['id']) || empty($field['name']) || empty($field['slug'])) { continue; } $field_data = array(); $field_data['id'] = $field['id']; $field_data['name'] = $field['name']; $field_data['description'] = isset($field['description']) ? $field['description'] : ''; $field_data['type'] = $field['type']; $field_data['slug'] = $field['slug']; $field_data['data'] = isset($field['data']) && is_array($field['data']) ? $field['data'] : array(); $fields_existing[$field_id] = $field_data; $fields_check[] = $field_id; // WPML global $iclTranslationManagement; if (!empty($iclTranslationManagement) && isset($field['wpml_action'])) { $iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id] = $field['wpml_action']; $iclTranslationManagement->save_settings(); } wpcf_admin_message_store(sprintf(__('User field "%s" added/updated', 'wpcf'), $field['name'])); } } // Delete fields if ($delete_fields) { foreach ($fields_existing as $k => $v) { if (!empty($v['data']['controlled'])) { continue; } if (!in_array($k, $fields_check)) { wpcf_admin_message_store(sprintf(__('User field "%s" deleted', 'wpcf'), $fields_existing[$k]['name'])); unset($fields_existing[$k]); } } } else { if (!empty($_POST['user-fields-to-be-deleted'])) { foreach ($_POST['user-fields-to-be-deleted'] as $field_to_delete) { wpcf_admin_message_store(sprintf(__('User field "%s" deleted', 'wpcf'), $fields_existing[$field_to_delete]['name'])); unset($fields_existing[$field_to_delete]); } } } update_option('wpcf-usermeta', $fields_existing); // Process types $types_existing = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); $types_check = array(); if (!empty($data->types) && isset($data->types->type)) { $types = array(); // Set insert data from XML foreach ($data->types->type as $type) { $type = wpcf_admin_import_export_simplexml2array($type); // Set if submitted in 'types' context if ($context == 'types') { if (isset($_POST['types'][$type['id']])) { $types[$type['id']] = $type; } } else { $types[$type['id']] = $type; } } // Set insert data from POST if (!empty($_POST['types'])) { foreach ($_POST['types'] as $type_id => $type) { if (empty($types[$type_id])) { continue; } $types[$type_id]['add'] = !empty($type['add']); $types[$type_id]['update'] = isset($type['update']) && $type['update'] == 'update' ? true : false; } } // Insert types foreach ($types as $type_id => $type) { if (isset($type['add']) && !$type['add'] && !$overwrite_types) { continue; } unset($type['add'], $type['update']); $types_existing[$type_id] = $type; $types_check[] = $type_id; wpcf_admin_message_store(sprintf(__('Custom post type "%s" added/updated', 'wpcf'), $type_id)); } } // Delete types if ($delete_types) { foreach ($types_existing as $k => $v) { if (!in_array($k, $types_check)) { unset($types_existing[$k]); wpcf_admin_message_store(sprintf(__('Custom post type "%s" deleted', 'wpcf'), esc_html($k))); } } } else { if (!empty($_POST['types-to-be-deleted'])) { foreach ($_POST['types-to-be-deleted'] as $type_to_delete) { wpcf_admin_message_store(sprintf(__('Custom post type "%s" deleted', 'wpcf'), $types_existing[$type_to_delete]['labels']['name'])); unset($types_existing[$type_to_delete]); } } } update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $types_existing); // Process taxonomies $taxonomies_existing = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); $taxonomies_check = array(); if (!empty($data->taxonomies) && isset($data->taxonomies->taxonomy)) { $taxonomies = array(); // Set insert data from XML foreach ($data->taxonomies->taxonomy as $taxonomy) { $taxonomy = wpcf_admin_import_export_simplexml2array($taxonomy); // Set if submitted in 'types' context if ($context == 'types') { if (isset($_POST['taxonomies'][$taxonomy['id']])) { $taxonomies[$taxonomy['id']] = $taxonomy; } } else { $taxonomies[$taxonomy['id']] = $taxonomy; } } // Set insert data from POST if (!empty($_POST['taxonomies'])) { foreach ($_POST['taxonomies'] as $taxonomy_id => $taxonomy) { if (empty($taxonomies[$taxonomy_id])) { continue; } $taxonomies[$taxonomy_id]['add'] = !empty($taxonomy['add']); $taxonomies[$taxonomy_id]['update'] = isset($taxonomy['update']) && $taxonomy['update'] == 'update' ? true : false; } } // Insert taxonomies foreach ($taxonomies as $taxonomy_id => $taxonomy) { if (isset($taxonomy['add']) && !$taxonomy['add'] && !$overwrite_tax) { continue; } unset($taxonomy['add'], $taxonomy['update']); $taxonomies_existing[$taxonomy_id] = $taxonomy; $taxonomies_check[] = $taxonomy_id; wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" added/updated', 'wpcf'), $taxonomy_id)); } } /** * reset TOOLSET_EDIT_LAST */ if ($data_installer) { $data_installer->reset_toolset_edit_last(); } // Delete taxonomies if ($delete_tax) { foreach ($taxonomies_existing as $k => $v) { if (!in_array($k, $taxonomies_check)) { unset($taxonomies_existing[$k]); wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" deleted', 'wpcf'), $k)); } } } else { if (!empty($_POST['taxonomies-to-be-deleted'])) { foreach ($_POST['taxonomies-to-be-deleted'] as $taxonomy_to_delete) { wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" deleted', 'wpcf'), $taxonomies_existing[$taxonomy_to_delete]['labels']['name'])); unset($taxonomies_existing[$taxonomy_to_delete]); } } } update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $taxonomies_existing); // Add relationships if (!empty($data->post_relationships) && !empty($_POST['post_relationship'])) { $relationship_existing = get_option('wpcf_post_relationship', array()); /** * be sure, $relationship_existing is a array! */ if (!is_array($relationship_existing)) { $relationship_existing = array(); } $relationship = json_decode($data->post_relationships->data, true); if (is_array($relationship)) { $relationship = array_merge($relationship_existing, $relationship); update_option('wpcf_post_relationship', $relationship); wpcf_admin_message_store(__('Post relationships created', 'wpcf')); } else { wpcf_admin_message_store(__('Post relationships settings were not imported because it contained unsecured data. You should re-export your Types settings using the latest version of Types', 'wpcf'), 'error'); } } // WPML bulk registration if (wpcf_get_settings('register_translations_on_import')) { wpcf_admin_bulk_string_translation(); } // Flush rewrite rules wpcf_init_custom_types_taxonomies(); flush_rewrite_rules(); if ($redirect) { echo '<script type="text/javascript"> <!-- window.location = "' . admin_url('admin.php?page=wpcf-import-export') . '" //--> </script>'; die; } }
/** * Update posts checkboxes fields. * * @param type $field * @param type $action * @return boolean|int */ function wpcf_admin_fields_checkboxes_migrate_empty($field, $action) { $field = wpcf_admin_fields_get_field($field); if (empty($field) || $field['type'] != 'checkboxes' || empty($field['data']['options'])) { return false; } $option = get_option('wpcf_checkboxes_migration', array()); $meta_key = wpcf_types_get_meta_prefix($field) . $field['id']; if (empty($option[$action])) { $posts = wpcf_admin_fields_checkboxes_migrate_empty_check($field['id'], $action . '_check'); } else { $posts = $option[$action]; } if (!empty($posts)) { if ($action == 'do_not_save') { $count = 0; foreach ($posts as $temp_key => $post_id) { if ($count == 1000) { $option[$action] = $posts; update_option('wpcf_checkboxes_migration', $option); $data = array('offset' => $temp_key); return $data; } $meta_saved = get_post_meta($post_id, $meta_key); if (!empty($meta_saved)) { foreach ($meta_saved as $key => $value) { if (!is_array($value)) { $value_check = array(); } else { $value_check = $value; } foreach ($field['data']['options'] as $option_id => $option_data) { if (isset($value_check[$option_id])) { unset($value_check[$option_id]); } } update_post_meta($post_id, $meta_key, $value_check, $value); } } unset($posts[$temp_key]); $count++; } unset($option[$action]); update_option('wpcf_checkboxes_migration', $option); return $posts; } else { if ($action == 'save') { $count = 0; foreach ($posts as $temp_key => $post_id) { if ($count == 1000) { $option[$action] = $posts; update_option('wpcf_checkboxes_migration', $option); $data = array('offset' => $temp_key); return $data; } $meta_saved = get_post_meta($post_id, $meta_key); if (!empty($meta_saved)) { foreach ($meta_saved as $key => $value) { if (!is_array($value)) { $value_check = array(); } else { $value_check = $value; } $set_value = array(); foreach ($field['data']['options'] as $option_id => $option_data) { if (!isset($value_check[$option_id])) { $set_value[$option_id] = 0; } } $updated_value = $value_check + $set_value; update_post_meta($post_id, $meta_key, $updated_value, $value); } } unset($posts[$temp_key]); $count++; } unset($option[$action]); update_option('wpcf_checkboxes_migration', $option); return $posts; } } } return false; }
/** * Summary. * * Description. * * @since x.x.x * @access (for functions: only use if private) * * @see Function/method/class relied on * @link URL * @global type $varname Description. * @global type $varname Description. * * @param type $var Description. * @param type $var Optional. Description. * @return type Description. */ public function wpml($form_data) { $form = array(); if (true && isset($form_data['meta_type']) && isset($form_data['type']) && isset($form_data['id']) && 'postmeta' == $form_data['meta_type'] && function_exists('wpml_cf_translation_preferences')) { $custom_field = !empty($form_data['slug']) ? wpcf_types_get_meta_prefix($form_data) . $form_data['slug'] : false; $suppress_errors = $custom_field == false ? true : false; $translatable = array('textfield', 'textarea', 'wysiwyg'); $action = in_array($form_data['type'], $translatable) ? 'translate' : 'copy'; $wpml_prefs = wpml_cf_translation_preferences($form_data['id'], $custom_field, 'wpcf', false, $action, false, $suppress_errors); $wpml_prefs = str_replace('<span style="color:#FF0000;">', '<span class="wpcf-form-error">', $wpml_prefs); $form['wpcf-' . $form_data['id'] . '-wpml-preferences'] = array('#title' => __('Translation preferences', 'wpcf'), '#type' => 'markup', '#markup' => $wpml_prefs, '#pattern' => '<tr><td><LABEL></td><td><ERROR><BEFORE><ELEMENT><AFTER></td></tr>'); } return $form; }
function wpv_ajax_wpv_get_types_field_name() { // TODO check where this is used, maybe create a function to handle it past wpnonce verification if (wp_verify_nonce($_POST['wpv_nonce'], 'wpv_get_types_field_name_nonce')) { if (!defined('WPCF_VERSION')) { echo json_encode(array('found' => false, 'name' => $_POST['field'])); } else { if (defined('WPCF_INC_ABSPATH')) { require_once WPCF_INC_ABSPATH . '/fields.php'; } if (function_exists('wpcf_admin_fields_get_fields')) { $fields = wpcf_admin_fields_get_fields(); } else { $fields = array(); } $found = false; foreach ($fields as $field) { if ($_POST['field'] == wpcf_types_get_meta_prefix($field) . $field['slug']) { echo json_encode(array('found' => true, 'name' => $field['name'])); $found = true; break; } } if (!$found) { echo json_encode(array('found' => false, 'name' => $_POST['field'])); } } } die; }
/** * Calls view function for specific field type. * * @param type $field * @param type $atts * @return type */ function types_render_field($field_id = null, $params = array(), $content = null, $code = '') { if (empty($field_id)) { return ''; } global $wpcf; // HTML var holds actual output $html = ''; // Set post ID to global $post_id = get_the_ID(); // Check if other post required if (isset($params['post_id'])) { // If numeric value if (is_numeric($params['post_id'])) { $post_id = intval($params['post_id']); // WP parent } else { if ($params['post_id'] == '$parent') { $current_post = get_post($post_id); if (empty($current_post->post_parent)) { return ''; } $post_id = $current_post->post_parent; // Types parent } else { if (strpos($params['post_id'], '$') === 0) { $post_id = intval(WPCF_Relationship::get_parent($post_id, trim($params['post_id'], '$'))); } } } } if (empty($post_id)) { return ''; } // Set post $post = get_post($post_id); if (empty($post)) { return ''; } // Get field $field = types_get_field($field_id); // If field not found return empty string if (empty($field)) { // Log if (!function_exists('wplogger')) { require_once WPCF_EMBEDDED_ABSPATH . '/common/wplogger.php'; } global $wplogger; $wplogger->log('types_render_field call for missing field \'' . $field_id . '\'', WPLOG_DEBUG); return ''; } // Set field $wpcf->field->set($post, $field); // See if repetitive if (types_is_repetitive($field)) { $wpcf->repeater->set($post_id, $field); $_meta = $wpcf->repeater->_get_meta(); $meta = $_meta['custom_order']; // Sometimes if meta is empty - array(0 => '') is returned if (count($meta) == 1 && reset($meta) == '') { return ''; } if (!empty($meta)) { $output = ''; if (isset($params['index'])) { $index = $params['index']; } else { $index = ''; } // Allow wpv-for-each shortcode to set the index $index = apply_filters('wpv-for-each-index', $index); if ($index === '') { $output = array(); foreach ($meta as $temp_key => $temp_value) { $params['field_value'] = $temp_value; $temp_output = types_render_field_single($field, $params, $content, $code, $temp_key); if (!empty($temp_output)) { $output[] = $temp_output; } } if (!empty($output) && isset($params['separator']) && $params['separator'] !== '') { $output = implode(html_entity_decode($params['separator']), $output); } else { if (!empty($output)) { $output = implode(' ', $output); } else { return ''; } } } else { // Make sure indexed right $_index = 0; foreach ($meta as $temp_key => $temp_value) { if ($_index == $index) { $params['field_value'] = $temp_value; return types_render_field_single($field, $params, $content, $code, $temp_key); } $_index++; } // If missed index return ''; } $html = $output; } else { return ''; } } else { // Non-repetitive field $params['field_value'] = wpcf_get_post_meta($post_id, wpcf_types_get_meta_prefix($field) . $field['slug'], true); if ($params['field_value'] == '' && $field['type'] != 'checkbox') { return ''; } $html = types_render_field_single($field, $params, $content, $code, $wpcf->field->meta_object->meta_id); } return $wpcf->field->html($html, $params); }
/** * wpcf_get_fields * * returns the fields handled by types * */ function wpcf_get_post_meta_field_names() { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; $fields = wpcf_admin_fields_get_fields(); $field_names = array(); foreach ($fields as $field) { $field_names[] = wpcf_types_get_meta_prefix($field) . $field['slug']; } return $field_names; }
/** * Adds items to view dropdown. * * Core function. Works and stable. Do not move or change. * If required, add hooks only. * * @param type $items * @return type */ function wpcf_admin_post_editor_addon_menus_filter($items) { global $wpcf; $groups = wpcf_admin_fields_get_groups(); $all_post_types = implode(' ', get_post_types(array('public' => true))); $add = array(); if (!empty($groups)) { // $group_id is blank therefore not equal to $group['id'] // use array for item key and CSS class $item_styles = array(); foreach ($groups as $group_id => $group) { $fields = wpcf_admin_fields_get_fields_by_group($group['id'], 'slug', true, false, true); if (!empty($fields)) { // code from Types used here without breaking the flow // get post types list for every group or apply all $post_types = get_post_meta($group['id'], '_wp_types_group_post_types', true); if ($post_types == 'all') { $post_types = $all_post_types; } $post_types = trim(str_replace(',', ' ', $post_types)); $item_styles[$group['name']] = $post_types; foreach ($fields as $field_id => $field) { // Use field class $wpcf->field->set($wpcf->post, $field); // Get field data $data = (array) $wpcf->field->config; // Get inherited field if (isset($data['inherited_field_type'])) { $inherited_field_data = wpcf_fields_type_action($data['inherited_field_type']); } $callback = ''; if (isset($data['editor_callback'])) { $callback = sprintf($data['editor_callback'], $field['id']); } else { // Set callback if function exists $function = 'wpcf_fields_' . $field['type'] . '_editor_callback'; $callback = function_exists($function) ? 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\')' : ''; } $add[$group['name']][stripslashes($field['name'])] = array(stripslashes($field['name']), trim(wpcf_fields_get_shortcode($field), '[]'), $group['name'], $callback); /* * Since Types 1.2 * We use field class to enqueue JS and CSS */ $wpcf->field->enqueue_script(); $wpcf->field->enqueue_style(); } } } } $search_key = ''; // Iterate all items to be displayed in the "V" menu foreach ($items as $key => $item) { if ($key == __('Basic', 'wpv-views')) { $search_key = 'found'; continue; } if ($search_key == 'found') { $search_key = $key; } if ($key == __('Field', 'wpv-views') && isset($item[trim(wpcf_types_get_meta_prefix(), '-')])) { unset($items[$key][trim(wpcf_types_get_meta_prefix(), '-')]); } } if (empty($search_key) || $search_key == 'found') { $search_key = count($items); } $insert_position = array_search($search_key, array_keys($items)); $part_one = array_slice($items, 0, $insert_position); $part_two = array_slice($items, $insert_position); $items = $part_one + $add + $part_two; // apply CSS styles to each item based on post types foreach ($items as $key => $value) { if (isset($item_styles[$key])) { $items[$key]['css'] = $item_styles[$key]; } else { $items[$key]['css'] = $all_post_types; } } return $items; }
function wpcf_admin_userprofilesave_init($user_id) { if (defined('WPTOOLSET_FORMS_VERSION')) { global $wpcf; $errors = false; /** * check checkbox type fields to delete or save empty if needed */ $groups = wpcf_admin_usermeta_get_groups_fields(); foreach ($groups as $group) { if (!array_key_exists('fields', $group) || empty($group['fields'])) { continue; } foreach ($group['fields'] as $field) { switch ($field['type']) { case 'checkboxes': if (!array_key_exists('wpcf', $_POST) || !array_key_exists($field['slug'], $_POST['wpcf'])) { delete_user_meta($user_id, $field['meta_key']); } break; case 'checkbox': if (!array_key_exists('wpcf', $_POST) || !array_key_exists($field['slug'], $_POST['wpcf'])) { if ('yes' == $field['data']['save_empty']) { $_POST['wpcf'][$field['slug']] = 0; } else { delete_user_meta($user_id, $field['meta_key']); } } break; } } } // Save meta fields if (!empty($_POST['wpcf'])) { foreach ($_POST['wpcf'] as $field_slug => $field_value) { // Get field by slug $field = wpcf_fields_get_field_by_slug($field_slug, 'wpcf-usermeta'); if (empty($field)) { continue; } // Skip copied fields if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) { continue; } $_field_value = !types_is_repetitive($field) ? array($field_value) : $field_value; // Set config $config = wptoolset_form_filter_types_field($field, $user_id); foreach ($_field_value as $_k => $_val) { // Check if valid $valid = wptoolset_form_validate_field('your-profile', $config, $_val); if (is_wp_error($valid)) { $errors = true; $_errors = $valid->get_error_data(); $_msg = sprintf(__('Field "%s" not updated:', 'wpcf'), $field['name']); wpcf_admin_message_store($_msg . ' ' . implode(', ', $_errors), 'error'); if (types_is_repetitive($field)) { unset($field_value[$_k]); } else { break; } } } // Save field if (types_is_repetitive($field)) { $wpcf->usermeta_repeater->set($user_id, $field); $wpcf->usermeta_repeater->save($field_value); } else { $wpcf->usermeta_field->set($user_id, $field); $wpcf->usermeta_field->usermeta_save($field_value); } do_action('wpcf_user_field_saved', $user_id, $field); // TODO Move to checkboxes if ($field['type'] == 'checkboxes') { $field_data = wpcf_admin_fields_get_field($field['id'], false, false, false, 'wpcf-usermeta'); if (!empty($field_data['data']['options'])) { $update_data = array(); foreach ($field_data['data']['options'] as $option_id => $option_data) { if (!isset($_POST['wpcf'][$field['id']][$option_id])) { if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') { $update_data[$option_id] = 0; } } else { $update_data[$option_id] = $_POST['wpcf'][$field['id']][$option_id]; } } update_user_meta($user_id, $field['meta_key'], $update_data); } } } } if ($errors) { update_post_meta($user_id, '__wpcf-invalid-fields', true); } do_action('wpcf_user_saved', $user_id); return; } global $wpcf; $all_fields = array(); $_not_valid = array(); $_error = false; $error = ''; $groups = $groups = wpcf_admin_usermeta_get_groups_fields(); if (empty($groups)) { return false; } foreach ($groups as $group) { // Process fields $fields = wpcf_admin_usermeta_process_fields($user_id, $group['fields'], true, false, 'validation'); // Validate fields $form = wpcf_form_simple_validate($fields); $all_fields = $all_fields + $fields; // Collect all not valid fields if ($form->isError()) { $_error = true; // Set error only to true $_not_valid = array_merge($_not_valid, (array) $form->get_not_valid()); } } // Set fields foreach ($all_fields as $k => $v) { // only Types field if (empty($v['wpcf-id'])) { continue; } $_temp = new WPCF_Usermeta_Field(); $_temp->set($user_id, $v['wpcf-id']); $all_fields[$k]['_field'] = $_temp; } foreach ($_not_valid as $k => $v) { // only Types field if (empty($v['wpcf-id'])) { continue; } $_temp = new WPCF_Usermeta_Field(); $_temp->set($user_id, $v['wpcf-id']); $_not_valid[$k]['_field'] = $_temp; } $not_valid = apply_filters('wpcf_post_form_not_valid', $_not_valid, $_error, $all_fields); // Notify user about error if ($error) { wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error'); } /* * Save invalid elements so user can be informed after redirect. */ if (!empty($not_valid)) { update_user_meta($user_id, 'wpcf-invalid-fields', $not_valid); } if (!empty($_POST['wpcf'])) { foreach ($_POST['wpcf'] as $field_slug => $field_value) { $field = wpcf_fields_get_field_by_slug($field_slug, 'wpcf-usermeta'); if (empty($field)) { continue; } $wpcf->usermeta_field->set($user_id, $field); if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) { continue; } if (isset($_POST['__wpcf_repetitive'][$wpcf->usermeta_field->slug])) { $wpcf->usermeta_repeater->set($user_id, $field); $wpcf->usermeta_repeater->save(); } else { $wpcf->usermeta_field->usermeta_save(); } do_action('wpcf_post_field_saved', '', $field); } //end foreach } //end if foreach ($all_fields as $field) { if (!isset($field['#type'])) { continue; } if ($field['#type'] == 'checkbox') { $field_data = wpcf_admin_fields_get_field($field['wpcf-id'], false, false, false, 'wpcf-usermeta'); if (!isset($_POST['wpcf'][$field['wpcf-slug']])) { if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') { update_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], 0); } else { delete_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug']); } } } if ($field['#type'] == 'checkboxes') { $field_data = wpcf_admin_fields_get_field($field['wpcf-id'], false, false, false, 'wpcf-usermeta'); if (!empty($field_data['data']['options'])) { $update_data = array(); foreach ($field_data['data']['options'] as $option_id => $option_data) { if (!isset($_POST['wpcf'][$field['wpcf-slug']][$option_id])) { if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') { $update_data[$option_id] = 0; } } else { $update_data[$option_id] = $_POST['wpcf'][$field['wpcf-slug']][$option_id]; } } update_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], $update_data); } } } }
/** * Processes field form data. * * @param type $type * @param type $form_data * @return type */ function wpcf_fields_get_field_form_data($type, $form_data = array()) { // Get field type data $field_data = wpcf_fields_type_action($type); if (!empty($field_data)) { // require_once $filename; $form = array(); // Set right ID if existing field if (isset($form_data['submitted_key'])) { $id = $form_data['submitted_key']; } else { $id = $type . '-' . rand(); } // Set remove link $remove_link = isset($form_data['group_id']) ? admin_url('admin-ajax.php?' . 'wpcf_ajax_callback=wpcfFieldsFormDeleteElement&wpcf_warning=' . __('Are you sure?', 'wpcf') . '&action=wpcf_ajax&wpcf_action=remove_field_from_group' . '&group_id=' . intval($form_data['group_id']) . '&field_id=' . $form_data['id']) . '&_wpnonce=' . wp_create_nonce('remove_field_from_group') : admin_url('admin-ajax.php?' . 'wpcf_ajax_callback=wpcfFieldsFormDeleteElement&wpcf_warning=' . __('Are you sure?', 'wpcf') . '&action=wpcf_ajax&wpcf_action=remove_field_from_group') . '&_wpnonce=' . wp_create_nonce('remove_field_from_group'); // Set move button $form['wpcf-' . $id . '-control'] = array('#type' => 'markup', '#markup' => '<img src="' . WPCF_RES_RELPATH . '/images/move.png" class="wpcf-fields-form-move-field" alt="' . __('Move this field', 'wpcf') . '" /><a href="' . $remove_link . '" ' . 'class="wpcf-form-fields-delete wpcf-ajax-link">' . '<img src="' . WPCF_RES_RELPATH . '/images/delete-2.png" alt="' . __('Delete this field', 'wpcf') . '" /></a>'); // Set fieldset $collapsed = wpcf_admin_fields_form_fieldset_is_collapsed('fieldset-' . $id); // Set collapsed on AJAX call (insert) $collapsed = defined('DOING_AJAX') ? false : $collapsed; // Set title $title = !empty($form_data['name']) ? $form_data['name'] : __('Untitled', 'wpcf'); $title = '<span class="wpcf-legend-update">' . $title . '</span> - ' . sprintf(__('%s field', 'wpcf'), $field_data['title']); $form['wpcf-' . $id] = array('#type' => 'fieldset', '#title' => $title, '#id' => 'fieldset-' . $id, '#collapsible' => true, '#collapsed' => $collapsed); // Get init data $field_init_data = wpcf_fields_type_action($type); // See if field inherits some other $inherited_field_data = false; if (isset($field_init_data['inherited_field_type'])) { $inherited_field_data = wpcf_fields_type_action($field_init_data['inherited_field_type']); } $form_field = array(); // Force name and description $form_field['name'] = array('#type' => 'textfield', '#name' => 'name', '#attributes' => array('class' => 'wpcf-forms-set-legend', 'style' => 'width:100%;margin:10px 0 10px 0;'), '#validate' => array('required' => array('value' => true)), '#inline' => true, '#value' => __('Enter field name', 'wpcf')); if (empty($form_data['name'])) { $form_field['name']['#attributes']['onclick'] = 'if (jQuery(this).val() == \'' . __('Enter field name', 'wpcf') . '\') { jQuery(this).val(\'\'); }'; $form_field['name']['#attributes']['onblur'] = 'if (jQuery(this).val() == \'\') { jQuery(this).val(\'' . __('Enter field name', 'wpcf') . '\') }'; } $form_field['slug'] = array('#type' => 'textfield', '#name' => 'slug', '#attributes' => array('style' => 'width:100%;margin:0 0 10px 0;'), '#validate' => array('nospecialchars' => array('value' => true)), '#inline' => true, '#value' => __('Enter field slug', 'wpcf')); if (empty($form_data['slug'])) { $form_field['slug']['#attributes']['onclick'] = 'if (jQuery(this).val() == \'' . __('Enter field slug', 'wpcf') . '\') { jQuery(this).val(\'\'); }'; $form_field['slug']['#attributes']['onblur'] = 'if (jQuery(this).val() == \'\') { jQuery(this).val(\'' . __('Enter field slug', 'wpcf') . '\') }'; } // If insert form callback is not provided, use generic form data if (function_exists('wpcf_fields_' . $type . '_insert_form')) { $form_field_temp = call_user_func('wpcf_fields_' . $type . '_insert_form', $form_data, 'wpcf[fields][' . $id . ']'); if (is_array($form_field_temp)) { unset($form_field_temp['name'], $form_field_temp['slug']); $form_field = $form_field + $form_field_temp; } } $form_field['description'] = array('#type' => 'textarea', '#name' => 'description', '#attributes' => array('rows' => 5, 'cols' => 1, 'style' => 'margin:0 0 10px 0;'), '#inline' => true, '#value' => __('Describe this field', 'wpcf')); if (empty($form_data['description'])) { $form_field['description']['#attributes']['onfocus'] = 'if (jQuery(this).val() == \'' . __('Describe this field', 'wpcf') . '\') { jQuery(this).val(\'\'); }'; $form_field['description']['#attributes']['onblur'] = 'if (jQuery(this).val() == \'\') { jQuery(this).val(\'' . __('Describe this field', 'wpcf') . '\') }'; } // Process all form fields foreach ($form_field as $k => $field) { $form['wpcf-' . $id][$k] = $field; // Check if nested if (isset($field['#name']) && strpos($field['#name'], '[') === false) { $form['wpcf-' . $id][$k]['#name'] = 'wpcf[fields][' . $id . '][' . $field['#name'] . ']'; } else { if (isset($field['#name'])) { $form['wpcf-' . $id][$k]['#name'] = 'wpcf[fields][' . $id . ']' . $field['#name']; } } if (!isset($field['#id'])) { $form['wpcf-' . $id][$k]['#id'] = $type . '-' . $field['#type'] . '-' . rand(); } if (isset($field['#name']) && isset($form_data[$field['#name']])) { $form['wpcf-' . $id][$k]['#value'] = $form_data[$field['#name']]; $form['wpcf-' . $id][$k]['#default_value'] = $form_data[$field['#name']]; // Check if it's in 'data' } else { if (isset($field['#name']) && isset($form_data['data'][$field['#name']])) { $form['wpcf-' . $id][$k]['#value'] = $form_data['data'][$field['#name']]; $form['wpcf-' . $id][$k]['#default_value'] = $form_data['data'][$field['#name']]; } } } // Set type $form['wpcf-' . $id]['type'] = array('#type' => 'hidden', '#name' => 'wpcf[fields][' . $id . '][type]', '#value' => $type, '#id' => $id . '-type'); // Add validation box $form_validate = wpcf_admin_fields_form_validation('wpcf[fields][' . $id . '][validate]', call_user_func('wpcf_fields_' . $type), $form_data); foreach ($form_validate as $k => $v) { $form['wpcf-' . $id][$k] = $v; } // WPML Translation Preferences if (function_exists('wpml_cf_translation_preferences')) { $custom_field = !empty($form_data['slug']) ? wpcf_types_get_meta_prefix($form_data) . $form_data['slug'] : false; $translatable = array('textfield', 'textarea', 'wysiwyg'); $action = in_array($type, $translatable) ? 'translate' : 'copy'; $form['wpcf-' . $id]['wpml-preferences'] = array('#type' => 'fieldset', '#title' => __('Translation preferences', 'wpcf'), '#collapsed' => true); $form['wpcf-' . $id]['wpml-preferences']['form'] = array('#type' => 'markup', '#markup' => wpml_cf_translation_preferences($id, $custom_field, 'wpcf', false, $action)); } if (empty($form_data) || isset($form_data['is_new'])) { $form['wpcf-' . $id]['is_new'] = array('#type' => 'hidden', '#name' => 'wpcf[fields][' . $id . '][is_new]', '#value' => '1'); } return $form; } return false; }
/** * Editor callback form. */ function wpcf_fields_file_editor_callback() { wp_enqueue_style('wpcf-fields-file', WPCF_EMBEDDED_RES_RELPATH . '/css/basic.css', array(), WPCF_VERSION); // Get field $field = wpcf_admin_fields_get_field($_GET['field_id']); if (empty($field)) { _e('Wrong field specified', 'wpcf'); die; } // Get post_ID $post_ID = false; if (isset($_POST['post_id'])) { $post_ID = intval($_POST['post_id']); } else { $http_referer = explode('?', $_SERVER['HTTP_REFERER']); parse_str($http_referer[1], $http_referer); if (isset($http_referer['post'])) { $post_ID = $http_referer['post']; } } // Get attachment $attachment_id = false; if ($post_ID) { $file = get_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true); if (!empty($file)) { // Get attachment by guid global $wpdb; $attachment_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts}\r\n WHERE post_type = 'attachment' AND guid=%s", $file)); } } $last_settings = wpcf_admin_fields_get_field_last_settings($_GET['field_id']); $form = array(); $form['#form']['callback'] = 'wpcf_fields_file_editor_submit'; if ($attachment_id) { $form['preview'] = array('#type' => 'markup', '#markup' => '<div class="message updated" style="margin: 0 0 20px 0"><p>' . $file . '</p></div>'); } $form['link'] = array('#type' => 'checkbox', '#title' => __('Display as link', 'wpcf'), '#name' => 'link', '#default_value' => isset($last_settings['link']) ? $last_settings['link'] : 1); $form['title'] = array('#type' => 'textfield', '#title' => __('Link title', 'wpcf'), '#name' => 'title', '#value' => isset($last_settings['title']) ? $last_settings['title'] : ''); $form['class'] = array('#type' => 'textfield', '#title' => __('Class', 'wpcf'), '#name' => 'class', '#value' => isset($last_settings['class']) ? $last_settings['class'] : ''); $form['style'] = array('#type' => 'textfield', '#title' => __('Style', 'wpcf'), '#name' => 'style', '#value' => isset($last_settings['style']) ? $last_settings['style'] : ''); $form['submit'] = array('#type' => 'submit', '#name' => 'submit', '#value' => __('Insert shortcode', 'wpcf'), '#attributes' => array('class' => 'button-primary')); $f = wpcf_form('wpcf-form', $form); wpcf_admin_ajax_head('Insert email', 'wpcf'); echo '<form method="post" action="">'; echo $f->renderForm(); echo '</form>'; wpcf_admin_ajax_footer(); }
/** * Adds items to view dropdown. * * @param type $items * @return type */ public static function editorDropdownFilter($items) { $post = wpcf_admin_get_edited_post(); if (empty($post)) { $post = (object) array('ID' => -1); } $groups = wpcf_admin_fields_get_groups('wp-types-group', 'group_active'); $all_post_types = implode(' ', get_post_types(array('public' => true))); $add = array(); if (!empty($groups)) { // $group_id is blank therefore not equal to $group['id'] // use array for item key and CSS class $item_styles = array(); foreach ($groups as $group) { $fields = wpcf_admin_fields_get_fields_by_group($group['id'], 'slug', true, false, true); if (!empty($fields)) { // code from Types used here without breaking the flow // get post types list for every group or apply all $post_types = get_post_meta($group['id'], '_wp_types_group_post_types', true); if ($post_types == 'all') { $post_types = $all_post_types; } $post_types = trim(str_replace(',', ' ', $post_types)); $item_styles[$group['name']] = $post_types; foreach ($fields as $field) { $callback = 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\', \'postmeta\', ' . $post->ID . ')'; $add[$group['name']][stripslashes($field['name'])] = array(stripslashes($field['name']), trim(wpcf_fields_get_shortcode($field), '[]'), $group['name'], $callback); // TODO Remove - it's not post edit screen (meta box JS and CSS) WPCF_Fields::enqueueScript($field['type']); WPCF_Fields::enqueueStyle($field['type']); } } } } $search_key = ''; // Iterate all items to be displayed in the "V" menu foreach ($items as $key => $item) { if ($key == __('Basic', 'wpv-views')) { $search_key = 'found'; continue; } if ($search_key == 'found') { $search_key = $key; } if ($key == __('Field', 'wpv-views') && isset($item[trim(wpcf_types_get_meta_prefix(), '-')])) { unset($items[$key][trim(wpcf_types_get_meta_prefix(), '-')]); } } if (empty($search_key) || $search_key == 'found') { $search_key = count($items); } $insert_position = array_search($search_key, array_keys($items)); $part_one = array_slice($items, 0, $insert_position); $part_two = array_slice($items, $insert_position); $items = $part_one + $add + $part_two; // apply CSS styles to each item based on post types foreach ($items as $key => $value) { if (isset($item_styles[$key])) { $items[$key]['css'] = $item_styles[$key]; } else { $items[$key]['css'] = $all_post_types; } } return $items; }
/** * Creates form elements. * * Core function. Works and stable. Do not move or change. * If required, add hooks only. * * @param type $post * @param type $fields * @return type */ function wpcf_admin_post_process_fields($post = false, $fields = array(), $use_cache = true, $add_to_editor = true, $context = 'group') { global $wpcf; $wpcf->field->use_cache = $use_cache; $wpcf->field->add_to_editor = $add_to_editor; $wpcf->repeater->use_cache = $use_cache; $wpcf->repeater->add_to_editor = $add_to_editor; // Get cached static $cache = array(); $cache_key = !empty($post->ID) ? $post->ID . md5(serialize($fields)) : false; if ($use_cache && $cache_key && isset($cache[$cache_key])) { return $cache[$cache_key]; } $fields_processed = array(); // Get invalid fields (if submitted) $invalid_fields = array(); if (!empty($post->ID)) { $invalid_fields = get_post_meta($post->ID, 'wpcf-invalid-fields', true); delete_post_meta($post->ID, 'wpcf-invalid-fields'); /* * * Add it to global $wpcf * From now on take it there. */ $wpcf->field->invalid_fields = $invalid_fields; } // TODO WPML Get WPML original fields $original_cf = array(); if (function_exists('wpml_get_copied_fields_for_post_edit') && !wpcf_wpml_post_is_original($post)) { $__fields_slugs = array(); foreach ($fields as $_f) { $__fields_slugs[] = $_f['meta_key']; } $original_cf = wpml_get_copied_fields_for_post_edit($__fields_slugs); } foreach ($fields as $field) { // Repetitive fields if (wpcf_admin_is_repetitive($field) && $context != 'post_relationship') { // First check if repetitive fields are copied using WPML /* * TODO All WPML specific code needs moving to * /embedded/includes/wpml.php * * @since Types 1.2 */ // TODO WPML move if (!empty($original_cf['fields']) && in_array(wpcf_types_get_meta_prefix($field) . $field['slug'], $original_cf['fields'])) { /* * See if repeater can handle copied fields */ $wpcf->repeater->set(get_post($original_cf['original_post_id']), $field); $fields_processed = $fields_processed + $wpcf->repeater->get_fields_form(); } else { // Set repeater /* * * * @since Types 1.2 * Now we're using repeater class to handle repetitive forms. * Main change is - use form from $field_meta_box_form() without * re-processing form elements. * * Field should pass form as array: * 'my_checkbox' => array('#type' => 'checkbox' ...), * 'my_textfield' => array('#type' => 'textfield' ...), * * In form it should set values to be stored. * Use hooks to adjust saved data. */ $wpcf->repeater->set($post, $field); $fields_processed = $fields_processed + $wpcf->repeater->get_fields_form(); } /* * * * * * * * * * * * * * * * * Non-repetitive fields */ } else { /* * meta_form will be treated as normal form. * See if any obstacles prevent us from using completed * form from config files. * * Main change is - use form from $field_meta_box_form() without * re-processing form elements. * * Field should pass form as array: * 'my_checkbox' => array('#type' => 'checkbox' ...), * 'my_textfield' => array('#type' => 'textfield' ...), * * In form it should set values to be stored. * Use hooks to adjust saved data. */ $wpcf->field->set($post, $field); // TODO WPML move Check if repetitive field is copied using WPML if (!empty($original_cf['fields'])) { if (in_array($wpcf->field->slug, $original_cf['fields'])) { // Switch to parent post $wpcf->field->set(get_post($original_cf['original_post_id']), $field); } } /* * From Types 1.2 use complete form setup */ $fields_processed = $fields_processed + $wpcf->field->_get_meta_form(); } } // Cache results if ($cache_key) { $cache[$cache_key] = $fields_processed; } return $fields_processed; }
/** * All AJAX calls go here. */ function wpcf_ajax_embedded() { if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $_REQUEST['wpcf_action'])) { die('Verification failed'); } switch ($_REQUEST['wpcf_action']) { case 'editor_insert_date': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/date.php'; wpcf_fields_date_editor_form(); break; case 'insert_skype_button': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/skype.php'; wpcf_fields_skype_meta_box_ajax(); break; case 'editor_callback': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; $field = wpcf_admin_fields_get_field($_GET['field_id']); if (!empty($field)) { $function = 'wpcf_fields_' . $field['type'] . '_editor_callback'; if (function_exists($function)) { call_user_func($function); } } break; case 'dismiss_message': if (isset($_GET['id'])) { $messages = get_option('wpcf_dismissed_messages', array()); $messages[] = $_GET['id']; update_option('wpcf_dismissed_messages', $messages); } break; case 'pr_add_child_post': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = 'Passed wrong parameters'; if (isset($_GET['post_id']) && isset($_GET['post_type_child']) && isset($_GET['post_type_parent'])) { $relationships = get_option('wpcf_post_relationship', array()); $post = get_post($_GET['post_id']); $post_type = $_GET['post_type_child']; $parent_post_type = $_GET['post_type_parent']; $data = $relationships[$parent_post_type][$post_type]; $output = wpcf_pr_admin_post_meta_box_has_row($post, $post_type, $data, $parent_post_type, false); } echo json_encode(array('output' => $output)); break; case 'pr_save_child_post': ob_start(); // Try to catch any errors require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = array(); if (isset($_GET['post_id']) && isset($_GET['post_type_child'])) { $post = get_post($_GET['post_id']); $post_type = $_GET['post_type_child']; $output = wpcf_pr_admin_save_post_hook($_GET['post_id']); } $errors = ob_get_clean(); echo json_encode(array('output' => $output, 'errors' => $errors)); break; case 'pr_delete_child_post': require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = 'Passed wrong parameters'; if (isset($_GET['post_id'])) { $output = wpcf_pr_admin_delete_child_item($_GET['post_id']); } echo json_encode(array('output' => $output)); break; case 'pr-update-belongs': require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = 'Passed wrong parameters'; if (isset($_POST['post_id']) && isset($_POST['wpcf_pr_belongs'])) { $output = wpcf_pr_admin_update_belongs($_POST['post_id'], $_POST['wpcf_pr_belongs']); } echo json_encode(array('output' => $output)); break; case 'pr_pagination': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = 'Passed wrong parameters'; if (isset($_GET['post_id']) && isset($_GET['post_type'])) { $post = get_post($_GET['post_id']); $post_type = $_GET['post_type']; $has = wpcf_pr_admin_get_has($post->post_type); $output = wpcf_pr_admin_post_meta_box_has_form($post, $post_type, $has[$post_type], $post->post_type); } echo json_encode(array('output' => $output)); break; case 'pr_sort': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = 'Passed wrong parameters'; if (isset($_GET['field']) && isset($_GET['sort']) && isset($_GET['post_id']) && isset($_GET['post_type'])) { $post = get_post($_GET['post_id']); $post_type = $_GET['post_type']; $has = wpcf_pr_admin_get_has($post->post_type); $output = wpcf_pr_admin_post_meta_box_has_form($post, $post_type, $has[$post_type], $post->post_type); } echo json_encode(array('output' => $output)); break; case 'pr_sort_parent': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = 'Passed wrong parameters'; if (isset($_GET['field']) && isset($_GET['sort']) && isset($_GET['post_id']) && isset($_GET['post_type'])) { $post = get_post($_GET['post_id']); $post_type = $_GET['post_type']; $has = wpcf_pr_admin_get_has($post->post_type); $output = wpcf_pr_admin_post_meta_box_has_form($post, $post_type, $has[$post_type], $post->post_type); } echo json_encode(array('output' => $output)); break; case 'pr_save_all': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = array(); if (isset($_POST['post_id']) && isset($_POST['wpcf_post_relationship'])) { $output = wpcf_pr_admin_save_post_hook($_POST['post_id']); } echo json_encode(array('output' => $output)); break; case 'repetitive_add': if (isset($_GET['field_id'])) { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; $field = wpcf_admin_fields_get_field($_GET['field_id']); // Pass as normal unset($field['data']['repetitive']); $fields = array($_GET['field_id'] => $field); $element = wpcf_admin_post_process_fields(false, $fields, false, false, 'repetitive'); if ($field['type'] == 'skype') { $key = key($element); unset($element[$key]['#title']); echo json_encode(array('output' => wpcf_form_simple($element) . wpcf_form_render_js_validation('#post', false))); } else { $element = array_shift($element); if (!in_array($field['type'], array('checkbox'))) { unset($element['#title']); } echo json_encode(array('output' => wpcf_form_simple(array('repetitive' => $element)) . wpcf_form_render_js_validation('#post', false))); } } else { echo json_encode(array('output' => 'params missing')); } break; case 'repetitive_delete': if (isset($_POST['post_id']) && isset($_POST['field_id']) && isset($_POST['old_value'])) { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; $field = wpcf_admin_fields_get_field($_POST['field_id']); if (!empty($field)) { if ($field['type'] == 'date') { delete_post_meta($_POST['post_id'], wpcf_types_get_meta_prefix($field) . $field['id'], strtotime(base64_decode($_POST['old_value']))); } else { if ($field['type'] == 'skype') { delete_post_meta($_POST['post_id'], wpcf_types_get_meta_prefix($field) . $field['id'], unserialize(base64_decode($_POST['old_value']))); } else { delete_post_meta($_POST['post_id'], wpcf_types_get_meta_prefix($field) . $field['id'], base64_decode($_POST['old_value'])); } } echo json_encode(array('output' => 'deleted')); } else { echo json_encode(array('output' => 'field not found')); } } else { echo json_encode(array('output' => 'params missing')); } break; case 'cd_verify': if (!is_array($_POST['wpcf'])) { die; } require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/conditional-display.php'; $passed_fields = array(); $failed_fields = array(); $post = false; if (isset($_SERVER['HTTP_REFERER'])) { $split = explode('?', $_SERVER['HTTP_REFERER']); if (isset($split[1])) { parse_str($split[1], $vars); if (isset($vars['post'])) { $_POST['post_ID'] = $vars['post']; $post = get_post($vars['post']); } } } // Dummy post if (!$post) { $post = new stdClass(); $post->ID = 1; } // Filter meta values (switch them with $_POST values) add_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4); foreach ($_POST['wpcf'] as $field_id => $field_value) { $element = array(); $field = wpcf_admin_fields_get_field($field_id); if (!empty($field['data']['conditional_display']['conditions'])) { $element = wpcf_cd_post_edit_field_filter($element, $field, $post, 'group'); if (isset($element['__wpcf_cd_status']) && $element['__wpcf_cd_status'] == 'passed') { $passed_fields[] = 'wpcf[' . $field['id'] . ']'; } else { $failed_fields[] = 'wpcf[' . $field['id'] . ']'; } } } // Remove filter meta values (switch them with $_POST values) remove_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4); if (!empty($passed_fields) || !empty($failed_fields)) { $execute = ''; foreach ($passed_fields as $field_name) { $execute .= 'jQuery(\'[name^="' . $field_name . '"]\').parents(\'.wpcf-cd\').show().removeClass(\'wpcf-cd-failed\').addClass(\'wpcf-cd-passed\');' . " "; } foreach ($failed_fields as $field_name) { $execute .= 'jQuery(\'[name^="' . $field_name . '"]\').parents(\'.wpcf-cd\').hide().addClass(\'wpcf-cd-failed\').removeClass(\'wpcf-cd-passed\');' . " "; } echo json_encode(array('output' => '', 'execute' => $execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } die; break; case 'cd_group_verify': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/conditional-display.php'; $group = wpcf_admin_fields_get_group($_POST['group_id']); if (empty($group)) { echo json_encode(array('output' => '')); die; } $execute = ''; $group['conditional_display'] = get_post_meta($group['id'], '_wpcf_conditional_display', true); // Filter meta values (switch them with $_POST values) add_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4); $post = false; if (isset($_SERVER['HTTP_REFERER'])) { $split = explode('?', $_SERVER['HTTP_REFERER']); if (isset($split[1])) { parse_str($split[1], $vars); if (isset($vars['post'])) { $_POST['post_ID'] = $vars['post']; $post = get_post($vars['post']); } } } // Dummy post if (!$post) { $post = new stdClass(); $post->ID = 1; } if (!empty($group['conditional_display']['conditions'])) { $result = wpcf_cd_post_groups_filter(array(0 => $group), $post, 'group'); if (!empty($result)) { $result = array_shift($result); $passed = $result['_conditional_display'] == 'passed' ? true : false; } else { $passed = false; } if (!$passed) { $execute = 'jQuery("#' . $group['slug'] . '").slideUp().find(".wpcf-cd-group").addClass(\'wpcf-cd-group-failed\').removeClass(\'wpcf-cd-group-passed\').hide();'; } else { $execute = 'jQuery("#' . $group['slug'] . '").show().find(".wpcf-cd-group").addClass(\'wpcf-cd-group-passed\').removeClass(\'wpcf-cd-group-failed\').slideDown();'; } } // Remove filter meta values (switch them with $_POST values) remove_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4); echo json_encode(array('output' => '', 'execute' => $execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'pr_verify': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/conditional-display.php'; $passed_fields = array(); $failed_fields = array(); $post = false; if (isset($_SERVER['HTTP_REFERER'])) { $split = explode('?', $_SERVER['HTTP_REFERER']); if (isset($split[1])) { parse_str($split[1], $vars); if (isset($vars['post'])) { $_POST['post_ID'] = $vars['post']; $post = get_post($vars['post']); } } } // Dummy post if (!$post) { $post = new stdClass(); $post->ID = 1; } // Filter meta values (switch them with $_POST values) add_filter('get_post_metadata', 'wpcf_cd_pr_meta_ajax_validation_filter', 10, 4); if (isset($_POST['wpcf_post_relationship'])) { $child_post_id = key($_POST['wpcf_post_relationship']); $data = $_POST['wpcf_post_relationship'] = array_shift($_POST['wpcf_post_relationship']); foreach ($data as $field_id => $field_value) { $element = array(); $field = wpcf_admin_fields_get_field(str_replace(WPCF_META_PREFIX, '', $field_id)); if (!empty($field['data']['conditional_display']['conditions'])) { $element = wpcf_cd_post_edit_field_filter($element, $field, $post, 'group'); if (isset($element['__wpcf_cd_status']) && $element['__wpcf_cd_status'] == 'passed') { $passed_fields[] = 'wpcf_post_relationship_' . $child_post_id . '_' . $field['id']; } else { $failed_fields[] = 'wpcf_post_relationship_' . $child_post_id . '_' . $field['id']; } } } } // Remove filter meta values (switch them with $_POST values) remove_filter('get_post_metadata', 'wpcf_cd_pr_meta_ajax_validation_filter', 10, 4); if (!empty($passed_fields) || !empty($failed_fields)) { $execute = ''; foreach ($passed_fields as $field_name) { $execute .= 'jQuery(\'#' . $field_name . '\').parents(\'.wpcf-cd\').show().removeClass(\'wpcf-cd-failed\').addClass(\'wpcf-cd-passed\');' . " "; } foreach ($failed_fields as $field_name) { $execute .= 'jQuery(\'#' . $field_name . '\').parents(\'.wpcf-cd\').hide().addClass(\'wpcf-cd-failed\').removeClass(\'wpcf-cd-passed\');' . " "; } echo json_encode(array('output' => '', 'execute' => $execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } die; break; default: break; } if (function_exists('wpcf_ajax')) { wpcf_ajax(); } die; }
function short_code_menu_callback($index, $cf_key, $function_name, $menu, $shortcode) { global $link_layout_number, $wpdb; static $fields_started = false; static $templates_started = false; if ($menu == __('View template', 'wpv-views') && !$templates_started) { $templates_started = true; echo '</tr><td> </td><tr>'; echo '</tr><tr><td><strong>' . __('View templates', 'wpv-views') . '</strong></td>'; echo '</tr><tr>'; $link_layout_number = 0; } if ($menu != '' && $menu != __('View template', 'wpv-views') && !$fields_started) { $fields_started = true; echo '</tr><td> </td><tr>'; echo '</tr><tr><td><strong>' . __('Fields', 'wpv-views') . '</strong></td>'; echo '</tr><tr>'; $link_layout_number = 0; } if (!($link_layout_number % 2)) { if ($link_layout_number != 0) { echo '</tr><tr>'; } } if ($menu == __('View template', 'wpv-views')) { // get the View template title. $field_name = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM {$wpdb->posts} WHERE post_type='view-template' AND post_name=%s", $cf_key)); if (!$field_name) { $field_name = $cf_key; } } else { $field_name = $cf_key; if (function_exists('wpcf_types_get_meta_prefix')) { // we have types. // Get the field name for display $types_prefix = wpcf_types_get_meta_prefix(); if (strpos($cf_key, $types_prefix) === 0) { $field_info = wpcf_fields_get_field_by_slug(substr($cf_key, strlen($types_prefix))); if (isset($field_info['name'])) { $field_name = $field_info['name']; } } } } echo '<td><a style="cursor: pointer" onclick="on_add_field_wpv(\'' . $menu . '\', \'' . esc_js($cf_key) . '\', \'' . base64_encode($field_name) . '\')">'; echo $field_name; echo '</a></td>'; $link_layout_number++; }
/** * Exports data to XML. */ function wpcf_admin_export_data() { require_once WPCF_EMBEDDED_ABSPATH . '/common/array2xml.php'; $xml = new ICL_Array2XML(); $data = array(); // Get groups $groups = get_posts('post_type=wp-types-group&post_status=null&numberposts=-1'); if (!empty($groups)) { $data['groups'] = array('__key' => 'group'); foreach ($groups as $key => $post) { $post = (array) $post; $post_data = array(); $copy_data = array('ID', 'post_content', 'post_title', 'post_excerpt', 'post_type', 'post_status'); foreach ($copy_data as $copy) { if (isset($post[$copy])) { $post_data[$copy] = $post[$copy]; } } $data['groups']['group-' . $post['ID']] = $post_data; $meta = get_post_custom($post['ID']); if (!empty($meta)) { $data['groups']['group-' . $post['ID']]['meta'] = array(); foreach ($meta as $meta_key => $meta_value) { if (in_array($meta_key, array('_wp_types_group_terms', '_wp_types_group_post_types', '_wp_types_group_fields'))) { $data['groups']['group-' . $post['ID']]['meta'][$meta_key] = $meta_value[0]; } } if (empty($data['groups']['group-' . $post['ID']]['meta'])) { unset($data['groups']['group-' . $post['ID']]['meta']); } } } } // Get fields $fields = wpcf_admin_fields_get_fields(); if (!empty($fields)) { // WPML global $iclTranslationManagement; if (!empty($iclTranslationManagement)) { foreach ($fields as $field_id => $field) { // @todo Fix for added fields if (isset($iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id])) { $fields[$field_id]['wpml_action'] = $iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id]; } } } $data['fields'] = $fields; $data['fields']['__key'] = 'field'; } // Get custom types $custom_types = get_option('wpcf-custom-types', array()); if (!empty($custom_types)) { foreach ($custom_types as $key => $type) { $custom_types[$key]['id'] = $key; } $data['types'] = $custom_types; $data['types']['__key'] = 'type'; } // Get custom tax $custom_taxonomies = get_option('wpcf-custom-taxonomies', array()); if (!empty($custom_taxonomies)) { foreach ($custom_taxonomies as $key => $tax) { $custom_taxonomies[$key]['id'] = $key; } $data['taxonomies'] = $custom_taxonomies; $data['taxonomies']['__key'] = 'taxonomy'; } // Offer for download $data = $xml->array2xml($data, 'types'); $sitename = sanitize_title(get_bloginfo('name')); if (!empty($sitename)) { $sitename .= '.'; } $filename = $sitename . 'types.' . date('Y-m-d') . '.xml'; $code = "<?php\r\n"; $code .= '$timestamp = ' . time() . ';' . "\r\n"; $code .= '$auto_import = '; $code .= isset($_POST['embedded-settings']) && $_POST['embedded-settings'] == 'ask' ? 0 : 1; $code .= ';' . "\r\n"; $code .= "\r\n?>"; if (class_exists('ZipArchive')) { $zipname = $sitename . 'types.' . date('Y-m-d') . '.zip'; $file = tempnam("tmp", "zip"); $zip = new ZipArchive(); $zip->open($file, ZipArchive::OVERWRITE); $zip->addFromString('settings.xml', $data); $zip->addFromString('settings.php', $code); $zip->close(); $data = file_get_contents($file); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=" . $zipname); header("Content-Type: application/zip"); header("Content-length: " . strlen($data) . "\n\n"); header("Content-Transfer-Encoding: binary"); echo $data; unlink($file); die; } else { // download the xml. header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=" . $filename); header("Content-Type: application/xml"); header("Content-length: " . strlen($data) . "\n\n"); echo $data; die; } }
/** * * @global object $wpdb * */ function prepare_items() { global $wpdb; $wpcf_per_page = 15; // Get ours and enabled $cf_types = wpcf_admin_fields_get_fields(true, true); $__groups = wpcf_admin_fields_get_groups(); foreach ($__groups as $__group_id => $__group) { $__groups[$__group_id]['fields'] = wpcf_admin_fields_get_fields_by_group($__group['id'], 'slug', false, true, false); } foreach ($cf_types as $cf_id => $cf) { foreach ($__groups as $__group) { if (isset($__group['fields'][$cf_id])) { $cf_types[$cf_id]['groups'][$__group['id']] = $__group['name']; } } $cf_types[$cf_id]['groups_txt'] = empty($cf_types[$cf_id]['groups']) ? __('None', 'wpcf') : implode(', ', $cf_types[$cf_id]['groups']); } // Get others (cache this result?) $cf_other = $wpdb->get_results("\r\n\t\tSELECT meta_id, meta_key\r\n\t\tFROM {$wpdb->postmeta}\r\n\t\tGROUP BY meta_key\r\n\t\tHAVING meta_key NOT LIKE '\\_%'\r\n\t\tORDER BY meta_key"); // Clean from ours foreach ($cf_other as $type_id => $type_data) { if (strpos($type_data->meta_key, WPCF_META_PREFIX) !== false) { $field_temp = wpcf_admin_fields_get_field(str_replace(WPCF_META_PREFIX, '', $type_data->meta_key)); if (!empty($field_temp)) { if (!empty($field_temp['data']['disabled'])) { $cf_types[$field_temp['id']] = array('id' => $field_temp['id'], 'slug' => $type_data->meta_key, 'name' => $type_data->meta_key, 'type' => 0, 'groups_txt' => __('None', 'wpcf')); } else { unset($cf_other[$type_id]); } } else { if (wpcf_types_cf_under_control('check_exists', $type_data->meta_key)) { unset($cf_other[$type_id]); } else { $cf_types[$type_data->meta_key] = array('id' => $type_data->meta_key, 'slug' => $type_data->meta_key, 'name' => $type_data->meta_key, 'type' => 0, 'groups_txt' => __('None', 'wpcf')); } } } else { if (wpcf_types_cf_under_control('check_exists', $type_data->meta_key)) { unset($cf_other[$type_id]); } else { $cf_types[$type_data->meta_key] = array('id' => $type_data->meta_key, 'slug' => $type_data->meta_key, 'name' => $type_data->meta_key, 'type' => 0, 'groups_txt' => __('None', 'wpcf')); } } } // Set some values foreach ($cf_types as $cf_id_temp => $cf_temp) { if (empty($cf_temp['type']) || !empty($cf_temp['data']['controlled'])) { $cf_types[$cf_id_temp]['slug'] = $cf_temp['name']; } else { $cf_types[$cf_id_temp]['slug'] = wpcf_types_get_meta_prefix($cf_temp) . $cf_temp['slug']; } } // Order if (!empty($_REQUEST['orderby'])) { $sort_matches = array('c' => 'name', 'g' => 'groups_txt', 't' => 'slug', 'f' => 'type'); $sorted_keys = array(); $new_array = array(); foreach ($cf_types as $cf_id_temp => $cf_temp) { if (isset($sort_matches[$_REQUEST['orderby']])) { $sorted_keys[$cf_temp['id']] = strtolower($cf_temp[$sort_matches[$_REQUEST['orderby']]]); } else { $sorted_keys[$cf_temp['id']] = strtolower($cf_temp[$sort_matches['c']]); } } asort($sorted_keys, SORT_STRING); if ('desc' == $_REQUEST['order']) { $sorted_keys = array_reverse($sorted_keys, true); } foreach ($sorted_keys as $cf_id_temp => $groups_txt) { $new_array[$cf_id_temp] = $cf_types[$cf_id_temp]; } $cf_types = $new_array; } // Search if (!empty($_REQUEST['s'])) { $search_results = array(); foreach ($cf_types as $search_id => $search_field) { if (strpos(strval($search_field['name']), strval(trim(stripslashes($_REQUEST['s'])))) !== false) { $search_results[$search_id] = $cf_types[$search_id]; } } $cf_types = $search_results; } if (empty($_GET['display_all'])) { $total_items = count($cf_types); if ($total_items < $wpcf_per_page) { $wpcf_per_page = $total_items; } if ($this->get_pagenum() == 1) { $offset = 0; } else { $offset = ($this->get_pagenum() - 1) * $wpcf_per_page; } // Display required number of entries on page $this->items = array_slice($cf_types, $offset, $wpcf_per_page); $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $wpcf_per_page)); } else { $this->items = $cf_types; } $this->_column_headers = array($this->get_columns(), array(), $this->get_sortable_columns()); }
/** * Fix due to a bug saving date as array. * * BUGS * 'timestamp' is saved without Hour and Minute appended. * * @param type $value * @param type $field */ function __wpcf_fields_date_check_leftover($value, $field, $use_cache = true) { if (empty($value)) { return $value; } if (!is_object($field)) { $post_id = wpcf_get_post_id(); $field_id = isset($field['id']) ? $field['id'] : false; $meta_id = isset($field['__meta_id']) ? $field['__meta_id'] : false; } else { $post_id = isset($field->meta_object->post_id) ? $field->meta_object->post_id : false; $field_id = isset($field->cf['id']) ? $field->cf['id'] : false; $meta_id = isset($field->meta_object->meta_id) ? $field->meta_object->meta_id : false; } if (empty($post_id) || empty($meta_id) || empty($field_id)) { return $value; } $field_slug = wpcf_types_get_meta_prefix() . $field_id; // Check if cached static $cache = array(); $cache_key = $meta_id; if (isset($cache[$cache_key]) && $use_cache) { return $cache[$cache_key]; } $_meta = wpcf_get_post_meta($post_id, '_wpcf_' . $field_id . '_hour_and_minute', true); /* * If meta exists - it's outdated value * and Hour and Minute should be appended and removed. */ if (!empty($_meta) && is_array($_meta) && isset($_meta[$meta_id])) { $meta = $_meta[$meta_id]; // Return empty date if can not be calculated if (!empty($meta['timestamp']) || !empty($meta['datepicker'])) { $meta['timestamp'] = wpcf_fields_date_value_get_filter($meta, $field, 'timestamp', 'check_leftover'); // Check if calculation needed if (isset($meta['hour']) && $meta['hour'] != adodb_date('H', $meta['timestamp']) || isset($meta['minute']) && $meta['minute'] != adodb_date('i', $meta['timestamp'])) { $value = wpcf_fields_date_calculate_time($meta); } } } // Cache it if ($use_cache) { $cache[$cache_key] = $value; } return $value; }