Advanced aggregation critical css module.

Fichier

advagg_critical_css/advagg_critical_css.module

View source
<?php


/**
 * @file
 * Advanced aggregation critical css module.
 */

/**
 * @addtogroup hooks
 * @{
 */

/**
 * Implements hook_menu().
 */
function advagg_critical_css_menu() {
    $file_path = drupal_get_path('module', 'advagg_critical_css');
    $config_path = advagg_admin_config_root_path();
    $items[$config_path . '/advagg/critical-css'] = array(
        'title' => 'Critical CSS',
        'description' => 'Control critical css.',
        'page callback' => 'drupal_get_form',
        'page arguments' => array(
            'advagg_critical_css_admin_settings_form',
        ),
        'type' => MENU_LOCAL_TASK,
        'access arguments' => array(
            'administer site configuration',
        ),
        'file path' => $file_path,
        'file' => 'advagg_critical_css.admin.inc',
        'weight' => 10,
    );
    return $items;
}

/**
 * Implements hook_module_implements_alter().
 */
function advagg_critical_css_module_implements_alter(&$implementations, $hook) {
    // Move critical_css_advagg_mod_critical_css_file_pre_alter to the bottom.
    if ($hook === 'critical_css_advagg_mod_critical_css_file_pre_alter' && array_key_exists('advagg_critical_css', $implementations)) {
        $item = $implementations['advagg_critical_css'];
        unset($implementations['advagg_critical_css']);
        $implementations['advagg_critical_css'] = $item;
    }
}

/**
 * @} End of "addtogroup hooks".
 */

/**
 * @addtogroup advagg_hooks
 * @{
 */

/**
 * Implements hook_advagg_mod_critical_css_file_pre_alter().
 */
function advagg_critical_css_advagg_mod_critical_css_file_pre_alter(&$filename, &$params, &$inline_strings) {
    list($dirs, $front_page, $object) = $params;
    // Build query parameters.
    $lookup = array(
        $dirs[6],
    );
    if ($front_page) {
        $lookup = array(
            '<front>',
        );
    }
    $lookup[] = $dirs[9];
    $lookup[] = $dirs[10];
    if (!empty($object->type)) {
        $lookup[] = $object->type;
    }
    $type = array(
        2,
        8,
    );
    $users = array(
        rtrim($dirs[2], '/\\'),
        rtrim($dirs[3], '/\\'),
    );
    // Get Results.
    $result = advagg_critical_css_table_get($GLOBALS['theme'], $type, $lookup, $users);
    // Put into the inline strings array.
    if (!empty($result)) {
        // Set string values.
        $inline_strings[0] = $result['css'];
        $inline_strings[1] = $result['dns'];
        $inline_strings[2] = $result['pre'];
        // Disable file lookup.
        $dirs[0] = '';
        $dirs[1] = '';
    }
    // Repack the $params array.
    $params = array(
        $dirs,
        $front_page,
        $object,
    );
}

/**
 * Implements hook_advagg_mod_critical_css_file_post_alter().
 */
function advagg_critical_css_advagg_mod_critical_css_file_post_alter(&$filename, &$params, &$inline_strings) {
    if (!empty($inline_strings[0])) {
        // Remove given css selectors.
        $selectors = variable_get('advagg_critical_css_selector_blacklist', '');
        $selectors_array = array_filter(array_map('trim', explode("\n", $selectors)));
        foreach ($selectors_array as $pattern) {
            $pattern = preg_quote($pattern, '/');
            $pattern = "/([^}]*{$pattern}[^{]*[^}]*\\})/s";
            $inline_strings[0] = preg_replace($pattern, '', $inline_strings[0]);
        }
    }
}

/**
 * @} End of "addtogroup advagg_hooks".
 */

