Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified Variables #1020

Closed
edirpedro opened this issue Jun 7, 2016 · 4 comments
Closed

Simplified Variables #1020

edirpedro opened this issue Jun 7, 2016 · 4 comments

Comments

@edirpedro
Copy link

edirpedro commented Jun 7, 2016

The variables attribute could have a simplified version for those outputs using a single variable and returning non formatted values, instead of writing that long array format. What do you think?

Kirki::add_field( 'my_config', array(
    'settings'  => 'my_setting',
    'label'     => __( 'Font Size', 'translation_domain' ),
    'section'   => 'my_section',
    'type'      => 'number',
    'priority'  => 10,
    'default'   => '16',
    'variables' => 'mainmenu-font-size'
) );

The get_variables() function:

/**
 * Build the variables.
 *
 * @return array    ('variable-name' => value)
 */
public function get_variables() {

    $variables = array();

    // Loop through all fields.
    foreach ( Kirki::$fields as $field ) {

        // Check if we have variables for this field.
        if ( isset( $field['variables'] ) && $field['variables'] && ! empty( $field['variables'] ) ) {

            // Single variable.
            if ( is_string( $field['variables'] ) ) {

                // Sanitize the variable name.
                $variable_name = esc_attr( $field['variables'] );

                // Get the value.
                $variables[ $variable_name ] = Kirki::get_option( $field['settings'] );

            } else {

                // Loop through the array of variables.
                foreach ( $field['variables'] as $field_variable ) {

                    // Is the variable ['name'] defined? If yes, then we can proceed.
                    if ( isset( $field_variable['name'] ) ) {

                        // Sanitize the variable name.
                        $variable_name = esc_attr( $field_variable['name'] );

                        // Do we have a callback function defined? If not then set $variable_callback to false.
                        $variable_callback = ( isset( $field_variable['callback'] ) && is_callable( $field_variable['callback'] ) ) ? $field_variable['callback'] : false;

                        // If we have a variable_callback defined then get the value of the option
                        // and run it through the callback function.
                        // If no callback is defined (false) then just get the value.
                        if ( $variable_callback ) {
                            $variables[ $variable_name ] = call_user_func( $field_variable['callback'], Kirki::get_option( $field['settings'] ) );
                        } else {
                            $variables[ $variable_name ] = Kirki::get_option( $field['settings'] );
                        }
                    }
                }

            }
        }
    }

    // Pass the variables through a filter ('kirki/variable') and return the array of variables.
    return apply_filters( 'kirki/variable', $variables );

}
@aristath
Copy link
Contributor

aristath commented Jun 8, 2016

Hey there!
I just added a couple of lines to allow using a string.
Can you please pull the latest develop branch and test if that works for you?
Thanks!

@edirpedro
Copy link
Author

Hi,
It's not working, this function is receiving the default empty array. I didn't tested my proposal, sorry about that.

aristath added a commit that referenced this issue Jun 9, 2016
This reverts commit 49dde59.
aristath added a commit that referenced this issue Jun 9, 2016
@aristath
Copy link
Contributor

aristath commented Jun 9, 2016

Can you please pull again and test it? I just pushed another commit. :)

@edirpedro
Copy link
Author

It's working now! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants