scarstens
3/21/2012 - 7:22 PM

QR Code Generator - Part of Whitelabel Framework

QR Code Generator - Part of Whitelabel Framework

<?php
//uses the same arguments as WordPress "checked()" function
//but adds the argument submitted and "default"to allow you 
//to set the default checked value of the checkbox
function wlwp_checked($checkboxPostedValue, $checkboxDefaultValue = 'on', $echo = false, $requiredField = NULL, $default = false) {
	if(empty($requiredField) || (isset($_REQUEST[$requiredField]) && !empty($_REQUEST[$requiredField])) ) {
		return checked($checkboxPostedValue, $checkboxDefaultValue, $echo);
	}
	//if a required field is set, and the required field has not been submitted
	//then page is loading for the first time and needs to load default value (whole point of the function)
	elseif($default) { 
		if($echo) echo 'checked="checked"';
		return 'checked="checked"'; 
	}
	else { global $errors; $errors['wlwp_checked'] = 'wlwp_checked() function failed'; }
}

function qr_code_builder($atts, $content = null) {
        if(isset($_REQUEST['qrSubmission']) && !empty($_REQUEST['qrSubmission'])) {
			if(!isset($_REQUEST['qrTrackingParam']) || $_REQUEST['qrTrackingParam'] != 'on') $qr_param = '';
			elseif(stristr($_REQUEST['qrSubmission'], '?')) $qr_param = '&qr=yes';
			else $qr_param = '?qr=yes';
			$qr_codes = '<p>'.esc_url($_REQUEST['qrSubmission']).
				$qr_param.'<br /><img src="http://api.qrserver.com/v1/create-qr-code/?size=1000x1000&data='.
				urlencode(esc_url($_REQUEST['qrSubmission'].$qr_param)).'"></p>';
		}
		else {
			unset($_REQUEST['qrSubmission']);
			$qr_codes = '<p><br /><img src="'.get_stylesheet_directory_uri().'/images/myanthem/create-qr-code-sample.png"></p>';
			$qr_code_form_defaults['tracking'] = 'checked="checked"';
		}
			$qr_code_form_EOL = '<br />';
			$qr_code_form_EOL = apply_filters('wlwp_qr_code_form_line_break', $qr_code_form_EOL);
			$qr_code_form = 
				'<form id="qr-code-form" method="post">'.
					'<label for="qrSubmission"> URL: <input type="text" id="qrSubmission" name="qrSubmission" value="'.$_REQUEST['qrSubmission'].'" /></label>'.$qr_code_form_EOL.
					'<label for="qrTrackingParam">(default checked) Tracking Parameter: <input type="checkbox" '.checked($_REQUEST['qrOptionsTrackingA'], 'on', false).' id="qrTrackingParam" name="qrOptionsTrackingA" /></label>'.$qr_code_form_EOL.
					'<label for="qrTrackingParam">(custom checked) Tracking Parameter: <input type="checkbox" '.wlwp_checked($_REQUEST['qrOptionsTrackingB'], 'on', false).' id="qrTrackingParam" name="qrOptionsTrackingB" /></label>'.$qr_code_form_EOL.
					'<label for="qrTrackingParam">(custom checked->on) Tracking Parameter: <input type="checkbox" '.wlwp_checked($_REQUEST['qrOptionsTrackingC'], 'on', false, 'qrSubmission', true).' id="qrTrackingParam" name="qrOptionsTrackingC" /></label>'.$qr_code_form_EOL.
					'<input type="submit" value="Build QR Code" />'.
				'</form>';
			return $qr_code_form.$qr_codes;
}
add_shortcode("qr-code-builder", "qr_code_builder");
?>