コード例 #1
0
 /**
  * Replaces LESS imports with the content from the actual files. This used as a preg callback.
  *
  * @param $matches
  *
  * @return string
  */
 private function get_less_import_contents($matches)
 {
     $filename = $matches[1];
     // First, we'll deal with a few special cases
     switch ($filename) {
         case 'mixins':
             return file_get_contents(plugin_dir_path(__FILE__) . 'less/mixins.less');
             break;
         case 'lesshat':
             return file_get_contents(plugin_dir_path(__FILE__) . 'less/lesshat.less');
             break;
     }
     //get file extension
     preg_match('/\\.\\w+$/', $filename, $ext);
     //if there is a file extension and it's not .less or .css we ignore
     if (!empty($ext)) {
         if (!($ext[0] == '.less' || $ext[0] == '.css')) {
             return '';
         }
     } else {
         $filename .= '.less';
     }
     //first check local widget styles directory and then bundle less directory
     $search_path = array(siteorigin_widget_get_plugin_dir_path($this->id_base) . 'styles/', plugin_dir_path(__FILE__) . 'less/');
     foreach ($search_path as $dir) {
         if (file_exists($dir . $filename)) {
             return file_get_contents($dir . $filename) . "\n\n";
         }
     }
     //file not found
     return '';
 }
コード例 #2
0
 /**
  * Generate the CSS for the widget.
  *
  * @param $instance
  * @return string
  */
 public function get_instance_css($instance)
 {
     if (!class_exists('lessc')) {
         require plugin_dir_path(__FILE__) . 'inc/lessc.inc.php';
     }
     $style_name = $this->get_style_name($instance);
     if (empty($style_name)) {
         return '';
     }
     $less = file_get_contents(siteorigin_widget_get_plugin_dir_path($this->id_base) . 'styles/' . $style_name . '.less');
     // Substitute the variables
     if (!class_exists('SiteOrigin_Widgets_Color_Object')) {
         require plugin_dir_path(__FILE__) . 'inc/color.php';
     }
     $vars = $this->get_less_variables($instance);
     if (!empty($vars)) {
         foreach ($vars as $name => $value) {
             if (empty($value)) {
                 continue;
             }
             $less = preg_replace('/\\@' . preg_quote($name) . ' *\\:.*?;/', '@' . $name . ': ' . $value . ';', $less);
         }
     }
     $mixins = file_get_contents(plugin_dir_path(__FILE__) . 'less/mixins.less');
     $less = preg_replace('/@import \\".*mixins\\";/', $mixins . "\n\n", $less);
     $style = $this->get_style_name($instance);
     $hash = $this->get_style_hash($instance);
     $css_name = $this->id_base . '-' . $style . '-' . $hash;
     $less = '.so-widget-' . $css_name . ' { ' . $less . ' } ';
     $c = new lessc();
     return $c->compile($less);
 }