Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS Preprocessors #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 94 additions & 56 deletions functions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,98 @@
<?php
/**
* Pears functions and customizations
**/

// Setup for CSS preprocessors
// Possible options here:
// css, sass, scss, less, stylus
define("STYLE_TYPE", "scss");
define("COMPASS", true); // if you're using Sass/scss, will you be using compass?

add_action( 'add_meta_boxes', 'pears_add_meta_box' );
add_action( 'save_post', 'pears_save_post' );

function pears_add_meta_box() {

add_meta_box(
'pears',
'Pears',
'pears_meta_box',
'post',
'normal',
'high'
);

}

function pears_meta_box( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'pears_noncename' );

$html = get_post_meta($post->ID,'html',true);
$style = get_post_meta($post->ID,STYLE_TYPE,true);

echo '<p>These fields are for the HTML markup and '.STYLE_TYPE.' styles. The post body can be used for notes.</p>';
echo '<label for="html">HTML</label>';
echo '<p><textarea id="html" name="html" rows="20" cols="90" />' . $html . '</textarea></p>';
echo '<label for="style">'.strtoupper(STYLE_TYPE).'</label> ';
echo '<p><textarea id="style" name="style" rows="20" cols="90" />' . $style . '</textarea></p>';
}

function pears_save_post( $post_id ) {

// Ignore if doing an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;

// verify data came from pears meta box
if ( !wp_verify_nonce( $_POST['pears_noncename'], plugin_basename( __FILE__ ) ) )
return;

// Check user permissions
if ( 'post' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return;
}
else{
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}

$html_data = $_POST['html'];
update_post_meta($post_id, 'html', $html_data);

$style_data = $_POST['style'];
update_post_meta($post_id, STYLE_TYPE, $style_data);
}

// call css STYLE_TYPE convertion API
function convertScss($url, $source){
if (STYLE_TYPE != 'css') {
$postfields = array();
$postfields["type"] = STYLE_TYPE;
$postfields["source"] = (COMPASS) ? "@import 'compass';\n\n".$source : $source;

$the_query = http_build_query($postfields);

$session = curl_init();

curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($postfields));

$data = curl_exec($session);
curl_close($session);

} else {
$data = $source;
}
return $data;

}



/**
* TwentyTen functions and definitions
*
Expand Down Expand Up @@ -506,59 +600,3 @@ function twentyten_posted_in() {
);
}
endif;

add_action( 'add_meta_boxes', 'pears_add_meta_box' );
add_action( 'save_post', 'pears_save_post' );

function pears_add_meta_box() {

add_meta_box(
'pears',
'Pears',
'pears_meta_box',
'post',
'normal',
'high'
);

}

function pears_meta_box( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'pears_noncename' );

$html = get_post_meta($post->ID,'html',true);
$css = get_post_meta($post->ID,'css',true);

echo '<p>These fields are for the HTML markup and CSS styles. The post body can be used for notes.</p>';
echo '<label for="html">HTML</label>';
echo '<p><textarea id="html" name="html" rows="20" cols="90" />' . $html . '</textarea></p>';
echo '<label for="css">CSS</label> ';
echo '<p><textarea id="css" name="css" rows="20" cols="90" />' . $css . '</textarea></p>';
}

function pears_save_post( $post_id ) {

// Ignore if doing an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;

// verify data came from pears meta box
if ( !wp_verify_nonce( $_POST['pears_noncename'], plugin_basename( __FILE__ ) ) )
return;

// Check user permissions
if ( 'post' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return;
}
else{
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}

$html_data = $_POST['html'];
update_post_meta($post_id, 'html', $html_data);

$css_data = $_POST['css'];
update_post_meta($post_id, 'css', $css_data);
}
11 changes: 7 additions & 4 deletions loop-single.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

<style id="s" type="text/css">
<?php $key="css"; echo get_post_meta($post->ID, $key, true); ?>
<?php
// convert SCSS to CSS
echo convertScss("http://alloy.divshot.com/compile", get_post_meta($post->ID, STYLE_TYPE, true));
?>
</style>

<div id="pattern" class="mod group">
Expand All @@ -40,9 +43,9 @@
</div>

<div id="style" class="mod">
<h3 class="label">CSS</h3> <a href="#" class="clip" title="select code for copying"><img src="<?php bloginfo('template_directory'); ?>/images/icon-copy.png" alt="copy" /></a>
<textarea id="css-code" class="mod-ta">
<?php $key="css"; echo get_post_meta($post->ID, $key, true); ?>
<h3 class="label"><?php echo STYLE_TYPE ?></h3> <a href="#" class="clip" title="select code for copying"><img src="<?php bloginfo('template_directory'); ?>/images/icon-copy.png" alt="copy" /></a>
<textarea id="csss-code" class="mod-ta">
<?php echo get_post_meta($post->ID, STYLE_TYPE, true); ?>
</textarea>
</div>
</div>
Expand Down