bm-custom-login.php 12.0 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
<?php
/*
Plugin Name: BM Custom Login
Plugin URI: http://www.binarymoon.co.uk/projects/bm-custom-login/
Description: Display custom images on the wordpress login screen. Useful for branding.
Author: Ben Gillbanks
Version: 1.6.6
Author URI: http://www.binarymoon.co.uk/
*/

/**
 * TODO : add contextual help
 * TODO : add secondary navigation
 * TODO : full blown administration customisation
 */

define ('CL_GROUP', 'custom_login');
define ('CL_PAGE', 'custom_login_admin');
define ('CL_SECTION', 'custom_login_section');
define ('CL_OPTIONS', 'custom_login_options');
define ('CL_LOCAL', 'custom_login');

$cl_options = array ();


/**
 *
 * @global type $cl_options
 * @return type 
 */
function custom_login_get_options() {
	
	global $cl_options;
	
	if ( empty( $cl_options ) ) {
		$cl_options = get_option( CL_OPTIONS );
	}
	
	return $cl_options;
	
}


/**
 * 
 */
function bm_custom_login () {
	
	$cl_options = custom_login_get_options ();

	// default wordpress mu plugin path
	$pluginPath = '/wp-content/mu-plugins/';
	
	// is it wordpress mu or wordpress normal?
	if (!is_dir ($pluginPath)) {
		$pluginPath = '/wp-content/plugins/';
	}

	$pluginUrl = get_option ('siteurl') . $pluginPath . plugin_basename (dirname (__FILE__)) . '/bm-custom-login.css';
	
	// output styles
	echo '<link rel="stylesheet" type="text/css" href="' . $pluginUrl . '" />';
	echo '<style>';
	
	$background = $cl_options['cl_background'];
	
	if (!empty ($background)) {
?>
	#login {
		background:url(<?php echo $background; ?>) top center no-repeat;
	}
<?php
	}

	// text colour
	if (!empty ($cl_options['cl_color'])) {
?>
	#login,
	#login label {
		color:#<?php echo $cl_options['cl_color']; ?>;
	}
<?php
	}

	$body_styles = array();
	// background colour
	if (!empty ($cl_options['cl_backgroundColor'])) {
		$body_styles[] = '#' . $cl_options['cl_backgroundColor'];
	}
	if (!empty ($cl_options['cl_backgroundImage'])) {
		$body_styles[] = 'url(' . $cl_options['cl_backgroundImage'] . ')';
		if (!empty($cl_options['cl_backgroundPX'])) { $body_styles[] = $cl_options['cl_backgroundPX']; }
		if (!empty($cl_options['cl_backgroundPY'])) { $body_styles[] = $cl_options['cl_backgroundPY']; }
		if (!empty($cl_options['cl_backgroundRepeat'])) { $body_styles[] = $cl_options['cl_backgroundRepeat']; }
	}
	
	if (count($body_styles)) {
?>
	html {
		background:<?php echo implode($body_styles, ' '); ?> !important;
	}
	body.login {
		background:transparent !important;
	}
<?php
	}

	// text colour
	if (!empty ($cl_options['cl_linkColor'])) {
?>
	.login #login a {
		color:#<?php echo $cl_options['cl_linkColor']; ?> !important;
	}
<?php
	}
	
	if (!empty ($cl_options['cl_colorShadow'])) {
?>
	#login #nav,
	#login #backtoblog {
		text-shadow:0 1px 4px #<?php echo $cl_options['cl_colorShadow']; ?>;
	}
<?php
	}
	
	echo '</style>';
}


/**
 * 
 */
function custom_login_url( $url ) {

    return get_home_url();
	
}


/**
 * 
 */
function custom_login_title ($title) {
	
	$cl_options = custom_login_get_options();
	
	if (!empty ($cl_options['cl_powerby'])) {
		$title = $cl_options['cl_powerby'];
	}
	
	return $title;
	
}


/**
 *
 * @param <type> $oldText
 * @return <type>
 */
function custom_login_admin_footer_text ($old_text) {

	$cl_options = custom_login_get_options ();

	if ( ! empty( $cl_options['cl_footertext'] ) )  {
		return $cl_options['cl_footertext'];
	}
	
	return $old_text;
	
} 


/**
 * 
 */
function custom_login_admin_add_page () {
	
	add_options_page ('BM Custom Login', 'Custom Login', 'manage_options', CL_PAGE, 'custom_login_options');
	
}


/**
 * 
 */
