Web development

debug which wordpress plugin is slower

Sunday, August 21st, 2011 | Web development | Comments Off

Once I had a performance problem with wordpress website even if it run on a dedicated server.

I suspected that the problem is one of the plugins but I had at least 50 installed :) .
In order to see who’s the slowest I opened wp-includes/plugin.php and temprary changed the do_action function to something like this:

function do_action($tag, $arg = '') {
	global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
 
	if ( ! isset($wp_actions) )
		$wp_actions = array();
 
	if ( ! isset($wp_actions[$tag]) )
		$wp_actions[$tag] = 1;
	else
		++$wp_actions[$tag];
 
	$wp_current_filter[] = $tag;
 
	// Do 'all' actions first
	if ( isset($wp_filter['all']) ) {
		$all_args = func_get_args();
		_wp_call_all_hook($all_args);
	}
 
	if ( !isset($wp_filter[$tag]) ) {
		array_pop($wp_current_filter);
		return;
	}
 
	$args = array();
	if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
		$args[] =& $arg[0];
	else
		$args[] = $arg;
	for ( $a = 2; $a < func_num_args(); $a++ )
		$args[] = func_get_arg($a);
 
	// Sort
	if ( !isset( $merged_filters[ $tag ] ) ) {
		ksort($wp_filter[$tag]);
		$merged_filters[ $tag ] = true;
	}
 
	reset( $wp_filter[ $tag ] );
 
	do {
		foreach ( (array) current($wp_filter[$tag]) as $the_ ) {
 
			if ( !is_null($the_['function']) ) {
                if($tag=='init') $time_start = microtime(true);
				call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
                if($tag=='init') {
                    $time_end = microtime(true);$time = $time_end - $time_start;
                    echo print_r($the_['function']).' -----  <b>'.$time."</b><br>\n";
                }                
            }
        }
 
	} while ( next($wp_filter[$tag]) !== false );
 
	array_pop($wp_current_filter);
}

after this you’ll be able to see what plugin is eating the most of your time (after each function call you’ll see execution time in bold)

It’s not a mature script just a quick snippet to have a starting point for someone who runs into the same problem.

CSS transparent border hack, easy

Wednesday, May 18th, 2011 | Web development | Comments Off

Recently I wanted to have different elements surrounded by transparent borders.
For instance an ajax popup, similar with what facebook has, or a design element to look nicer.

The solution is having another div that is inside the one you already have with an opacity applyed to it.
The HTML:

<div class="normalDiv">
  <div class="transparentBorder">&nbsp;</div>
  our content here,
</div>

The CSS:

.transparentBorder{border: 10px solid #363636;
    position: absolute; top: -10px; left: -10px; height: 100%; width: 100%;
    opacity: 0.34;filter:alpha(opacity=34);
    z-index: -10;
}
.normalDiv {position: relative; width: 500px; height: 300px; text-align: center; font-size: 13px; color; #363636;}

See the DEMO for transparent borders

clear space after floating divs – includes opera fix

Thursday, February 10th, 2011 | Web development | Comments Off

When writing html code to draw columns you usually use float elements contained inside a div container like here:

<div class="clearfix" style="width: 100%;">
<div style="width: 30%; float: left;">left content</div>
<div style="width: 60%; float: right;">right content</div>
</div>

the problem occurs when bellow this code you want to have a footer for instance. To work in all the browser most of front end developers will clear the space like this:

<div style="clear:both;font-size:1px;">&nbsp;</div>

a better solution is to just use css and don’t worry about additional ugly and useless html code :)
just add the following code inside your stylesheet:

 
.clearfix:after {
    content: '.';
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0px;
    height: 0px;
}
 
 
.clearfix {
    display: inline-block; 
}
 
html[xmlns] .clearfix {
    display: block;
}
 
* html .clearfix {
    height: 1%;
}
.clearfix { display:block; }

last line is for opera – don’t know why it doesn’t like this clearfix so I made it to like it ;)

Removing css outline from buttons and html elements

Tuesday, February 8th, 2011 | Web development | Comments Off