/**
 * Get the db select return object.
 *
 * @param string $theme
 *   Name of the current theme.
 * @param array $type
 *   Array of int types to lookup.
 * @param array $lookup
 *   The lookup value.
 * @param array $user
 *   Array of user string values.
 *
 * @return SelectQuery
 *   Return the SelectQuery object after it has been executed.
 */
function advagg_critical_css_table_get($theme, array $type, array $lookup, array $user) {
    $output = array();
    try {
        $results = db_select('advagg_critical_css', 'acc')->fields('acc')
            ->condition('theme', $theme)
            ->condition('type', $type, 'IN')
            ->condition('user', $user, 'IN')
            ->condition('lookup', $lookup, 'IN')
            ->orderBy('type', 'DESC')
            ->execute();
        // Get first result.
        $output = $results->fetchAssoc();
        // Check for a better match in other results if they exist.
        foreach ($results as $values) {
            $values = (array) $values;
            if ($values['type'] < $output['type']) {
                $output = $values;
                break;
            }
            if ($values['type'] = $output['type']) {
                if (($values['user'] === 'anonymous' || $values['user'] === 'authenticated') && $output['user'] === 'all') {
                    $output = $values;
                    break;
                }
                if (is_int($values['user'])) {
                    $output = $values;
                    break;
                }
            }
        }
    } catch (PDOException $e) {
        // Log the error if in development mode.
        if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0) {
            watchdog('advagg_critical_css', 'Development Mode - Caught PDO Exception: <code>@info</code>', array(
                '@info' => $e,
            ));
        }
    }
    return $output;
}

/**
 * Insert/Update data in the advagg_critical_css table.
 *
 * @param array $records
 *   List of rows needed that need to be changed in the db.
 *
 * @return array
 *   Return array of booleans if anything was written to the database.
 */
function advagg_critical_css_table_insert_update(array $records) {
    $return = array();
    foreach ($records as $values) {
        list($keys, $record) = $values;
        if (!isset($record['settings'])) {
            $record['settings'] = '';
        }
        try {
            $return[] = db_merge('advagg_critical_css')->key(array(
                'theme' => $keys['theme'],
                'user' => $keys['user'],
                'type' => $keys['type'],
                'lookup' => $keys['lookup'],
            ))
                ->fields($record)
                ->execute();
        } catch (PDOException $e) {
            // Log the error if in development mode.
            if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0) {
                watchdog('advagg_critical_css', 'Development Mode - Caught PDO Exception: <code>@info</code>', array(
                    '@info' => $e,
                ));
            }
        }
    }
    return $return;
}

/**
 * Delete data in the advagg_critical_css table.
 *
 * @param array $records
 *   List of rows needed that need to be removed from the db.
 *
 * @return array
 *   Return array of booleans if anything was removed from the database.
 */
function advagg_critical_css_table_delete(array $records) {
    $return = array();
    foreach ($records as $record) {
        try {
            $return[] = db_delete('advagg_critical_css')->condition('theme', $record['theme'])
                ->condition('user', $record['user'])
                ->condition('type', $record['type'])
                ->condition('lookup', $record['lookup'])
                ->execute();
        } catch (PDOException $e) {
            // Log the error if in development mode.
            if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0) {
                watchdog('advagg_critical_css', 'Development Mode - Caught PDO Exception: <code>@info</code>', array(
                    '@info' => $e,
                ));
            }
        }
    }
    return $return;
}

Functions

Titre Deprecated Résumé
advagg_critical_css_advagg_mod_critical_css_file_post_alter Implements hook_advagg_mod_critical_css_file_post_alter().
advagg_critical_css_advagg_mod_critical_css_file_pre_alter Implements hook_advagg_mod_critical_css_file_pre_alter().
advagg_critical_css_menu Implements hook_menu().
advagg_critical_css_module_implements_alter Implements hook_module_implements_alter().
advagg_critical_css_table_delete Delete data in the advagg_critical_css table.
advagg_critical_css_table_get Get the db select return object.
advagg_critical_css_table_insert_update Insert/Update data in the advagg_critical_css table.