function custom_login_options () {
?>
	
<style>
	.wrap {
		position:relative;
	}
	
	.cl_notice {
		padding:10px 20px;
		-moz-border-radius:3px;
		-webkit-border-radius:3px;
		border-radius:3px;
		background:lightyellow;
		border:1px solid #e6db55;
		margin:10px 5px 10px 0;
	}
	
	.cl_notice h3 {
		margin-top:5px;
		padding-top:0;
	}
	
	.cl_notice li {
		list-style-type:disc;
		margin-left:20px;
	}
</style>

<div class="wrap">
	<div class="icon32" id="icon-options-general"><br /></div>
	<h2>Custom Login Options</h2>
	
	<form action="options.php" method="post">
<?php	
	settings_fields (CL_GROUP);
	do_settings_sections (CL_PAGE);
?>
		<p class="submit">
			<input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" class="button-primary" />	
		</p>
	</form>
	
	<div class="cl_notice">
		<h3>More WordPress Goodies &rsaquo;</h3>
		<p>If you like this plugin then you may also like my themes on <a href="http://prothemedesign.com" target="_blank">Pro Theme Design</a></p>
		<ul>
			<li><a href="http://twitter.com/prothemedesign">Pro Theme Design on Twitter</a></li>
			<li><a href="http://facebook.com/ProThemeDesign">Pro Theme Design on Facebook</a></li>
		</ul>
	</div>

</div>

<?php
}


/**
 * 
 */
function custom_login_init () {
	
	$vars = custom_login_get_options ();
		
	register_setting (CL_GROUP, CL_OPTIONS, 'custom_login_validate');
	add_settings_section (CL_SECTION, __('Login Screen Settings', CL_LOCAL), 'custom_login_section_validate', CL_PAGE);

	add_settings_field (
		'cl_background',
		__('Background Image Url:', CL_LOCAL),
		'form_text',
		CL_PAGE,
		CL_SECTION,
		array (
			'id' => 'cl_background',
			'value' => $vars,
			'default' => '',
			'description' => __('URL path to image to use for background (sized 312px wide, and around 600px tall so that it can be cropped). You can upload your image with the media uploader', CL_LOCAL),
		)
	);

	add_settings_field (
		'cl_powerby',
		__('Custom Login Powered by:', CL_LOCAL),
		'form_text',
		CL_PAGE,
		CL_SECTION,
		array (
			'id' => 'cl_powerby',
			'value' => $vars,
			'default' => '',
			'description' => '',
		)
	);

	add_settings_field (
		'cl_footertext',
		__('WordPress footer text:', CL_LOCAL),
		'form_text',
		CL_PAGE,
		CL_SECTION,
		array (
			'id' => 'cl_footertext',
			'value' => $vars,
			'default' => '',
			'description' => __('Appears at the bottom of the admin pages when logged in.', CL_LOCAL),
		)
	);

	add_settings_field (
		'cl_backgroundColor',
		__('Page Background Color:', CL_LOCAL),
		'form_text',
		CL_PAGE,
		CL_SECTION,
		array (
			'id' => 'cl_backgroundColor',
			'value' => $vars,
			'default' => 'eeeeee',
			'description' => __('6 digit hex color code', CL_LOCAL),
		)
	);
	
	add_settings_field (
		'cl_backgroundImage',
		__('Page Background Image:', CL_LOCAL),
		'form_text',
		CL_PAGE,
		CL_SECTION,
		array (
			'id' => 'cl_backgroundImage',
			'value' => $vars,
			'default' => '',
			'description' => __('Url path to image to use for the page background', CL_LOCAL),
		)
	);
	
	add_settings_field (
		'cl_backgroundPY',
		__('Page Background Vertical Position:', CL_LOCAL),
		'form_select',
		CL_PAGE,
		CL_SECTION,
		array (
			'id' => 'cl_backgroundPY',
			'value' => $vars,
			'default' => 'top',
			'options' => array ('top', 'center', 'bottom'),
			'description' => __('Vertical  position of background element', CL_LOCAL),
		)
	);

	add_settings_field (
		'cl_backgroundPX',
		__('Page Background Horizontal Position:', CL_LOCAL),
		'form_select',
		CL_PAGE,
		CL_SECTION,
		array (
			'id' => 'cl_backgroundPX',
			'value' => $vars,
			'default' => 'center',
			'options' => array ('left', 'center', 'right'),
			'description' => __('Horizontal position of background element', CL_LOCAL),
		)
	);
	
	add_settings_field (
		'cl_backgroundPRepeat',
		__('Page Background Repeat:', CL_LOCAL),
		'form_select',
		CL_PAGE,
		CL_SECTION,
		array (
			'id' => 'cl_backgroundRepeat',
			'value' => $vars,
			'default' => 'no-repeat',
			'options' => array ('no-repeat', 'repeat-x', 'repeat-y', 'repeat'),
			'description' => __('Background image repeat', CL_LOCAL),
		)
	);

	add_settings_field (
		'cl_color',
		__('Text Color:', CL_LOCAL),
		'form_text',
		CL_PAGE,
		CL_SECTION,
		array (
			'id' => 'cl_color',
			'value' => $vars,
			'default' => 'ffffff',
			'description' => __('6 digit hex color code', CL_LOCAL),
		)
	);

	add_settings_field (
		'cl_colorShadow',
		__('Text Shadow Color:', CL_LOCAL),
		'form_text',
		CL_PAGE,
		CL_SECTION,
		array (
			'id' => 'cl_colorShadow',
			'value' => $vars,
			'default' => '000',
			'description' => __('6 digit hex color code', CL_LOCAL),
		)
	);

	add_settings_field (
		'cl_linkColor',
		__('Text Link Color:', CL_LOCAL),
		'form_text',
		CL_PAGE,
		CL_SECTION,
		array (
			'id' => 'cl_linkColor',
			'value' => $vars,
			'default' => '21759B',
			'description' => __('6 digit hex color code', CL_LOCAL),
		)
	);

}


