Skip to content

Instantly share code, notes, and snippets.

@htvu
Created March 30, 2013 14:18
Show Gist options
  • Select an option

  • Save htvu/5276880 to your computer and use it in GitHub Desktop.

Select an option

Save htvu/5276880 to your computer and use it in GitHub Desktop.

Revisions

  1. htvu created this gist Mar 30, 2013.
    235 changes: 235 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,235 @@
    /**
    * Inserts CSS with field_id markers.
    *
    * Inserts CSS into a dynamic.css file, placing it between
    * BEGIN and END field_id markers. Replaces existing marked info,
    * but still retains surrounding data.
    *
    * @param string $field_id The CSS option field ID.
    * @param array $options The current option_tree array.
    * @return bool True on write success, false on failure.
    *
    * @access public
    * @since 1.1.8
    * @updated 2.0
    */
    if ( ! function_exists( 'ot_insert_css_with_markers' ) ) {

    function ot_insert_css_with_markers( $field_id = '', $insertion = '', $meta = false ) {

    /* missing $field_id or $insertion exit early */
    if ( '' == $field_id || '' == $insertion )
    return;

    /* path to the dynamic.css file */
    $filepath = get_stylesheet_directory() . '/dynamic.css';

    /* allow filter on path */
    $filepath = apply_filters( 'css_option_file_path', $filepath, $field_id );

    /* grab a copy of the paths array */
    $ot_css_file_paths = get_option( 'ot_css_file_paths', array() );

    /* set the path for this field */
    $ot_css_file_paths[$field_id] = $filepath;

    /* update the paths */
    update_option( 'ot_css_file_paths', $ot_css_file_paths );

    /* insert CSS into file */
    if ( file_exists( $filepath ) ) {

    $insertion = ot_normalize_css( $insertion );
    //$regex = "/{{([a-zA-Z0-9\_\-\#\|\=]+)}}/";
    $regex = "/{{([a-zA-Z0-9\_\-\#\|\=\<\>]+)}}/"; // HTVU
    $marker = $field_id;

    /* Match custom CSS */
    preg_match_all( $regex, $insertion, $matches );

    /* Loop through CSS */
    foreach( $matches[0] as $option ) {

    // Begin HTVU code
    $my_matches = '';
    if (preg_match_all ("/<([a-zA-Z0-9\_\-\#\|\=\<\>]+)>/", $option, $my_matches)>0) {
    $my_matches = str_replace(array('<', '>'), '', $my_matches[0]);
    if (is_array($my_matches)) $my_matches = $my_matches[0];
    $ori_option = $option;
    $option = preg_replace("/<([a-zA-Z0-9\_\-\#\|\=\<\>]+)>/", '', $option);
    } else {$my_matches = false;}
    // End HTVU code

    $value = '';
    $option_id = str_replace( array( '{{', '}}' ), '', $option );
    $option_array = explode( '|', $option_id );

    /* get the array value */
    if ( $meta ) {
    global $post;

    $value = get_post_meta( $post->ID, $option_array[0], true );

    } else {

    $options = get_option( 'option_tree' );

    if ( isset( $options[$option_array[0]] ) ) {

    $value = $options[$option_array[0]];

    }

    }

    if ( is_array( $value ) ) {

    if ( ! isset( $option_array[1] ) ) {

    /* Measurement */
    if ( isset( $value[0] ) && isset( $value[1] ) ) {

    /* set $value with measurement properties */
    $value = $value[0].$value[1];

    /* typography */
    } else if ( ot_array_keys_exists( $value, array( 'font-color', 'font-family', 'font-size', 'font-style', 'font-variant', 'font-weight', 'letter-spacing', 'line-height', 'text-decoration', 'text-transform' ) ) ) {
    $font = array();

    if ( ! empty( $value['font-color'] ) )
    $font[] = "color: " . $value['font-color'] . ";";

    if ( ! empty( $value['font-family'] ) ) {
    foreach ( ot_recognized_font_families() as $key => $v ) {
    if ( $key == $value['font-family'] ) {
    $font[] = "font-family: " . $v . ";";
    }
    }
    }

    if ( ! empty( $value['font-size'] ) )
    $font[] = "font-size: " . $value['font-size'] . ";";

    if ( ! empty( $value['font-style'] ) )
    $font[] = "font-style: " . $value['font-style'] . ";";

    if ( ! empty( $value['font-variant'] ) )
    $font[] = "font-variant: " . $value['font-variant'] . ";";

    if ( ! empty( $value['font-weight'] ) )
    $font[] = "font-weight: " . $value['font-weight'] . ";";

    if ( ! empty( $value['letter-spacing'] ) )
    $font[] = "letter-spacing: " . $value['letter-spacing'] . ";";

    if ( ! empty( $value['line-height'] ) )
    $font[] = "line-height: " . $value['line-height'] . ";";

    if ( ! empty( $value['text-decoration'] ) )
    $font[] = "text-decoration: " . $value['text-decoration'] . ";";

    if ( ! empty( $value['text-transform'] ) )
    $font[] = "text-transform: " . $value['text-transform'] . ";";

    /* set $value with font properties or empty string */
    $value = ! empty( $font ) ? implode( "\n", $font ) : '';

    /* background */
    } else if ( ot_array_keys_exists( $value, array( 'background-color', 'background-image', 'background-repeat', 'background-attachment', 'background-position' ) ) ) {
    $bg = array();

    if ( ! empty( $value['background-color'] ) )
    $bg[] = $value['background-color'];

    if ( ! empty( $value['background-image'] ) )
    $bg[] = 'url("' . $value['background-image'] . '")';

    if ( ! empty( $value['background-repeat'] ) )
    $bg[] = $value['background-repeat'];

    if ( ! empty( $value['background-attachment'] ) )
    $bg[] = $value['background-attachment'];

    if ( ! empty( $value['background-position'] ) )
    $bg[] = $value['background-position'];

    /* set $value with background properties or empty string */
    $value = ! empty( $bg ) ? 'background: ' . implode( " ", $bg ) . ';' : '';
    }

    } else {

    $value = $value[$option_array[1]];

    }
    }

    // Begin HTVU code
    if ($my_matches) {
    $value = apply_filters("ot_css_value_{$my_matches}", $value);
    $option = $ori_option;
    }
    // End HTVU code

    /* insert CSS, even if the value is empty */
    $insertion = stripslashes( str_replace( $option, $value, $insertion ) );
    }

    /* create array from the lines of code */
    $markerdata = explode( "\n", implode( '', file( $filepath ) ) );

    /* can't write to the file return false */
    if ( ! $f = @fopen( $filepath, 'w' ) )
    return false;

    $searching = true;
    $foundit = false;

    /* has array of lines */
    if ( ! empty( $markerdata ) ) {

    /* foreach line of code */
    foreach( $markerdata as $n => $markerline ) {

    /* found begining of marker, set $searching to false */
    if ( $markerline == "/* BEGIN {$marker} */" )
    $searching = false;

    /* keep rewrite each line of CSS */
    if ( $searching == true ) {
    if ( $n + 1 < count( $markerdata ) )
    fwrite( $f, "{$markerline}\n" );
    else
    fwrite( $f, "{$markerline}" );
    }

    /* found end marker write code */
    if ( $markerline == "/* END {$marker} */" ) {
    fwrite( $f, "/* BEGIN {$marker} */\n" );
    fwrite( $f, "{$insertion}\n" );
    fwrite( $f, "/* END {$marker} */\n" );
    $searching = true;
    $foundit = true;
    }

    }

    }

    /* nothing inserted, write code. DO IT, DO IT! */
    if ( ! $foundit ) {
    fwrite( $f, "/* BEGIN {$marker} */\n" );
    fwrite( $f, "{$insertion}\n" );
    fwrite( $f, "/* END {$marker} */\n" );
    }

    /* close file */
    fclose( $f );
    return true;
    }

    return false;

    }

    }