analogwp / analogwp-templates

Style Kits for Elementor adds a number of intuitive styling controls in the Elementor editor that allow you to apply styles globally or per page.
https://analogwp.com
27 stars 7 forks source link

Settings includes are getting exposed everywhere #243

Closed lushkant closed 4 years ago

lushkant commented 4 years ago

Reason

We have our settings saver function hooked at wp_loaded for conditional failure reasons with admin_menu hook and since wp_loaded gets fired before admin_init placing our settings include won't work there. https://github.com/mauryaratan/analogwp-templates/blob/25a05fbf1d1ea8a7ae82d210d1e29a969463174d/inc/register-settings.php#L122

So we have it included in an early action that is init, making it exposed to everywhere. https://github.com/mauryaratan/analogwp-templates/blob/25a05fbf1d1ea8a7ae82d210d1e29a969463174d/inc/register-settings.php#L173

Possible Fix

We can have a conditional admin check in the init hook and rest will work fine, without having our settings exposed outside.

/**
 * Default options.
 *
 * Sets up the default options used on the settings page.
 */
function create_options() {
    if ( ! is_admin() ) {
        return false;
    }
    // Include settings so that we can run through defaults.
    include_once dirname( __FILE__ ) . '/class-admin-settings.php';

    $settings = Admin_Settings::get_settings_pages();

    foreach ( $settings as $section ) {
        if ( ! method_exists( $section, 'get_settings' ) ) {
            continue;
        }
        $subsections = array_unique( array_merge( array( '' ), array_keys( $section->get_sections() ) ) );

        foreach ( $subsections as $subsection ) {
            foreach ( $section->get_settings( $subsection ) as $value ) {
                if ( isset( $value['default'] ) && isset( $value['id'] ) ) {
                    $autoload = isset( $value['autoload'] ) ? (bool) $value['autoload'] : true;
                    add_option( $value['id'], $value['default'], '', ( $autoload ? 'yes' : 'no' ) );
                }
            }
        }
    }
}
add_action( 'init', 'Analog\Settings\create_options', 20 );

(On a side note - We already have a conditional admin check in the setting saver function since it loads at wp_loaded which is also a global hook, it should have been as well added at init for settings includes in the first place.)