/**
 *
 * @param type $fields
 * @return type 
 */
function custom_login_validate ($fields) {

	// colour validation
	$fields['cl_color'] = str_replace ('#', '', $fields['cl_color']);
	$fields['cl_color'] = substr ($fields['cl_color'], 0, 6);

	// shadow colour validation
	$fields['cl_colorShadow'] = str_replace ('#', '', $fields['cl_colorShadow']);
	$fields['cl_colorShadow'] = substr ($fields['cl_colorShadow'], 0, 6);

	// background colour validation
	$fields['cl_backgroundColor'] = str_replace ('#', '', $fields['cl_backgroundColor']);
	$fields['cl_backgroundColor'] = substr ($fields['cl_backgroundColor'], 0, 6);
	
	// colour validation
	$fields['cl_linkColor'] = str_replace ('#', '', $fields['cl_linkColor']);
	$fields['cl_linkColor'] = substr ($fields['cl_linkColor'], 0, 6);
	
	// clean image urls
	$fields['cl_background'] = esc_url_raw ($fields['cl_background']);
	$fields['cl_backgroundImage'] = esc_url_raw ($fields['cl_backgroundImage']);
	
	// sanitize powered by message
	$fields['cl_powerby'] = esc_html ($fields['cl_powerby']);
	$fields['cl_powerby'] = strip_tags ($fields['cl_powerby']);
		
	return $fields;
	
}


/**
 *
 * @param type $fields
 * @return type 
 */
function custom_login_section_validate ($fields) {
	
	return $fields;
	
}


/**
 *
 * @param type $args 
 */
function form_text ($args) {
	
	// defaults
	$id = '';
	$value = '';
	$description = '';
	
	// set values
	if (!empty ($args['value'][$args['id']])) {
		$value = $args['value'][$args['id']];
	} else {
		if (!empty ($args['default'])) {
			$value = $args['default'];				
		}
	}
		
	if (!empty ($args['description'])) {
		$description = $args['description'];
	}
	
	$id = $args['id'];
?>
	<input type="text" id="<?php echo $id; ?>" name="<?php echo CL_OPTIONS; ?>[<?php echo $id; ?>]" value="<?php echo $value; ?>" class="regular-text"/>
<?php
	if (!empty ($description)) {
		echo '<br /><span class="description">' . $description . '</span>';
	}
	
}


/**
 *
 * @param type $args 
 */
function form_select ($args) {
	
	// defaults
	$id = '';
	$value = '';
	$options = array();
	$description = '';
	
	if (!empty($args['options'])) {
		$options = $args['options'];
	}
	
	if (!empty($args['value'][$args['id']])) {
		$value = $args['value'][$args['id']];
	} else {
		if (!empty ($args['default'])) {
			$value = $args['default'];				
		}
	}
	
	if (!empty ($args['description'])) {
		$description = $args['description'];
	}
	
	$id = $args['id'];
	
	// display select box options list
	if ($options) {
		echo '<select id="' . $id . '" name="' . CL_OPTIONS . '[' . $id . ']">';
		foreach ($options as $o) {
			$selected = '';
			if ($o == $value) {
				$selected = ' selected="selected" ';
			}
			echo '<option value="' . $o . '" ' . $selected . '>' . $o . '</option>';
		}
		echo '</select>';		
	}
	
	if (!empty ($description)) {
		echo '<br /><span class="description">' . $description . '</span>';
	}
	
}


add_action( 'admin_init', 'custom_login_init' );
add_action( 'admin_menu', 'custom_login_admin_add_page' );
add_action( 'login_head', 'bm_custom_login' );
add_filter( 'login_headerurl', 'custom_login_url' );
add_filter( 'login_headertitle', 'custom_login_title' );
add_filter( 'admin_footer_text', 'custom_login_admin_footer_text' );