예제 #1
0
/**
 * @todo: documentation
 * Register a post connector
 * @param string $unique_name The slug for this connector
 * @param array $new_post_connector Args for this connector
 */
function simple_fields_register_post_connector($unique_name = "", $new_post_connector = array())
{
    #sf_d($new_post_connector);
    global $sf;
    $post_connectors = $sf->get_post_connectors();
    // Id of found or new connector
    $connector_id = NULL;
    // Id of highest connector, if no connector found for slug
    $highest_connector_id = 0;
    $is_new_connector = FALSE;
    // Check if connector already exist
    // or if it does not then get a new id for it
    foreach ($post_connectors as $oneConnector) {
        if ($oneConnector["slug"] == $unique_name && !empty($unique_name)) {
            // Connector already exists
            $connector_id = $oneConnector["id"];
            // No need to loop further once we found the connector
            break;
        } else {
            if (!isset($connector_id) && $oneConnector["id"] > $highest_connector_id) {
                // Connector not found so far and id of this connector is the highest so far
                $highest_connector_id = $oneConnector["id"];
            }
        }
    }
    // If no connector_id was found then this is a new connector
    // Set connector_id to the highest connector_id + 1
    if (!isset($connector_id) || !is_numeric($connector_id)) {
        $is_new_connector = TRUE;
        if (!empty($post_connectors[$highest_connector_id]) || $highest_connector_id > 0) {
            $highest_connector_id++;
        }
        $connector_id = $highest_connector_id;
        // If $connector_id is 0 here then it's the first ever created
        // But 0 is the id to tell SF to create new (in admin), so we must up it to 1
        if ($connector_id === 0) {
            $connector_id = 1;
        }
    }
    // Make sure connector has a slug
    if (empty($unique_name)) {
        $unique_name = "post_connector_" . $connector_id;
    }
    $unique_name = sanitize_key($unique_name);
    // Make sure name is not empty
    if (!isset($new_post_connector["name"]) || empty($new_post_connector["name"])) {
        $new_post_connector["name"] = $unique_name;
    }
    // Make sure post_types is an array (this allows for post_types to be a string)
    if (isset($new_post_connector["post_types"]) && !is_array($new_post_connector["post_types"])) {
        $new_post_connector["post_types"] = (array) $new_post_connector["post_types"];
    }
    // Setup defaults to merge to
    if ($is_new_connector) {
        // New connector, setup defaults
        $post_connector_defaults = array("id" => $connector_id, "key" => $unique_name, "slug" => $unique_name, "name" => $unique_name . "_" . $connector_id, "field_groups" => array(), "post_types" => array(), "deleted" => false, "hide_editor" => false, "added_with_code" => true);
    } else {
        // Existing connector, get old values
        $post_connector_defaults = $post_connectors[$connector_id];
        // make sure all values are set
        // added_with_code since 1.2.4
        if (!isset($post_connector_defaults["added_with_code"])) {
            $post_connector_defaults["added_with_code"] = true;
        }
    }
    // Create or update this connector_id id the array of existing connectors
    $post_connectors[$connector_id] = simple_fields_merge_arrays($post_connector_defaults, $new_post_connector);
    $post_connectors[$connector_id]['post_types'] = array_unique($post_connectors[$connector_id]['post_types']);
    // If field group passed as args is a non-empty array
    // This is where field groups get attached to this connector
    if (isset($new_post_connector["field_groups"]) && is_array($new_post_connector["field_groups"]) && !empty($new_post_connector["field_groups"])) {
        // Array with all field groups that this connector has
        $field_group_connectors = array();
        // Default values
        $field_group_connector_defaults = array("id" => "", "key" => "", "deleted" => 0, "context" => "normal", "priority" => "low");
        // For each field group that we want to connect to this connector
        foreach ($new_post_connector["field_groups"] as $field_group_options) {
            // Key is deprecated, use slug
            if (isset($field_group_options["key"]) && !empty($field_group_options["key"])) {
                $field_group_options["slug"] = $field_group_options["key"];
            } else {
                if (isset($field_group_options["slug"]) && !empty($field_group_options["slug"])) {
                    $field_group_options["key"] = $field_group_options["slug"];
                }
            }
            // Check if the field group we want to connect actually exists
            // First check by id, then if not found by slug - slug is prefered and the only options kinda supported...by me at least :)
            $found_field_group = NULL;
            $found_field_group = isset($field_group_options["id"]) ? $sf->get_field_group_by_slug($field_group_options["id"]) : NULL;
            $found_field_group = !isset($found_field_group) && isset($field_group_options["slug"]) ? $sf->get_field_group_by_slug($field_group_options["slug"]) : NULL;
            // Field group was found, so now add it to the connector
            if (isset($found_field_group)) {
                $field_group_id = $found_field_group["id"];
                $field_group_slug = $found_field_group["slug"];
                $field_group_name = $found_field_group["name"];
                // If field group is deleted or not
                // default_field_group_connector = a copy of new field group defaults or a copy of existing field group values
                $default_field_group_connector = NULL;
                if (isset($field_group_connectors[$field_group_id]) && !$field_group_connectors[$field_group_id]["deleted"]) {
                    // A little unsure on how when we get here
                    $default_field_group_connector = $field_group_connectors[$field_group_id];
                } else {
                    $default_field_group_connector = $field_group_connector_defaults;
                }
                // Add id from found field group
                // And slug + also key for backwards compatibility
                // And name
                $field_group_connectors[$field_group_id]["id"] = $field_group_id;
                $field_group_connectors[$field_group_id]["slug"] = $field_group_slug;
                $field_group_connectors[$field_group_id]["key"] = $field_group_slug;
                $field_group_connectors[$field_group_id]["name"] = $field_group_name;
                // Go through all default values and make sure field array has each of them set
                foreach ($default_field_group_connector as $oneGroupConnectorDefaultKey => $oneGroupConnectorDefaultValue) {
                    // Skip some keys, that are added always above
                    if (in_array($oneGroupConnectorDefaultKey, array("id", "slug", "name"))) {
                        continue;
                    }
                    // Ok, what happens here?
                    // field_group_options is one of the field groups what we want to connect to post connector, as send here by parameter (not as saved in db or such)
                    // field_group_connectors is array with all field groups that this connector will have
                    if (isset($field_group_options[$oneGroupConnectorDefaultKey])) {
                        // If the key from the defaults array did exist in the field group sent in to this function
                        // So use the value that we got as an parameter, overwriting the default value
                        $field_group_connectors[$field_group_id][$oneGroupConnectorDefaultKey] = $field_group_options[$oneGroupConnectorDefaultKey];
                    } else {
                        // Key from defaults array was not found in the field group sent to this function
                        // So use default value
                        $field_group_connectors[$field_group_id][$oneGroupConnectorDefaultKey] = $oneGroupConnectorDefaultValue;
                    }
                }
                // make sure all keys form default values exists
            }
            // if field group found
        }
        // foreach field group
        // Done adding field group
        $post_connectors[$connector_id]["field_groups"] = $field_group_connectors;
    }
    // if field groups was sent along as arg
    // Clear cache, store values, and return
    $sf->clear_caches();
    update_option("simple_fields_post_connectors", $post_connectors);
    return $sf->get_connector_by_id($connector_id);
}
/**
 * @todo: documentation
 */
