Get list of all registered scripts and styles in WP

Another one for the future references.

global $enqueued_scripts;
global $enqueued_styles;

add_action( 'wp_print_scripts', 'cyb_list_scripts' );
function cyb_list_scripts() {
    global $wp_scripts;
    global $enqueued_scripts;
    $enqueued_scripts = array();
    foreach( $wp_scripts->queue as $handle ) {
        $enqueued_scripts[] = $wp_scripts->registered[$handle]->src;
    }
}

add_action( 'wp_print_styles', 'cyb_list_styles' );
function cyb_list_styles() {
    global $wp_styles;
    global $enqueued_styles;
    $enqueued_styles = array();
    foreach( $wp_styles->queue as $handle ) {
        $enqueued_styles[] = $wp_styles->registered[$handle]->src;
    }
}

Print somewhere:

    global $enqueued_scripts;
    var_dump( $enqueued_scripts );
    global $enqueued_styles;
    var_dump( $enqueued_styles );

Thanks to @cybmeta at stack overflow.

in_associative_array PHP function

Pretty straightforward function for determining if associative array already contains value in specified array column.

function in_associative_array($needle, $haystack, $column, $nivo = 1)
{
	/*
		$haystack = array(
			[0] = array(
				"column1" => "value1",
				"column2" => "value2"
			),
			[1] = array(
				"column1" => "value1",
				"column2" => "value2"
			)	
		)	
	*/
	
	if ($nivo == 1)
	foreach($haystack as $item)
	{
		if (isset($item[ $column ]) && $item[ $column ] == $needle)
			return true;
	}
	
	/*
		$haystack = array(
			"column1" => "value1",
			"column2" => "value2",
			"column3" => "value1",
		)	
	*/	
	if ($nivo == 0)
	foreach($haystack as $key => $item)
	{
		if ($key == $column && $item == $needle)
			return true;
	}	
		
	
	return false;
}

Prettier, customizable print_r function

When developing random stuff with PHP I tend to rely heavily on various variable printing so I made a small function that I use to print variables the way I want to.

<?php
	function print_pre($text, $ret = false, $inStyles = array()) {
	$aStyles = array("-webkit-border-radius" => "3px", 
		"-moz-border-radius" => "3px", 
		"border-radius" => "3px",
		"background-color" => "#F0F0F0", 
		"padding" => "3px",
		"border" => "1px solid #A8A8A8");
	
	if (is_array($inStyles))
	{
		foreach($inStyles as $key => $value)
			$aStyles[$key] = $value;
	}
		
	$applyStyles = array();	
	foreach($aStyles as $key => $value)
		array_push($applyStyles, $key . ": " . $value . ";");

	$return = "<pre style='".implode(" ", $applyStyles)."'>" . print_r($text, true) . "</pre>";
	if($ret) return $return;
	print($return);
}
?>

It’s pretty straightforward – it takes 3 arguments, 1st being variable we want to display, 2nd if we want the return string returned or directly printed on page and finally CSS styling pairs as associative array.

Few examples of use

Default print

print_pre("Test string", true);

Result:

Test string

Custom style

print_pre("Test string", false, array("background-color" => "#000", "color" => "#fff"));

Result:

Test string

Printing an object

$array = array("name" => "david", "age" => (date("Y") - 1987), "gender" => "male");
$object = json_decode(json_encode($array), FALSE);
print_pre($object, false, array("background-color" => "blue", "color" => "#fff"));

Result:

stdClass Object
(
    [name] => david
    [age] => 30
    [gender] => male
)

Getting the return print string of regular array and echoing it as a variable

$array = array("name" => "david", "age" => (date("Y") - 1987), "gender" => "male");
$value = print_pre($array, true, array("background-color" => "yellow", "color" => "gray"));
echo $value;

Result:

Array
(
    [name] => david
    [age] => 30
    [gender] => male
)

PHPExcel lock cells

I’ve been using wonderful PHPExcel library for exporting my mysql data to excel via PHP and encountered a little setback trying to lock particular cells to prevent my client from editing values I’d need to process when uploading the file back to the system so I tried the most straightforward method I found in the official documentation but nothing seemed to work until I came across this solution:

$obj_Sheet->getProtection()->setPassword('pass_to_remove_protection');
$obj_Sheet->getProtection()->setSheet(true);
$obj_Sheet->getStyle('B2:J5')->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);

First I set the password to remove protection (‘pass_to_remove_protection’) that anyone can use to remove protection (as long as they know the password), then I turn on sheet protection and finally set all cells in a range from B2 to J5 to unprotected. This basically locked EVERYTHING on selected sheet and unlocked the fields I specified as unlocked.