Example #1
0
 /**
  * Gets CSS selectors for realtime appearance manipulations
  * @param string $what What to return. 'string' for pregenerated JS array or 'raw' for PHP array
  * @return array Array of CSS properties, LESS variables and selectors
  */
 public static function getCssFontSelectors($optionKey = null)
 {
     aitIncludeLess();
     $less = new AitLess(THEME_DIR . '/style.less.css');
     $variables = aitPrepareVariablesForLess();
     $styleLess = $less->parseTree();
     $selectors = array();
     $isLessVar = false;
     $i = -1;
     foreach ($styleLess as $key => $value) {
         if (is_array($value)) {
             foreach ($value as $i => $k) {
                 if (is_array($k) and $k[0] == 'block') {
                     foreach ($k[1]->props as $property) {
                         if ($property[1] == 'font-family') {
                             $isLessVar = false;
                             if (isset($property[2][2][0][0]) and $property[2][2][0][0] == 'variable' or isset($property[2][2][1][0]) and $property[2][2][1][0] == 'variable') {
                                 if ($property[2][2][0][1][0] == "@") {
                                     $option = trim($property[2][2][0][1], "@");
                                 }
                                 $isLessVar = true;
                             }
                             if (isset($property[2][0]) and $property[2][0] == 'variable') {
                                 if ($property[2][1][0] == "@") {
                                     $option = trim($property[2][1], "@");
                                 }
                                 $isLessVar = true;
                             }
                             if ($isLessVar) {
                                 $i++;
                                 $tags = is_array($k[1]->tags) ? implode(', ', $k[1]->tags) : $k[1]->tags;
                                 preg_match_all('/:hover/i', $tags, $hovers);
                                 if (!empty($hovers) and isset($hovers[0][0])) {
                                     foreach ($k[1]->props as $p) {
                                         list(, $pname, $pvalue) = $p;
                                         $line = $less->reduce($pvalue, $variables);
                                         if ($p[1] == 'text-shadow') {
                                             $selectors[$option]['hover'][$i]['text-shadow'] = $less->compileValue($line);
                                         }
                                         if ($p[1] == 'color') {
                                             $selectors[$option]['hover'][$i]['color'] = $less->compileValue($line);
                                         }
                                     }
                                     $tags = preg_replace('/:hover/i', '', $tags);
                                 }
                                 $selectors[$option]['selectors'][$i] = $tags;
                                 foreach ($k[1]->props as $p) {
                                     if ($p[1] == 'text-shadow') {
                                         list(, $pname, $pvalue) = $p;
                                         $selectors[$option]['text-shadow'][$i] = $less->compileValue($less->reduce($pvalue, $variables));
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $i = 0;
     $s = array();
     foreach ($selectors as $key => $values) {
         $i = 0;
         foreach ($values as $group => $value) {
             foreach ($value as $index => $v) {
                 $tags = $selectors[$key]['selectors'][$index];
                 $s[$key][$tags]['selectors'] = $tags;
                 if (isset($selectors[$key]['text-shadow']) and isset($selectors[$key]['text-shadow'][$index])) {
                     $s[$key][$tags]['text-shadow'] = $selectors[$key]['text-shadow'][$index];
                 }
                 if (isset($selectors[$key]['hover']) and isset($selectors[$key]['hover'][$index])) {
                     $s[$key][$tags]['hover'] = $selectors[$key]['hover'][$index];
                 }
             }
         }
     }
     if (empty($optionKey)) {
         return $s;
     } else {
         if (isset($s[$optionKey])) {
             return $s[$optionKey];
         } else {
             return 'bad option key';
         }
     }
     return $return;
 }
Example #2
0
/**
 * Generates CSS from LESS
 * @param string $input Absolut path to LESS file
 * @param string $output Absolut path to CSS file
 * @param array $options
 */
function aitSaveLess2Css($input = null, $output = null, $options = null)
{
    aitIncludeLess();
    if ($input === null and $output === null and $options === null) {
        $options = get_option(AIT_OPTIONS_KEY);
        $input = THEME_DIR . "/style.less.css";
        $output = THEME_STYLESHEET_FILE;
    }
    if ($options === false) {
        // for theme preview
        $options = aitGetThemeDefaultOptions($GLOBALS['aitThemeConfig']);
    }
    $less = new AitLess();
    $less->importDir = THEME_DIR . '/';
    $content = file_get_contents($input);
    $onlyDesignVars = true;
    $configTypes = aitGetOptionsTypes($GLOBALS['aitThemeConfig'], $onlyDesignVars);
    $customCss = aitGetOptionsByType(array('custom-css', 'custom-css-vars'), $configTypes, $options);
    if (isset($customCss['custom-css'])) {
        foreach ($customCss['custom-css'] as $css) {
            $content .= $css['value'];
            unset($options[$css['section']][$css['key']]);
        }
    }
    $variables = aitPrepareVariablesForLess($options, $configTypes);
    try {
        $css = $less->parse($content, $variables);
    } catch (Exception $e) {
        wp_die($e->getMessage());
    }
    // save also comment header
    preg_match("/\\/\\*.*?\\*\\//s", $content, $match);
    $header = $match[0];
    unset($content);
    // clean up
    $header .= "\n\n/* *************************************\n *    !!! Do not edit this file !!!    *\n * Please edit style.less.css instead. *\n * *********************************** */\n\n";
    if (!defined('AIT_DEVELOPMENT') or AIT_DEVELOPMENT != true) {
        $css = preg_replace('~\\s*([:;{},])\\s*~', '\\1', preg_replace('~/\\*.*\\*/~sU', '', $css));
    }
    @chmod($output, 0777);
    $written = @file_put_contents($output, $header . "\n" . $css);
    @chmod($output, 0755);
    if ($written === false) {
        return false;
    } else {
        return true;
    }
}