function simple_fields_register_post_connector($unique_name = "", $new_post_connector = array())
{
    global $sf;
    $post_connectors = $sf->get_post_connectors();
    $highest_connector_id = 0;
    foreach ($post_connectors as $oneConnector) {
        if ($oneConnector["key"] == $unique_name && !empty($unique_name)) {
            // Connector already exists
            $connector_id = $oneConnector["id"];
        } else {
            if (!isset($connector_id) && $oneConnector["id"] > $highest_connector_id) {
                $highest_connector_id = $oneConnector["id"];
            }
        }
    }
    if (!isset($connector_id) || !is_numeric($connector_id)) {
        if (!empty($post_connectors[$highest_connector_id]) || $highest_connector_id > 0) {
            $highest_connector_id++;
        }
        $connector_id = $highest_connector_id;
    }
    // If $connector_id is 0 here then it's the first ever created
    // But 0 is the id to tell SF to create new, so we must up it to 1
    if ($connector_id === 0) {
        $connector_id = 1;
    }
    if (empty($unique_name)) {
        $unique_name = "post_connector_" . $connector_id;
    } else {
        if (!isset($new_post_connector["name"]) || empty($new_post_connector["name"])) {
            $new_post_connector["name"] = $unique_name;
        }
    }
    $unique_name = sanitize_key($unique_name);
    $post_connector_defaults = array("id" => $connector_id, "key" => $unique_name, "slug" => $unique_name, "name" => $unique_name . "_" . $connector_id, "field_groups" => array(), "post_types" => array(), "deleted" => false, "hide_editor" => false);
    if (isset($post_connectors[$connector_id])) {
        $post_connector_defaults = $post_connectors[$connector_id];
    }
    $post_connectors[$connector_id] = simple_fields_merge_arrays($post_connector_defaults, $new_post_connector);
    $post_connectors[$connector_id]['post_types'] = array_unique($post_connectors[$connector_id]['post_types']);
    if (isset($new_post_connector["field_groups"]) && is_array($new_post_connector["field_groups"]) && !empty($new_post_connector["field_groups"])) {
        $field_group_connectors = array();
        $field_groups = $sf->get_field_groups();
        $field_group_connector_defaults = array("id" => "", "key" => "", "slug" => "", "name" => "", "deleted" => 0, "context" => "normal", "priority" => "low");
        foreach ($new_post_connector["field_groups"] as $field_group_options) {
            $field_group_found = false;
            foreach ($field_groups as $oneGroup) {
                if (isset($oneGroup["id"]) && isset($field_group_options["id"]) && $oneGroup["id"] == $field_group_options["id"] || $oneGroup["key"] == $field_group_options["key"]) {
                    // Field group found
                    $field_group_found = true;
                    $field_group_id = $oneGroup["id"];
                    $field_group_key = $oneGroup["key"];
                }
            }
            if ($field_group_found !== false) {
                if (isset($field_group_connectors[$field_group_id]) && !$field_group_connectors[$field_group_id]["deleted"]) {
                    $default_field_group_connector = $field_group_connectors[$field_group_id];
                } else {
                    $default_field_group_connector = $field_group_connector_defaults;
                }
                foreach ($default_field_group_connector as $oneGroupConnectorDefaultKey => $oneGroupConnectorDefaultValue) {
                    if ($oneGroupConnectorDefaultKey == "id") {
                        $field_group_connectors[$field_group_id]["id"] = $field_group_id;
                    } else {
                        if ($oneGroupConnectorDefaultKey == "key") {
                            $field_group_connectors[$field_group_id]["key"] = $field_group_key;
                        } else {
                            if (isset($field_group_options[$oneGroupConnectorDefaultKey])) {
                                $field_group_connectors[$field_group_id][$oneGroupConnectorDefaultKey] = $field_group_options[$oneGroupConnectorDefaultKey];
                            } else {
                                $field_group_connectors[$field_group_id][$oneGroupConnectorDefaultKey] = $oneGroupConnectorDefaultValue;
                            }
                        }
                    }
                }
            }
        }
        $post_connectors[$connector_id]["field_groups"] = $field_group_connectors;
    }
    $sf->clear_caches();
    update_option("simple_fields_post_connectors", $post_connectors);
    return $post_connectors[$connector_id];
}