Exemple #1
0
/**
 * Register a connection between two post types.
 *
 * This creates the appropriate meta box in the admin edit screen.
 *
 * Takes the following parameters, as an associative array:
 *
 * - 'name' - string A unique identifier for this connection type.
 *
 * - 'from' - string|array The first end of the connection.
 *
 * - 'from_query_vars' - array Additional query vars to pass to WP_Query. Default: none.
 *
 * - 'to' - string|array The second end of the connection.
 *
 * - 'to_query_vars' - array Additional query vars to pass to WP_Query. Default: none.
 *
 * - 'fields' - array( key => Title ) Metadata fields editable by the user. Default: none.
 *
 * - 'cardinality' - string How many connection can each post have: 'one-to-many', 'many-to-one' or 'many-to-many'. Default: 'many-to-many'
 *
 * - 'duplicate_connections' - bool Whether to allow more than one connection between the same two posts. Default: false.
 *
 * - 'self_connections' - bool Whether to allow a post to connect to itself. Default: false.
 *
 * - 'sortable' - bool|string Whether to allow connections to be ordered via drag-and-drop. Can be 'from', 'to', 'any' or false. Default: false.
 *
 * - 'title' - string|array The box's title. Default: 'Connected {$post_type}s'
 *
 * - 'from_labels' - array Additional labels for the admin box (optional)
 *
 * - 'to_labels' - array Additional labels for the admin box (optional)
 *
 * - 'reciprocal' - bool For indeterminate connections: True means all connections are displayed in a single box. False means 'from' connections are shown in one box and 'to' connections are shown in another box. Default: false.
 *
 * - 'admin_box' - bool|string|array Whether and where to show the admin connections box.
 *
 * - 'can_create_post' - bool Whether to allow post creation via the connection box. Default: true.
 *
 * @param array $args
 *
 * @return bool|object False on failure, P2P_Connection_Type instance on success.
 */
function p2p_register_connection_type($args)
{
    if (!did_action('init')) {
        trigger_error("Connection types should not be registered before the 'init' hook.");
    }
    $argv = func_get_args();
    if (count($argv) > 1) {
        $args = array();
        foreach (array('from', 'to', 'reciprocal') as $i => $key) {
            if (isset($argv[$i])) {
                $args[$key] = $argv[$i];
            }
        }
    } else {
        $args = $argv[0];
    }
    if (isset($args['id'])) {
        $args['name'] = _p2p_pluck($args, 'id');
    }
    if (isset($args['prevent_duplicates'])) {
        $args['duplicate_connections'] = !$args['prevent_duplicates'];
    }
    if (isset($args['show_ui'])) {
        $args['admin_box'] = array('show' => _p2p_pluck($args, 'show_ui'));
        if (isset($args['context'])) {
            $args['admin_box']['context'] = _p2p_pluck($args, 'context');
        }
    }
    $ctype = P2P_Connection_Type_Factory::register($args);
    do_action('p2p_registered_connection_type', $ctype, $args);
    return $ctype;
}
/**
 * Register a connection between two post types.
 *
 * This creates the appropriate meta box in the admin edit screen.
 *
 * Takes the following parameters, as an associative array:
 *
 * - 'name' - string A unique identifier for this connection type.
 *
 * - 'from' - string|array The first end of the connection.
 *
 * - 'from_query_vars' - array Additional query vars to pass to WP_Query. Default: none.
 *
 * - 'to' - string|array The second end of the connection.
 *
 * - 'to_query_vars' - array Additional query vars to pass to WP_Query. Default: none.
 *
 * - 'fields' - array( key => Title ) Metadata fields editable by the user. Default: none.
 *
 * - 'cardinality' - string How many connection can each post have: 'one-to-many', 'many-to-one' or 'many-to-many'. Default: 'many-to-many'
 *
 * - 'prevent_duplicates' - bool Whether to disallow duplicate connections between the same two posts. Default: true.
 *
 * - 'sortable' - bool|string Whether to allow connections to be ordered via drag-and-drop. Can be 'from', 'to', 'any' or false. Default: false.
 *
 * - 'title' - string|array The box's title. Default: 'Connected {$post_type}s'
 *
 * - 'from_labels' - array Additional labels for the admin box (optional)
 *
 * - 'to_labels' - array Additional labels for the admin box (optional)
 *
 * - 'reciprocal' - bool For indeterminate connections: True means all connections are displayed in a single box. False means 'from' connections are shown in one box and 'to' connections are shown in another box. Default: false.
 *
 * - 'admin_box' - bool|string|array Whether and where to show the admin connections box.
 *
 * - 'can_create_post' - bool Whether to allow post creation via the connection box. Default: true.
 *
 * @param array $args
 *
 * @return bool|object False on failure, P2P_Connection_Type instance on success.
 */
function p2p_register_connection_type($args)
{
    if (!did_action('init')) {
        trigger_error("Connection types should not be registered before the 'init' hook.");
    }
    $argv = func_get_args();
    $args = _p2p_back_compat_args($argv);
    if (isset($args['name'])) {
        if (strlen($args['name']) > 32) {
            trigger_error(sprintf("Connection name '%s' is longer than 32 characters.", $args['name']), E_USER_WARNING);
            return false;
        }
    } else {
        trigger_error("Connection types without a 'name' parameter are deprecated.", E_USER_WARNING);
    }
    // Box args
    if (isset($args['admin_box'])) {
        $metabox_args = _p2p_pluck($args, 'admin_box');
        if (!is_array($metabox_args)) {
            $metabox_args = array('show' => $metabox_args);
        }
    } else {
        $metabox_args = array();
    }
    foreach (array('fields', 'can_create_post') as $key) {
        if (isset($args[$key])) {
            $metabox_args[$key] = _p2p_pluck($args, $key);
        }
    }
    // Column args
    if (isset($args['admin_column'])) {
        $column_args = _p2p_pluck($args, 'admin_column');
    } else {
        $column_args = false;
    }
    $ctype = P2P_Connection_Type_Factory::register($args);
    if (is_admin()) {
        P2P_Box_Factory::register($ctype->name, $metabox_args);
        P2P_Column_Factory::register($ctype->name, $column_args);
    }
    return $ctype;
}