You are here

function rrssb_gen_css in Ridiculously Responsive Social Sharing Buttons 7.2

Same name and namespace in other branches
  1. 8.2 rrssb.module \rrssb_gen_css()

Auto-generate CSS for buttons.

The RRSSB library CSS is static. This function takes account the results of hook_rrssb_buttons and hook_rrssb_buttons_alter to create dynamic, site-specific CSS. It optimises by only including buttons that are enabled.

The fact that this module is not relying on library CSS for buttons means that there is greater compatibility to work with older library versions that are missing CSS for newer buttons.

1 call to rrssb_gen_css()
rrssb_get_buttons in ./rrssb.module
Returns a Drupal render array for the buttons.

File

./rrssb.module, line 647

Code

function rrssb_gen_css() {
  $css = "/* Auto-generated RRSSB CSS file. */\n";
  $settings = rrssb_settings();
  foreach ($settings as $name => $button) {

    // Add a white fill.
    $svg = str_replace('<path ', '<path fill="#FFF" ', $button['svg']);

    // URL encode - only certain special characters are needed: <>#" and " can be safely swapped for '.
    $svg = strtr($svg, array(
      '<' => '%3C',
      '>' => '%3E',
      '#' => '%23',
      '"' => '\'',
    ));
    $css .= <<<EOM
.rrssb-buttons li.rrssb-{<span class="php-variable">$name</span>} a { background-color: {<span class="php-variable">$button</span>[<span class="php-string">'color'</span>]}; }
.rrssb-buttons li.rrssb-{<span class="php-variable">$name</span>} a:hover { background-color: {<span class="php-variable">$button</span>[<span class="php-string">'color_hover'</span>]}; }
.rrssb-{<span class="php-variable">$name</span>} .rrssb-icon { background: url("data:image/svg+xml,{<span class="php-variable">$svg</span>}"); }

EOM;
  }

  // Save to a unique filename.
  $id = substr(hash('sha256', serialize($settings) . microtime()), 0, 8);
  $dir = 'public://rrssb';
  $file = "{$dir}/rrssb.{$id}.css";
  file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
  file_unmanaged_save_data($css, $file, FILE_EXISTS_REPLACE);

  // Delete the old file and record the new location.
  if ($old = variable_get('rrssb_css_file')) {
    file_unmanaged_delete($old);
  }
  variable_set('rrssb_css_file', $file);
  return $file;
}