MickeyKay / better-font-awesome-library

[WordPress] The easiest way to integrate Font Awesome into your WordPress theme or plugin.
34 stars 13 forks source link

Iconpicker issue when BFA Library is included in a parent theme and a child theme is used #5

Open BraadMartin opened 8 years ago

BraadMartin commented 8 years ago

Currently inside the Better_Font_Awesome_Library class there is a function setup_root_url() that determines the URL to use for enqueueing the admin JS and CSS files. This function calls the WordPress functions get_stylesheet_directory() and get_stylesheet_directory_uri(), which return different values when used in a parent theme vs. a child theme.

This issue is tricky because technically a theme only becomes a parent theme when it is child themed, and until it is child themed the get_stylesheet_* functions work great, but when a child theme is used the get_stylesheet_* functions refer to the child theme while the get_template_* functions refer to the parent theme.

I've updated the logic for the setup_root_url() function in a recent project to:

/**
 * Set up root URL for library, which differs for plugins vs. themes.
 *
 * @since  1.0.4
 */
function setup_root_url() {

    // Get BFA directory and theme root directory paths.
    $bfa_directory = dirname(__FILE__);
    $theme_directory = get_template_directory();
    $child_theme_directory = get_stylesheet_directory();
    $plugin_dir = plugin_dir_url( __FILE__ );

    /**
     * Check if we're inside a theme or plugin.
     *
     * If we're in a theme, than plugin_dir_url() will return a
     * funky URL that includes the actual file path (e.g.
     * /srv/www/site_name/wp-content/...)
     */
    $is_theme = false;
    if ( strpos( $plugin_dir, $bfa_directory ) !== false ) {
        $is_theme = true;
    }

    // First check if we're inside a theme.
    if ( $is_theme ) {

        // Use appropriate file paths for parent themes and child themes.
        if ( strpos( $bfa_directory, $theme_directory ) !== false ) {

            // Get relative BFA directory by removing theme root directory path.
            $bfa_rel_path = str_replace( $theme_directory, '', $bfa_directory );
            $this->root_url = trailingslashit( get_template_directory_uri() . $bfa_rel_path );

        } else {

            $bfa_rel_path = str_replace( $child_theme_directory, '', $bfa_directory );
            $this->root_url = trailingslashit( get_stylesheet_directory_uri() . $bfa_rel_path );

        }

    } else { // Otherwise we're inside a plugin.

        $this->root_url = trailingslashit( plugin_dir_url( __FILE__ ) );

    }

}

This code is working for me in the following situations: BFAL being in a theme by itself, BFA being in a parent theme, and BFAL being in a child theme where the parent doesn't include BFAL.

Any chance you can consider making this change or a similar change in BFAL? I'm happy to submit a PR also. I've only tested the new code in the cases I mentioned but I'm thinking it should work across the board.

simonfoxe commented 6 years ago

I'd like to second this - the iconpicker CSS doesn't enqueue with correct path from a parent theme that uses BFA, when utilising a child theme. This code fragment solves this.