Some Useful Functions

I have written some functions that you may find useful. These functions are now part of my plugin. The functions have been put in a namespace mf2tk so they need to be prefixed with \mf2tk\ e.g. \mf2tk\get_field_names().

function get_field_id( $field_name, $post_type = NULL )

get_field_id() returns the internal numerical id of the given magic field in the given post type.

function get_field_id( $field_name, $post_type = NULL ) {
    global $post, $wpdb; 
    static $field_id_cache = [];

    if ( !$post_type ) { $post_type = get_post_type( $post->ID ); }

    if ( !array_key_exists( $post_type, $field_id_cache ) ) {
        $field_id_cache[$post_type] = [];

        $sql = sprintf(
            "SELECT name, id FROM %s WHERE post_type = '%s'",
            MF_TABLE_CUSTOM_FIELDS, $post_type );

        $results = $wpdb->get_results( $sql, ARRAY_A );
        foreach ( $results as $result ) {
            $field_id_cache[$post_type][$result['name']] = $result['id'];
        }
    }

    return $field_id_cache[$post_type][$field_name];
}

function get_group_id( $group_name, $post_type = NULL )

get_group_id() returns the internal numerical id of the given group in the given post type.

function get_group_id( $group_name, $post_type = NULL ) {
    global $post, $wpdb; 
    static $group_id_cache = [];

    if ( !$post_type ) { $post_type = get_post_type( $post->ID ); }

    if ( !array_key_exists( $post_type, $group_id_cache ) ) {
        $group_id_cache[$post_type] = [];

        $sql = sprintf(
            "SELECT name, id FROM %s WHERE post_type = '%s'",
            MF_TABLE_CUSTOM_GROUPS, $post_type );

        $results = $wpdb->get_results( $sql, ARRAY_A );
        foreach ( $results as $result ) {
            $group_id_cache[$post_type][$result['name']] = $result['id'];
        }
    }

    return $group_id_cache[$post_type][$group_name];
}

function get_field_names( $post_id = NULL )

get_field_names() returns a numerically indexed array of all magic field names in the given post type including fields in groups.

function get_field_names( $post_type = NULL ) {
    global $post, $wpdb; 
    
    if ( !$post_type ) { $post_type = get_post_type( $post->ID ); }
    
    $sql = sprintf( "SELECT name FROM %s WHERE post_type = '%s' " .
        "ORDER BY custom_group_id, display_order",
        MF_TABLE_CUSTOM_FIELDS, $post_type );

    $results = $wpdb->get_results( $sql, ARRAY_A );

    return array_map( '_get_name_mc', $results );
}

function _get_name_mc( $row ) {
    return $row[ 'name' ];
}

function get_group_names( $post_type = NULL )

get_group_names() returns a numerically indexed array of all group names in the given post type.

function get_group_names( $post_type = NULL ) {
    global $post, $wpdb; 
    
    if ( !$post_type ) { $post_type = get_post_type( $post->ID ); }
  
    $sql = sprintf(
        "SELECT name FROM %s WHERE post_type = '%s'",
        MF_TABLE_CUSTOM_GROUPS, $post_type );

    $results = $wpdb->get_results( $sql, ARRAY_A );

    return array_map( '_get_name_mc', $results );
}

function _get_name_mc( $row ) {
    return $row[ 'name' ];
}

function get_field_names_in_group( $group_name, $post_type = NULL )

get_field_names_in_group() returns a numerically indexed array of all magic field names in the given group in the given post type.

function get_field_names_in_group( $group_name, $post_type = NULL ) {
    global $post, $wpdb; 

    if ( !$post_type ) { get_post_type( $post->ID ); }
    $group_id = get_group_id( $group_name, $post_type );

    $sql = sprintf( "SELECT name FROM %s WHERE post_type = '%s' " .
        "AND custom_group_id = %d ORDER BY display_order",
        MF_TABLE_CUSTOM_FIELDS, $post_type, $group_id );

    $results = $wpdb->get_results( $sql, ARRAY_A );

    return array_map( '_get_name_mc', $results );
}

function get_field_type( $field, $post_type = NULL )

get_field_type() returns the type of the field $field in a post of post type $post_type. The type is a string – ‘textbox’, ‘checkbox_list’, ‘radiobutton_list’, ‘related_type’, …

function get_field_type( $field, $post_type = NULL ) {
    global $wpdb, $post;
    static $field_type_cache = [];

    if ( !$post_type ) { $post_type = get_post_type( $post->ID ); }

    if ( !array_key_exists( $post_type, $field_type_cache ) ) {
        $field_type_cache[$post_type] = [];

        $sql = sprintf(
            "SELECT name, type FROM %s WHERE post_type = '%s'",
            MF_TABLE_CUSTOM_FIELDS, $post_type );

        $results = $wpdb->get_results( $sql, ARRAY_A );
        foreach ( $results as $result ) {
            $field_type_cache[$post_type][$result['name']] = $result['type'];
        }
    }

    return $field_type_cache[$post_type][$field];
}

function get_field_options( $field, $post_type = NULL )

get_field_options() returns the options of the field $field in a post of post type $post_type. The options are returned as an array. The keys of array are option names and the values are option values.

function get_field_options( $field, $post_type = NULL ) {
    global $wpdb, $post;
    static $field_options_cache = [];

    if ( !$post_type ) { $post_type = get_post_type( $post->ID ); }

    if ( !array_key_exists( $post_type, $field_options_cache ) ) {
        $field_options_cache[$post_type] = [];

        $sql = sprintf(
            "SELECT name, options FROM %s WHERE post_type = '%s'",
            MF_TABLE_CUSTOM_FIELDS, $post_type );

        $results = $wpdb->get_results( $sql, ARRAY_A );
        foreach ( $results as $result ) {
            $field_options_cache[$post_type][$result['name']] = unserialize( $result['options'] );
        }
    }

    return $field_options_cache[$post_type][$field];
}

function do_magic_fields( $callback = NULL, $post_id = NULL )

do_magic_fields() iterates over all the magic fields of the given post calling the function $callback with arguments field name, group index, field index, field value and field type. If no $callback is specified the default function _dump_magic_field_mc() is called. This gives a dump of all magic fields.

function do_magic_fields( $callback = NULL, $post_id = NULL ) {
    global $post;

    if ( !$callback ) { $callback = '_dump_magic_field_mc'; }
    if ( !$post_id ) { $post_id = $post->ID; }
    $post_type = get_post_type( $post_id );

    $fields = get_field_names( $post_type );
    foreach ( $fields as $field ) {

        $group_indices = get_order_group( $field, $post_id );
        foreach ( $group_indices as $group_index ) {

            $field_indices = get_order_field( $field, $group_index, $post_id );
            foreach ( $field_indices as $field_index ) {

                $value = get( $field, $group_index, $field_index, $post_id );
                $type = get_field_type( $field, $post_type );
                call_user_func( $callback, $field, $group_index, $field_index,
                    $value, $type );

            }
        }
    }
}

function _dump_magic_field_mc( $field, $group_index, $field_index, $value, $type ) {
    echo $type . ': ' . $field . '[' . $group_index . '][' . $field_index . '] = '
        . print_r( $value, true ) . '<br />';
}

function do_magic_fields_by_group( $callback = NULL, $post_id = NULL )

do_magic_fields() iterates over all the magic fields of the given post in order y group calling the function $callback with arguments field name, group index, field index, field value and field type. If no $callback is specified the default function _dump_magic_field_mc() is called. This gives a dump of all magic fields.

function do_magic_fields_by_group( $callback = NULL, $post_id = NULL ) {
    global $post, $wpdb; 
    
    if ( !$callback ) { $callback = '_dump_magic_field_mc'; }
    if ( !$post_id ) { $post_id = $post->ID; }
    $post_type = get_post_type( $post_id );
    foreach ( get_group_names( $post_type ) as $group_name ) {
        $field_names = get_field_names_in_group( $group_name, $post_type );
        $group_indices = get_order_group( $field_names[0], $post_id );
        foreach ( $group_indices as $group_index ) {
            foreach ( $field_names as $field_name ) {
                $field_indices = get_order_field( $field_name, $group_index,
                    $post_id );
                foreach ( $field_indices as $field_index ) {
                    $value = get( $field_name, $group_index, $field_index,
                        $post_id );
                    $type = get_field_type( $field_name, $post_type );
                    call_user_func( $callback, $field_name, $group_index,
                        $field_index, $value, $type );
                }
            }
        }
    }
}

function _dump_magic_field_mc( $field, $group_index, $field_index, $value, $type ) {
    echo $type . ': ' . $field . '[' . $group_index . '][' . $field_index . '] = '
        . print_r( $value, true ) . '<br />';
}

How to report problems, ask questions, …

Please post your problem reports, questions, requests and comments to the Magic Fields 2 Toolkit support forum.

Advertisements