When designing, one of the first things I do is to define some very common elements with general features like no margin/padding for body.
Similar with the body element I don’t like the outline that appears when clicking a button or a li element from a list.
To do this I write something like this:

*{outline:none;}

however… on firefox the outline still appers on buttons and submit buttons. To fix this just add:

input::-moz-focus-inner, button::-moz-focus-inner { border: 0; }

Extract modified files from GIT

Wednesday, January 26th, 2011 | Web development | Comments Off

Recently I stumbled upon a great script for extracting modified files between two GIT commits.
This is great for website deployments assuming on client side only ftp is accepted.

#!/bin/bash

BOLD=”\033[1m"
_BOLD="\033[22m"
RED="\033[31m"
YELLOW="\033[33m"
GREEN="\033[32m"
RESET="\033[39m"

range=$1
if [ -z "$range" ]; then
echo -e “${BOLD}${RED}You must specify a ‘..‘ argument.${RESET}${_BOLD}”
exit 1
fi

if [ -z "$2" ]; then
target=”$( pwd )/.deployments”
else
target=$( echo “$2″ | sed -e ‘s#/\+$##g’ )
fi

if [ -d "$target" ]; then
echo -ne “Do you wish to remove ‘$target’ first? [Y/n] ”
read prompt
if [ -z "$prompt" ] || [ "$prompt" == "Y" ] || [ "$prompt" == "y" ]; then
echo -e ” ${YELLOW}Purging ‘$target’…${RESET}”
if [ -d "$target" ]; then
rm -Rf “$target”
fi
echo -e ” ${GREEN}Done.${RESET}”
fi
fi

mkdir -p “$target”

LOG=$( git whatchanged -m –oneline “$range” | awk ‘{
if ($1 ~ /^:/) {
print $5 “:” $6
}
}’ | tac )

length=$( echo “$LOG” | wc -l )
manual=”

index=0
for command in $LOG; do
operation=${command:0:1}
filepath=${command:2}
case $operation in
“A” | “M”)
if [ -f "$filepath" ]; then
destination=$( dirname “$target/$filepath” )
filename=$( basename “$filepath” )
mkdir -p “$destination”
cp -f “$filepath” “$target/$filepath”
fi
;;
“D”)
manual=”$manual\n$filepath”
;;
*)
echo -e “${BOLD}${RED}Unknown operation $operation on file $filepath.${RESET}${_BOLD}”
exit 4
;;
esac
let “index++”
echo -ne “\r${YELLOW}Processing ${length} files…${RESET} $( echo “scale=2; ( $index / $length ) * 100.00″ | bc )%”
done
echo

