private function hack_around_legacy_import_routine($import_data, $import_args = null) { add_filter('wpcf_admin_message_store', '__return_false'); $_POST['overwrite-settings'] = isset($import_args['overwrite-settings']) ? (bool) $import_args['overwrite-settings'] : false; $_POST['overwrite-groups'] = isset($import_args['overwrite-groups']) && 1 == $import_args['overwrite-groups'] ? 1 : 0; $_POST['overwrite-fields'] = isset($import_args['overwrite-fields']) && 1 == $import_args['overwrite-fields'] ? 1 : 0; $_POST['overwrite-types'] = isset($import_args['overwrite-types']) && 1 == $import_args['overwrite-types'] ? 1 : 0; $_POST['overwrite-tax'] = isset($import_args['overwrite-tax']) && 1 == $import_args['overwrite-tax'] ? 1 : 0; $_POST['post_relationship'] = isset($import_args['post_relationship']) ? (bool) $import_args['post_relationship'] : false; $_POST['delete-groups'] = isset($import_args['delete-groups']) ? (bool) $import_args['delete-groups'] : false; $_POST['delete-fields'] = isset($import_args['delete-fields']) ? (bool) $import_args['delete-fields'] : false; $_POST['delete-types'] = isset($import_args['delete-types']) ? (bool) $import_args['delete-types'] : false; $_POST['delete-tax'] = isset($import_args['delete-tax']) ? (bool) $import_args['delete-tax'] : false; /** * This can be emtpy string '' or 'wpvdemo', but this second option has a serious bug with xml parsing/looping */ $context = isset($import_args['context']) ? $import_args['context'] : ''; // Not sure if this is needed require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/import-export.php'; // Prepare legacy arguments for Types_Data_Installer $legacy_args = array(); if (isset($import_args['overwrite'])) { $legacy_args['force_import_post_name'] = wpcf_getarr($import_args, 'overwrite', array()); } if (isset($import_args['skip'])) { $legacy_args['force_skip_post_name'] = wpcf_getarr($import_args, 'skip', array()); } if (isset($import_args['duplicate'])) { $legacy_args['force_duplicate_post_name'] = wpcf_getarr($import_args, 'duplicate', array()); } $result = wpcf_admin_import_data($import_data, false, $context, $legacy_args); return true; }
/** * @param mixed $value Single field value in the intermediate format (see data mappers for details) * * @return string Rendered HTML */ protected function render_single($value) { // Simply get the Skype name. $skype_name = wpcf_getarr($value, 'skypename'); $skype_name = is_string($skype_name) ? $skype_name : ''; return sanitize_text_field($skype_name); }
/** * @param Types_Field_Type_Definition $type * @return Types_Field_Type_Definition[] */ public function get_possible_conversions($type) { if (!$type instanceof Types_Field_Type_Definition) { throw new InvalidArgumentException('Not a field type definition'); } $matrix = $this->get_conversion_matrix(); $allowed_slugs = wpcf_ensarr(wpcf_getarr($matrix, $type->get_slug())); $allowed_types = Types_Field_Type_Definition_Factory::get_instance()->load_multiple_definitions($allowed_slugs); return $allowed_types; }
/** * @param string[] $url_components Result of parse_url(). * @return string Label of the resulting link. * @since 1.9.1 */ protected function get_link_label($url_components) { // Build link label only from host, path and query. $url_query = wpcf_getarr($url_components, 'query'); $url_query = empty($url_query) ? '' : '?' . $url_query; $url_path = wpcf_getarr($url_components, 'path'); if (empty($url_query)) { // Omit last slash when it would be the last label character $url_path = substr($url_path, 0, strlen($url_path) - 1); } $label = sprintf('%s%s%s', wpcf_getarr($url_components, 'host'), $url_path, $url_query); return $label; }
/** * WPCF_Field_Type_Definition constructor. * * @param string $field_type_slug Field type slug. * @param array $args Additional array of arguments which should contain at least 'display_name' (or 'title') * and 'description' elements, but omitting them is not critical. */ public function __construct($field_type_slug, $args) { if (sanitize_title($field_type_slug) != $field_type_slug) { throw new InvalidArgumentException('Invalid field type slug.'); } if (!is_array($args)) { throw new InvalidArgumentException('Wrong arguments provided.'); } $this->field_type_slug = $field_type_slug; // Try to fall back to legacy "title", and if even that fails, use id instead. $this->display_name = sanitize_text_field(wpcf_getarr($args, 'display_name', wpcf_getarr($args, 'title', $field_type_slug))); $this->description = wpcf_getarr($args, 'description', ''); $this->args = $args; }
/** * Determine value to be displayed for this option. * * @param bool $is_checked For which value should the output be rendered. * @return string Display value depending on option definition and field display mode * @since 1.9.1 */ public function get_display_value($is_checked = true) { $field_definition_array = $this->field_definition->get_definition_array(); $display_mode = wpcf_getnest($field_definition_array, array('data', 'display'), 'db'); $display_mode = 'value' == $display_mode ? 'value' : 'db'; if ('db' == $display_mode) { return $is_checked ? $this->get_value_to_store() : ''; } else { if ($is_checked) { return wpcf_getarr($this->config, 'display_value'); } else { return ''; } } }
/** * @inheritdoc * * @param array $definition_array * @return array * @since 2.1 */ protected function sanitize_field_definition_array_type_specific($definition_array) { $definition_array['type'] = Types_Field_Type_Definition_Factory::SELECT; $options = wpcf_ensarr(wpcf_getnest($definition_array, array('data', 'options'))); foreach ($options as $key => $option) { if ('default' == $key) { continue; } $options[$key] = $this->sanitize_single_option($option); } $default_option = wpcf_getarr($options, 'default'); if (!is_string($default_option) || !array_key_exists($default_option, $options)) { $default_option = 'no-default'; } $options['default'] = $default_option; $definition_array['data']['options'] = $options; return $definition_array; }
/** * Read the field values from $_POST. * * @return array Values in the "intermediate" format (see WPCF_Field_DataMapper_Abstract). For non-repetitive values, * it will be an array with a single item. */ private function read_field_values() { if (null == $this->field_values) { $definition = $this->field->get_definition(); $form_data = wpcf_ensarr(wpcf_getpost('wpcf')); $values = wpcf_getarr($form_data, $definition->get_slug()); // Handle single fields. if (!$definition->get_is_repetitive()) { $values = array($values); } // Map POST values to intermediate format. $this->field_values = array(); $data_mapper = $definition->get_data_mapper(); foreach ($values as $value) { $this->field_values[] = $data_mapper->post_to_intermediate($value, $form_data); } } return wpcf_ensarr($this->field_values); }
/** * Load a field type definition. * * @param string $field_type_slug Slug of the field type. If the function fails to find the field type and the slug * starts with a "wpcf-" prefix, it attempts to remove it and search again. This way, passing a field type ID, * which usually has this form, is also supported. * @return null|WPCF_Field_Type_Definition Field type definition or null if it can't be loaded. */ public function load_field_type_definition($field_type_slug) { if (!is_string($field_type_slug)) { return null; } // Check if we can use cached version. if (!in_array($field_type_slug, $this->field_type_definitions)) { // now it gets hacky $field_types = $this->get_legacy_field_types(); if (!in_array($field_type_slug, array_keys($field_types))) { // Field slug not recognized. Maybe we got a field identifier instead. Check if we can remove // the wpcf- prefix and try again. $prefix = 'wpcf-'; if (substr($field_type_slug, 0, strlen($prefix)) == $prefix) { $field_type_slug = substr($field_type_slug, strlen($prefix)); if (!in_array($field_type_slug, $field_types)) { // Removing prefix didn't help return null; } } else { // There was no prefix to remove. return null; } } // Not using getFieldTypeData() directly to avoid unnecessary getFieldsTypes() and filter applying. $field_type_configuration_path = $field_types[$field_type_slug]; $field_type_configuration = WPCF_Fields::getFieldTypeConfig($field_type_configuration_path); $field_type_id = wpcf_getarr($field_type_configuration, 'id', null); if (null == $field_type_id) { return null; } try { $field_type_definition = new WPCF_Field_Type_Definition($field_type_slug, $field_type_configuration); } catch (Exception $e) { return null; } // Save new instance to cache. $this->field_type_definitions[$field_type_slug] = $field_type_definition; } // Use cache. return $this->field_type_definitions[$field_type_slug]; }
/** * @param array $arguments Original action/filter arguments. * * @return mixed */ function process_call($arguments) { $query = wpcf_getarr($arguments, 1, array()); $domain = wpcf_getarr($query, 'domain', 'all'); unset($query['domain']); // Sanitize input if (!is_string($domain) || !is_array($query)) { return null; } if ('all' == $domain) { // Separate query for each available domain. $groups_by_domain = array(); $domains = Types_Field_Utils::get_domains(); foreach ($domains as $field_domain) { $groups_by_domain[$field_domain] = $this->query_specific_domain($field_domain, $query); } return $groups_by_domain; } else { return $this->query_specific_domain($domain, $query); } }
/** * Get field groups based on query arguments. * * @param array $query_args Optional. Arguments for the WP_Query that will be applied on the underlying posts. * Post type query is added automatically. * Additional arguments are allowed: * - 'types_search': String for extended search. See WPCF_Field_Group::is_match() for details. * - 'is_active' bool: If defined, only active/inactive field groups will be returned. * * @return Types_Field_Group[] * @since 1.9 */ public function query_groups($query_args = array()) { // Read specific arguments $search_string = wpcf_getarr($query_args, 'types_search'); $is_active = wpcf_getarr($query_args, 'is_active', null); // Query posts $query_args = array_merge($query_args, array('post_type' => $this->get_post_type(), 'posts_per_page' => -1)); // Group's "activeness" is defined by the post status. if (null !== $is_active) { unset($query_args['is_active']); $query_args['post_status'] = $is_active ? 'publish' : 'draft'; } $query = new WP_Query($query_args); $posts = $query->get_posts(); // Transform posts into Types_Field_Group $all_groups = array(); foreach ($posts as $post) { $field_group = $this->load_field_group($post); if (null != $field_group) { $all_groups[] = $field_group; } } // Filter groups by the search string. $selected_groups = array(); if (empty($search_string)) { $selected_groups = $all_groups; } else { /** @var Types_Field_Group $group */ foreach ($all_groups as $group) { if ($group->is_match($search_string)) { $selected_groups[] = $group; } } } return $selected_groups; }
/** * Get a value from nested associative array. * * This function will try to traverse a nested associative array by the set of keys provided. * * E.g. if you have $source = array( 'a' => array( 'b' => array( 'c' => 'my_value' ) ) ) and want to reach 'my_value', * you need to write: $my_value = wpcf_getnest( $source, array( 'a', 'b', 'c' ) ); * * @param mixed|array $source The source array. * @param string[] $keys Keys which will be used to access the final value. * @param null|mixed $default Default value to return when the keys cannot be followed. * * @return mixed|null Value in the nested structure defined by keys or default value. */ function wpcf_getnest(&$source, $keys = array(), $default = null) { $current_value = $source; while (!empty($keys)) { $current_key = array_shift($keys); $is_last_key = empty($keys); $current_value = wpcf_getarr($current_value, $current_key, null); if ($is_last_key) { return $current_value; } elseif (!is_array($current_value)) { return $default; } } return $default; }
/** * @return string Ellipsis to be added when some field values are omitted. * @since 1.9.1 */ protected function get_ellipsis() { return wpcf_getarr($this->args, 'ellipsis', $this->get_value_separator() . '...'); }
/** * Get array of groups that are associated with given taxonomy. * * @param string $taxonomy_slug Slug of the taxonomy * @return WPCF_Field_Group_Term[] Associated term field groups. */ public function get_groups_by_taxonomy($taxonomy_slug) { $groups_by_taxonomies = $this->get_groups_by_taxonomies(); return wpcf_ensarr(wpcf_getarr($groups_by_taxonomies, $taxonomy_slug)); }
/** * Change cardinality of given field, if it is permitted by its type. * * @param string $field_slug Field definition slug. * @param string $domain Field domain. * @param string[] $arguments Needs to contain the 'target_cardinality' key with 'single'|'repetitive' value. * @return bool|WP_Error|WPCF_Field_Definition The updated definition on succes, error/false otherwise. * @since 2.0 */ public static function change_field_cardinality($field_slug, $domain, $arguments) { $factory = Types_Field_Utils::get_definition_factory_by_domain($domain); $definition = $factory->load_field_definition($field_slug); if (null == $definition) { return new WP_Error(42, sprintf(__('Field definition for field "%s" not found in options.', 'wpcf'), sanitize_text_field($field_slug))); } else { if (!$definition->is_managed_by_types()) { return new WP_Error(42, sprintf(__('Field "%s" will not be converted because it is not managed by Types.', 'wpcf'), sanitize_text_field($field_slug))); } } $target_cardinality = wpcf_getarr($arguments, 'target_cardinality', null, array('single', 'repetitive')); if (null == $target_cardinality) { return false; } $set_as_repetitive = 'repetitive' == $target_cardinality; $result = $definition->set_is_repetitive($set_as_repetitive); if ($result) { return $definition; } else { return false; } }
/** * Render help tab content from its configuration. * * @param string $page_name Name of current page. * @return array|null Null when no help tab should be displayed, or an array with keys 'title' and 'content'. * @since 2.0 */ private function get_help_content($page_name) { $config = $this->get_help_config($page_name); if (null == $config) { return null; } $twig = $this->get_twig(); return array('title' => wpcf_getarr($config, 'title'), 'content' => $twig->render(wpcf_getarr($config, 'template'), wpcf_getarr($config, 'context'))); }
protected function get_maximum_image_size() { return wpcf_getarr($this->args, 'maximum_image_size', self::DEFAULT_MAX_IMAGE_SIZE); }
/** * Uses the Toolset_Tokenizer to break down the expression, replace problematic operators by their text-only * equivalents and glue the expression back together. * * Side-effects: Loses custom whitespace characters. All operators (except parentheses) will be surrounded by spaces * while everywhere else the whitespace characters will be trimmed. * * Note: If an invalid expression is provided, it doesn't do anything with it. * * @param string $expression Condition expression. * @return string Equivalent expression but without <, <=, etc. * @since 2.0 */ protected function transform_operators_to_text_equivalents($expression) { try { // The expression may come directly from parse_str() which may add backslashes to quotes. The tokenizer // wouldn't survive that. $expression = stripslashes($expression); $toolset_bootstrap = Toolset_Common_Bootstrap::getInstance(); $toolset_bootstrap->register_parser(); $tokenizer = new Toolset_Tokenizer(); $tokens = $tokenizer->Tokanize($expression); $token_value_replacements = array('<' => 'lt', '>' => 'gt', '<=' => 'lte', '>=' => 'gte', '<>' => 'ne', '=' => 'eq'); $result = ''; foreach ($tokens as $token) { if ($token->isCompOp) { $token->val = wpcf_getarr($token_value_replacements, $token->val, $token->val); } if ($token->isCompOp || $token->isArithmeticOp || $token->isLogicOp) { $result .= ' ' . $token->val . ' '; } else { if ($token->isStringLiteral) { $result .= '\'' . $token->val . '\''; } else { $result .= $token->val; } } } return $result; } catch (Exception $e) { // Most probably we were unable to tokenize the expression. We give up. return $expression; } }
/** * @return string Meta_key use to store this field's values. Defaults to wpcf-$slug. */ public function get_meta_key() { if (null == $this->meta_key) { $this->meta_key = sanitize_title(wpcf_getarr($this->definition_array, 'meta_key', self::FIELD_META_KEY_PREFIX . $this->get_slug())); } return $this->meta_key; }
/** * Query field definitions. * * @param array $args Following arguments are recognized: * * - filter: What field definitions should be retrieved: 'types'|'generic'|'all' * - orderby: 'name'|'slug'|'is_under_types_control'|'field_type' * - order: 'asc'|'desc' * - search: String for fulltext search. * - field_type: string|array Field type slug(s). Allowed only for Types fields. * - group_id: int Field group ID where this field belongs to. Allowed only for Types fields. * - group_slug: string Slug of an existing firld group where this field belongs to. If defined, overrides * the group_id argument. Allowed only for Types fields. * * @return WPCF_Field_Definition_Abstract[] Field definitions that match query arguments. * * @since 1.9 */ public function query_definitions($args) { $args = wp_parse_args($args, array('filter' => 'all')); // Get only certain type of field definitions (generic, Types or both) $filter = wpcf_getarr($args, 'filter'); if ('types' == $filter) { $results = $this->load_types_field_definitions(); } else { if ('generic' == $filter) { $results = $this->load_generic_field_definitions(); } else { if ('all' == $filter) { $results = $this->load_all_definitions(); } else { $results = array(); } } } // Save us some work if there will be no results at all if (empty($results)) { return array(); } // Perform fulltext search if needed $search_string = wpcf_getarr($args, 'search', ''); if (!empty($search_string)) { $matches = array(); foreach ($results as $definition) { if ($definition->is_match($search_string)) { $matches[] = $definition; } } $results = $matches; } // Select only fields of desired type $field_type = wpcf_getarr($args, 'field_type', array()); $field_type = empty($field_type) ? array() : $field_type; $field_type = is_array($field_type) ? $field_type : array($field_type); if (!empty($field_type)) { $type_matches = array(); foreach ($results as $definition) { if ($definition instanceof WPCF_Field_Definition) { $type = $definition->get_type(); if (in_array($type->get_slug(), $field_type)) { $type_matches[] = $definition; } } } $results = $type_matches; } // Select fields by field group. $group_source = wpcf_getarr($args, 'group_slug', (int) wpcf_getarr($args, 'group_id')); if (!empty($group_source)) { $group_factory = $this->get_group_factory(); $group = $group_factory->load_field_group($group_source); $group_matches = array(); if (null != $group) { foreach ($results as $field_definition) { if ($field_definition instanceof WPCF_Field_Definition && $field_definition->belongs_to_group($group)) { $group_matches[] = $field_definition; } } } $results = $group_matches; } // Sort results $orderby = wpcf_getarr($args, 'orderby', 'name'); $order = wpcf_getarr($args, 'order', 'asc', array('asc', 'desc')); $results = $this->order_definitions($results, $orderby, $order); return $results; }
/** * Check if field is repetitive. * * @deprecated Use types_is_repetitive instead. * @param array $field Field definition array. * @return bool */ function wpcf_admin_is_repetitive($field) { $field_type = wpcf_getarr($field, 'type', ''); $is_repetitive = (int) wpcf_getnest($field, array('data', 'repetitive'), 0); return $is_repetitive && !empty($field_type) && wpcf_admin_can_be_repetitive($field_type); }
/** * Convenience method for setting a 'data' attribute to the definition array safely. * * @param string $key * @param mixed $value * @since 2.0 */ private function set_data_key_safely($key, $value) { if (!is_array(wpcf_getarr($this->definition_array, 'data'))) { $this->definition_array['data'] = array(); } $this->definition_array['data'][$key] = $value; }
/** * Filter the data to be exported for custom taxonomies. * * Ensure the settings of post types associated with the taxonomy is exported correctly, even with support of legacy * settings. * * @param array $taxonomy_data * @return array Modified taxonomy data. * @since unknown */ function wpcf_fix_exported_taxonomy_assignment_to_cpt($taxonomy_data = array()) { $setting_name_prefix = '__types_cpt_supports_'; $post_type_support_settings = array(); // Associated CPTs slugs are stored as XML keys, so they can not start with a number. // We force a prefix on all of them on export, and restore them on import. $supported_post_types = wpcf_ensarr(wpcf_getarr($taxonomy_data, 'supports')); foreach ($supported_post_types as $post_type_slug => $is_supported) { $setting_name = $setting_name_prefix . $post_type_slug; $post_type_support_settings[$setting_name] = $is_supported ? 1 : 0; } // Here, we will also process the legacy "object_type" setting, containing supported post type slugs as array items, // in the samve way. $legacy_supported_post_type_array = wpcf_ensarr(wpcf_getarr($taxonomy_data, 'object_type')); foreach ($legacy_supported_post_type_array as $post_type_slug) { $setting_name = $setting_name_prefix . $post_type_slug; $post_type_support_settings[$setting_name] = 1; } // Now we need to remove this legacy setting to prevent producing invalid XML. unset($taxonomy_data['object_type']); $taxonomy_data['supports'] = $post_type_support_settings; return $taxonomy_data; }
/** * @return string Value that should be stored to database when this option is selected. * @since 1.9.1 */ public function get_value_to_store() { $value = wpcf_getarr($this->config, 'set_value'); if (!is_string($value)) { return ''; } return $value; }
/** * Check a slug for conflict with slugs used for post type permalink rewriting. * * @param string $value Value to check. * @param string $exclude_id Post type slug to exclude from checking. * * @return array|bool Conflict information (an associative array with conflicting_id, message) or false when * there's no conflict. * @since 2.1 */ private function check_slug_conflicts_in_post_type_rewrite_rules($value, $exclude_id) { // Merge currently registered post types (which might include some from other plugins) and // Types settings (which might include deactivated post types). $post_type_settings = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); if (!is_array($post_type_settings)) { return false; } $post_type_settings = array_merge($post_type_settings, get_post_types(array(), 'objects')); foreach ($post_type_settings as $post_type) { // Read information from the post type object or Types settings if (is_object($post_type)) { $slug = $post_type->name; $is_permalink_rewriting_enabled = (bool) wpcf_getarr($post_type->rewrite, 'enabled'); $rewrite_slug = wpcf_getarr($post_type->rewrite, 'slug'); $is_custom_slug_used = !empty($rewrite_slug); } else { $slug = wpcf_getarr($post_type, 'slug'); $is_permalink_rewriting_enabled = (bool) wpcf_getnest($post_type, array('rewrite', 'enabled')); $is_custom_slug_used = wpcf_getnest($post_type, array('rewrite', 'custom')) == 'custom'; $rewrite_slug = wpcf_getnest($post_type, array('rewrite', 'slug')); } if ($slug == $exclude_id) { continue; } if ($is_permalink_rewriting_enabled) { $conflict_candidate = $is_custom_slug_used ? $rewrite_slug : $slug; if ($conflict_candidate == $value) { $conflict = array('conflicting_id' => $slug, 'message' => sprintf(__('The same value is already used in permalink rewrite rules for the custom post type "%s". Using it again can cause issues with permalinks.', 'wpcf'), esc_html($slug))); return $conflict; } } } // No conflicts found. return false; }
/** * @param string $action_name One of the allowed action names: 'manage_with_types' * @param array $field Field definition model passed from JS. * @param string $domain Field domain name. * @param mixed $action_specific_data * @return bool|mixed|null|WP_Error|WP_Error[]|WPCF_Field_Definition An error, array of errors, boolean indicating * success or a result value to be passed back to JS. * @since 2.0 */ private function single_field_control_action($action_name, $field, $domain, $action_specific_data) { $field_slug = wpcf_getarr($field, 'slug'); switch ($action_name) { case 'manage_with_types': return Types_Field_Utils::start_managing_field(wpcf_getarr($field, 'metaKey'), $domain); case 'stop_managing_with_types': return Types_Field_Utils::stop_managing_field($field_slug, $domain); case 'change_group_assignment': return Types_Field_Utils::change_assignment_to_groups($field_slug, $domain, $action_specific_data); case 'delete_field': return Types_Field_Utils::delete_field($field_slug, $domain); case 'change_field_type': return Types_Field_Utils::change_field_type($field_slug, $domain, $action_specific_data); case 'change_field_cardinality': return Types_Field_Utils::change_field_cardinality($field_slug, $domain, $action_specific_data); default: return new WP_Error(42, __('Invalid action name.', 'wpcf')); } }
/** * Perform the upgrade by calling the appropriate upgrade routines and updating the version number in the database. * * @since 2.1 */ private function do_upgrade() { $from_version = $this->get_database_version(); $upgrade_routines = $this->get_upgrade_routines(); $target_version = $this->get_plugin_version(); // Sort upgrade routines by their version. $routines_by_version = array(); foreach ($upgrade_routines as $key => $row) { $routines_by_version[$key] = $row['version']; } array_multisort($routines_by_version, SORT_DESC, $upgrade_routines); // Run all the routines necessary foreach ($upgrade_routines as $routine) { $upgrade_version = (int) wpcf_getarr($routine, 'version'); if ($from_version < $upgrade_version && $upgrade_version <= $target_version) { $callback = wpcf_getarr($routine, 'callback'); if (is_callable($callback)) { call_user_func($callback); } $this->update_database_version($upgrade_version); } } // Finally, update to current plugin version even if there are no other routines to run, so that // this method is not called every time by check_upgrade(). $this->update_database_version($target_version); }
/** * Insert elements into source array at a specified position. * * @param array $source Source array. * @param array $to_insert Array of elements to insert. * @param int|array $position When integer is provided, zero or positive value means index of the first element that * will not be included before $to_insert. Negative value defines the position from the end of the source array * (-1 will insert at the very end, -2 before last element, etc.). When an array is provided, it is expected to * have form: * - 'key': Key to select an element in the source array * - 'where': Insert 'before'|'after' the selected element * * @return array * @since 1.9.1 */ public static function insert_at_position($source, $to_insert, $position) { if (is_array($position)) { $pivot_key = wpcf_getarr($position, 'key', null); $direction = wpcf_getarr($position, 'where', 'after', array('after', 'before')); if (array_key_exists($pivot_key, $source)) { $pivot_index = array_search($pivot_key, array_keys($source)); $position = 'before' == $direction ? $pivot_index : $pivot_index + 1; } else { $position = 'before' == $direction ? 0 : -1; } } // $position should be index of the first element that will NOT be included before $to_insert. $position = (int) $position; if (0 > $position) { // E.g.: When $position == -1, the inserted elements should be placed after the last element of $source. // $position will point after the last element of $source, new elements will be inserted after it. $position = count($source) + 1 + $position; //echo "pos=$position\n"; // Handle too low $position value - insert elements before whole $source. if (0 > $position) { $position = 0; } } $first_source_part = array_slice($source, 0, $position); $second_source_part = array_slice($source, $position); $result = array_merge($first_source_part, $to_insert, $second_source_part); return $result; }
/** * @return string Ellipsis to be added when some field values are omitted. * @since 1.9.1 */ protected function get_ellipsis() { return wpcf_getarr($this->args, 'ellipsis', '...'); }
/** * Query field definitions. * * @param array $args Following arguments are recognized: * - filter: What field definitions should be retrieved: 'types'|'generic'|'all' * - orderby: 'name'|'slug'|'is_under_types_control'|'field_type' * - order: 'asc'|'desc' * - search: String for fulltext search. * * @return WPCF_Field_Definition_Abstract[] Field definitions that match query arguments. */ public function query_definitions($args) { $args = wp_parse_args($args, array('filter' => 'all')); // Get only certain type of field definitions (generic, Types or both) switch ($args['filter']) { case 'types': $results = $this->load_types_field_definitions(); break; case 'generic': $results = $this->load_generic_field_definitions(); break; case 'all': $results = $this->load_all_definitions(); break; default: $results = array(); break; } // Perform fulltext search if needed $search_string = wpcf_getarr($args, 'search', ''); if (!empty($search_string)) { $matches = array(); foreach ($results as $definition) { if ($definition->is_match($search_string)) { $matches[] = $definition; } } $results = $matches; } // Sort results $orderby = wpcf_getarr($args, 'orderby', 'name'); $order = wpcf_getarr($args, 'order', 'asc', array('asc', 'desc')); $results = $this->order_definitions($results, $orderby, $order); return $results; }