Wordpress enqueue style зависимость загружается не в порядке - PullRequest
0 голосов
/ 29 сентября 2018

Итак, я собрал себя в конкатенаторе таблиц стилей enqueue, в то время как он работает, я обнаружил, что «зависимости» не в порядке ... в этом случае я отменяю регистрацию и удаляю из очереди мой«настраиваемая» таблица стилей, зарегистрируйте и поставьте в очередь мою составную таблицу стилей, а затем «повторно» зарегистрируйте и «повторно» поставьте в очередь мою настраиваемую таблицу стилей, используя описанный дескриптор в качестве зависимости для нее ... и она всегда загружается перед объединенной таблицей стилей.

Есть идеи, почему это может происходить?В моей дочерней теме я ставлю «custom» с приоритетом по умолчанию, а в родительской теме я выполняю конкатенацию, и мое действие имеет наивысший возможный приоритет.

Код:

protected function concatenate_css( $_cache_time ) {
    add_action( 'wp_enqueue_scripts', function( ) use ( $_cache_time ) {
        // need our global
        global $wp_styles;

        // our css string holder
        $_css_string = '';

        // path to the theme
        $_theme_path = get_stylesheet_directory( );

        // uri to the theme
        $_theme_uri = get_stylesheet_directory_uri( );

        // new file path
        $_new_css = $_theme_path . '/style.concat.css';

        // force the order based on the dependencies
        $wp_styles -> all_deps( $wp_styles -> queue );

        // setup our exclusions
        $_exclude = array( 'custom', 'concatcss', );

        // loop through everything in our global
        foreach( $wp_styles -> queue as $_hndl ) {

            // get the source from the hanlde
            $_path = $wp_styles -> registered[$_hndl]->src;

            // if we have a "custom" handle, we do not want to process it, so ... skip it
            // we also do want to process any source that is not set
            if( ! in_array( $_hndl, $_exclude ) && $_path ) {

                // we also only want to do this for local stylehseets
                if ( strpos( $_path, site_url( ) ) !== false ) {

                    $_path = ABSPATH . str_replace( site_url( ), '', $_path );

                    // now that we have everything we need, let's hold the contents of the file in a string variable, while concatenating
                    $_css_string .= file_get_contents( $_path ) . PHP_EOL;

                    // now remove the css from queue
                    wp_dequeue_style( $_hndl );
                    // and deregister it
                    wp_deregister_style( $_hndl );

                }
            }
        }

        // dequeue and deregsiter any "custom" style
        wp_dequeue_style( 'custom' );
        wp_deregister_style( 'custom' );

        // now write out the new stylesheet to the theme, and enqueue it
        // check the timestamp on it, versus the number of seconds we are specifying to see if we need to write a new file
        if( file_exists( $_new_css ) ) {
            $_ftime = filemtime( $_new_css ) + ( $_cache_time );
            $_ctime = time( );
            if( ( $_ftime <= $_ctime ) ) {
                file_put_contents( $_new_css, $_css_string );
            }
        } else {
            file_put_contents( $_new_css, $_css_string );
        }

        wp_register_style( 'concatcss', $_theme_uri . '/style.concat.css', array( ), null );
        wp_enqueue_style( 'concatcss' );

        // requeue and reregister our custom stylesheet, using this concatenated stylesheet as it's dependency
        wp_register_style( 'custom', $_theme_uri . '/css/custom.css?_=' . time( ), array( 'concatcss' ), null );
        wp_enqueue_style( 'custom' );

    }, PHP_INT_MAX );

}

Снимок экрана источника страницы

enter image description here

1 Ответ

0 голосов
/ 29 сентября 2018

Просто зарегистрируйте свой составной скрипт, но не ставьте его в очередь.

    wp_register_style( 'concatcss', $_theme_uri . '/style.concat.css', array( ), null );

    // requeue and reregister our custom stylesheet, using this concatenated stylesheet as it's dependency
    wp_enqueue_style( 'custom', $_theme_uri . '/css/custom.css?_=' . time( ), array( 'concatcss' ), null );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...