if [ ! -z "$manual" ]; then
echo $( echo -e “$manual” | sed -e ‘s/^\s\+//g’ | sort -u ) > “$target/.delete”
echo -e “[WARN] Please manually delete the files listed in ‘${BOLD}.delete${_BOLD}’”
fi

echo -e “${GREEN}Done.${RESET}”

save it as git-extract and use it as:

git-extract from_old_commit_hash..to_new_commit_hash

All the credits go to: http://blog.angeloff.name/post/1490030701/extract-changed-files-from-git-and-prepare-a-deployment

mysql IN function alternative when using mysql field

Monday, December 13th, 2010 | Web development | Comments Off

let’s assume you have a mysql field which is composed from comma separated values

table name: products
field name from table products: category_ids (the comma separated list of categories)

let’s assume that we want to search all products for a certain category. the first idea will be to use the IN mysql function: (I will note {$var} as an external variable)

SELECT * FROM products WHERE {$category_id} IN category_ids

however it won’t work so this statement should be used:
SELECT * FROM products WHERE category_ids REGEXP ‘^{$category_id},|,{$category_id},|,{$category_id}$|^{$category_id}$’

Magento problems: General error: 1005

Friday, November 26th, 2010 | Web development | Comments Off

The exact error I kept getting was:
exception ‘Zend_Db_Statement_Exception’ with message ‘SQLSTATE[HY000]: General error: 1005 Can’t create table ‘xxx catalog_category_flat_store_1.frm’ (errno: 150)’ in xxx/lib/Zend/Db/Statement/Pdo.php:234

The solution is to execute the following statements inside phpMyAdmin:
ALTER TABLE catalog_category_entity ENGINE=INNODB
ALTER TABLE core_store ENGINE=INNODB

Don’t forget the table prefixes if you have!

If you get a similar error but another table please note that this error is related to foreign indexes and occurs most offten when you move the magento database from a server to another.

I am taking the above example, the table catalog_category_flat_store_1. This table has 2 foreign keys one to catalog_category_entity and the other .. you guessed: core_store.

that’s why I executed:
ALTER TABLE catalog_category_entity ENGINE=INNODB
ALTER TABLE core_store ENGINE=INNODB

Project Management Open Source Software

Thursday, April 22nd, 2010 | Different Thoughts, Web development | Comments Off

There is almost an year since working with qdpm, http://qdpm.net/, which is a great tool for project management, however I wanted something more complete and if possible that had svn support for the projects.

update: qdpm supports gantt charts from version 5.5

Here is what I found:

1. Clockingi.com, http://www.clockingit.com/  has nice graphical reports, gantt charts and svn support

2. Codeni, http://www.codendi.com from XEROX, supports svn/cvs repository browsing and gantt charts, seems a complex and mature product

3. Collabtive, http://collabtive.o-dyn.de/, it’s a simple, lightweight project management too, useful for a very small team or a freelancer

4. project-open, http://www.project-open.org/

5. endeavour, http://endeavour-mgmt.sourceforge.net, you can check it’s features here: http://endeavour-mgmt.sourceforge.net/features1.html

6. Redmine, http://www.redmine.org/, gantt charts, integrations with SVN, CVS, Mercurial, etc.. written in Ruby on Rails

Tags:

Jquery Regex Validation

Wednesday, February 17th, 2010 | Web development | Comments Off

A quick post about validating anything you want using regex. I used it in a project that created forms on the fly, so validations like email, numbers and so on wasn’t so simple (I couldn’t do it by just adding the validation as a class: class=”email”)

so first step is adding what we need before the </head>:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.validate.pack.js">

now just after the above code we add our regex method:

<script type="text/javascript">
$.validator.addMethod(
 "regex",
 function(value, element, regexp) {
 var check = false;
 var re = new RegExp(regexp);
 return this.optional(element) || re.test(value);
 },
 "Please check your input"
);
</script>

and the final thing is ading the validation we need – you can use php to add them from the database for instance:

<script type="text/javascript">
$(document).ready(function(){
$("#myFieldId").rules("add", {regex: "^(0|[1-9][0-9]*|[1-9][0-9]{0,2}(,[0-9]{3,3})*)$"});
})
</script>

Overlength date field error in prestashop

Saturday, December 12th, 2009 | Web development | Comments Off

Recently I got this error when trying to send emails in prestashop.
An email was bouncing back and it contained the following message:
This message has been rejected because it has
an overlength date field which can be used
to subvert Microsoft mail programs

After a lot of headaches and a cofee :) I manage to detect what was wrong:
Presta uses in the email class a variable $templateVars
this was like:

$templateVars = array(
‘{date_facturare}’ =>
‘Nume Firma: ‘.$invoice->company.’<br>’.
‘Cod fiscal: ‘.$invoice->tax_code.’<br>’.
‘Registrul Comertului: ‘.$invoice->reg_data.’<br>’.
‘IBAN: ‘.$customer->iban.’<br>’,
‘CNP: ‘.$invoice->cnp.’<br>’,
‘{firstname}’ => $customer->firstname,
‘{lastname}’ => $customer->lastname, …

However the problem was because of a , instead of .
'IBAN: '.$customer->iban.'<br>',
should have been:
'IBAN: '.$customer->iban.'<br>'.

Because of the wrong “,”, $templateVars[0] was taking a long value and apparently Swift Mailer was appending it to the header date of the email hence the error with the overlength date field.

Tags: ,