Skip to content
This repository has been archived by the owner on Mar 4, 2018. It is now read-only.

Commit

Permalink
zy: Fixed and improved custom background image feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyml committed Dec 15, 2011
1 parent 4e7a9dc commit a451747
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 46 deletions.
2 changes: 2 additions & 0 deletions functions.php
Expand Up @@ -89,8 +89,10 @@ function arras_setup() {
remove_action( 'wp_head', 'pagenavi_css' );

add_action( 'arras_head', 'arras_override_styles' );

add_action( 'arras_custom_styles', 'arras_add_custom_logo' );
add_action( 'arras_custom_styles', 'arras_constrain_footer_sidebars' );
add_action( 'arras_custom_styles', 'arras_add_custom_background' );

add_action( 'arras_beside_nav', 'arras_social_nav' );

Expand Down
80 changes: 35 additions & 45 deletions library/admin/background.php
Expand Up @@ -21,8 +21,24 @@ function arras_custom_background_scripts() {
}

function arras_custom_background_styles() {
?> <link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/css/admin.css" type="text/css" /> <?php
wp_enqueue_style('farbtastic');
wp_enqueue_style( 'arras-admin', get_template_directory_uri() . '/css/admin.css', false, '2011-12-12', 'all' );
wp_enqueue_style( 'farbtastic' );
}

function arras_get_custom_background_defaults() {
$_defaults = array(
'enable' => false,
'id' => 0,
'attachment' => 'scroll',
'pos-x' => 'center',
'pos-y' => 'center',
'repeat' => 'no-repeat',
'color' => '#F0F0F0',
'foreground' => false,
'wrap' => false
);

return $_defaults;
}

function arras_custom_background() {
Expand All @@ -32,23 +48,16 @@ function arras_custom_background() {

if ( isset($_REQUEST['reset']) ) {
check_admin_referer('arras-custom-background');
$arras_custom_bg_options = array(
'enable' => false,
'id' => 0,
'attachment' => 'scroll',
'pos-x' => 'center',
'pos-y' => 'center',
'repeat' => 'no-repeat',
'color' => '#F0F0F0',
'foreground' => false,
'wrap' => false
);
update_option('arras_custom_bg_options', maybe_serialize($arras_custom_bg_options));
$arras_custom_bg_options = arras_get_custom_background_defaults();
update_option( 'arras_custom_bg_options', maybe_serialize($arras_custom_bg_options) );
$notices = '<div class="updated"><p>' . __('Your settings have been reverted to the defaults.', 'arras') . '</p></div>';
}

if ( isset($_REQUEST['save']) ) {
var_dump( $_REQUEST );

check_admin_referer('arras-custom-background');
$defaults = arras_get_custom_background_defaults();

if ($_FILES['import']['error'] != 4) {
$overrides = array('test_form' => false);
Expand All @@ -73,18 +82,19 @@ function arras_custom_background() {
$arras_custom_bg_options['id'] = wp_insert_attachment($object, $file);
}

$arras_custom_bg_options['enable'] = (boolean)(isset($_POST['bg-enable']));
$arras_custom_bg_options['attachment'] = stripslashes($_POST['bg-attachment']);
$arras_custom_bg_options['pos-x'] = stripslashes($_POST['bg-pos-x']);
$arras_custom_bg_options['pos-y'] = stripslashes($_POST['bg-pos-y']);
$arras_custom_bg_options['repeat'] = stripslashes($_POST['bg-repeat']);
$arras_custom_bg_options['color'] = stripslashes($_POST['bg-color']);
$arras_custom_bg_options['foreground'] = (boolean)(isset($_POST['foreground']));
$arras_custom_bg_options['wrap'] = (boolean)(isset($_POST['wrap']));
$arras_custom_bg_options['enable'] = ( isset( $_POST['bg-enable'] ) );

update_option('arras_custom_bg_options', maybe_serialize($arras_custom_bg_options));
$arras_custom_bg_options['attachment'] = isset( $_POST['bg-attachment'] ) ? $_POST['bg-attachment'] : $defaults['attachment'];
$arras_custom_bg_options['pos-x'] = isset( $_POST['bg-pos-x'] ) ? $_POST['bg-pos-x'] : $defaults['pos-x'];
$arras_custom_bg_options['pos-y'] = isset( $_POST['bg-pos-y'] ) ? $_POST['bg-pos-y'] : $defaults['pos-y'];
$arras_custom_bg_options['repeat'] = isset( $_POST['bg-repeat'] ) ? $_POST['bg-repeat'] : $defaults['repeat'];
$arras_custom_bg_options['color'] = ( isset( $_POST['bg-color'] ) && preg_match( '/^#?(([a-fA-F0-9]){3}){1,2}$/', $_POST['bg-color'] ) )? $_POST['bg-color'] : $defaults['color'];
$arras_custom_bg_options['foreground'] = isset( $_POST['foreground'] );
$arras_custom_bg_options['wrap'] = isset( $_POST['wrap'] ) ? $_POST['wrap'] : $defaults['wrap'];

update_option( 'arras_custom_bg_options', maybe_serialize( $arras_custom_bg_options ) );

$notices = '<div class="updated"><p>' . __('Your settings have been saved to the database.', 'arras') . '</p></div>';
$notices = '<div class="updated"><p>' . __( 'Your settings have been saved to the database.', 'arras' ) . '</p></div>';
}

if ( isset($_GET['page']) && $_GET['page'] == 'arras-custom-background' ) : ?>
Expand Down Expand Up @@ -150,7 +160,7 @@ function arras_custom_background() {
</div><!-- #custom-bg-options -->
<div id="custom-bg-preview">
<h3><?php _e('Current Background Image', 'arras') ?></h3>
<?php if ( $arras_custom_bg_options['id'] != 0 ) echo wp_get_attachment_image($arras_custom_bg_options['id'], 'full'); ?>
<?php if ( isset( $arras_custom_bg_options['id'] ) && $arras_custom_bg_options['id'] != 0 ) echo wp_get_attachment_image($arras_custom_bg_options['id'], 'full'); ?>
</div><!-- #custom-bg-preview -->
</form>

Expand All @@ -161,24 +171,4 @@ function arras_custom_background() {
<?php
}

function arras_add_custom_background() {
global $arras_custom_bg_options;

if (!$arras_custom_bg_options['enable']) return false;

$img = wp_get_attachment_image_src($arras_custom_bg_options['id'], 'full');

if ($arras_custom_bg_options['wrap']) $css_class = 'body';
else $css_class ='#wrapper';
?>
<?php echo $css_class ?> { background:<?php if($arras_custom_bg_options['id'] != 0) echo ' url(' . $img[0] . ')'; ?> <?php echo $arras_custom_bg_options['pos-x'] . ' ' . $arras_custom_bg_options['pos-y'] . ' ' . $arras_custom_bg_options['attachment'] . ' ' . $arras_custom_bg_options['repeat'] . ' ' . $arras_custom_bg_options['color']; ?> !important; }
<?php
if ($arras_custom_bg_options['foreground']) :
?>
#main { background: url(<?php echo get_template_directory_uri() ?>/images/foreground.png) !important; }
<?php
endif;
}
add_action('arras_custom_styles', 'arras_add_custom_background');

?>
33 changes: 32 additions & 1 deletion library/styles.php
Expand Up @@ -18,7 +18,6 @@ function is_valid_arras_style($file) {

function arras_override_styles() {
?>

<!-- Generated by Arras WP Theme -->
<style type="text/css">
<?php do_action('arras_custom_styles'); ?>
Expand Down Expand Up @@ -78,5 +77,37 @@ function arras_load_styles() {
do_action( 'arras_load_styles' );
}

function arras_add_custom_background() {
global $arras_custom_bg_options;

if ( !isset( $arras_custom_bg_options ) )
$arras_custom_bg_options = maybe_unserialize( get_option( 'arras_custom_bg_options' ) );

if ( !$arras_custom_bg_options['enable'] )
return false;

if ( isset( $arras_custom_bg_options['id'] ) )
$img = wp_get_attachment_image_src( $arras_custom_bg_options['id'], 'full' );

if ($arras_custom_bg_options['wrap']) $css_class = 'body';
else $css_class ='#wrapper';

echo $css_class . '{ background: ';
if ( isset( $img ) ) {
echo ' url(' . $img[0] . ') ' . $arras_custom_bg_options['pos-x'] . ' ' . $arras_custom_bg_options['pos-y'] . ' ' . $arras_custom_bg_options['attachment'] . ' ' . $arras_custom_bg_options['repeat'] . ' ';
}

if ( isset( $arras_custom_bg_options['color'] ) )
echo $arras_custom_bg_options['color'];

echo ' !important; }';

if ($arras_custom_bg_options['foreground']) {
?>
#main { background: url(<?php echo get_template_directory_uri() ?>/images/foreground.png) !important; }
<?php
}
}

/* End of file styles.php */
/* Location: ./library/styles.php */

0 comments on commit a451747

Please sign in to comment.