CodingStandards
Ajax
Best Practices
- Every ajax call SHOULD use nonce
- Every restricted ajax call MUST check for user capabilities
- Action SHOULD NOT be defined as "nopriv".
- Action names MUST start with "appq_" prefix.
- $_POST data MUST be sanitized for the correct input type.
- Queries usin user input data (e.g. $_POST data, profile fields) MUST be prepared
- Ajax functions MUST have at least one
wp_send_json_success([$optional_data]); or wp_send_json_error([$optional_data]);
Commenti
Cosa commentare
- The top of any program file. This is called the "Header Comment". It should include all the defining information about who wrote the code, and why, and when, and what it should do.
- Above every function/method. This is called the function/method header and provides information about the purpose of this "sub-component" of the program. When and if there is only one function in a file, the function header and file header comments should be merged into a single comment.
- In line Any "tricky" code where it is not immediately obvious what you are trying to accomplish, should have comments right above it or on the same line with it.
File header
/*
* @Author: `{NAME}` `{SURNAME}` <`{USERNAME}`>
* @Date: `{CREATION DATE}`
* @Filename: `{FILENAME}`
* @Last modified by: `{USERNAME}`
* @Last modified time: `{LAST EDIT DATE}`
*/
Method header
/**
* `{BRIEF DESCRIPTION OF METHOD}`
*
* @method `{METHOD NAME}`
* @params `{TYPE}` `{PARAMETER NAME}` `{PARAMETER DESCRIPTION}`
* @date: `{CREATION DATE}`
* @author: `{NAME}` `{SURNAME}` <`{USERNAME}`>
*/
Function header
/**
* `{BRIEF DESCRIPTION OF FUNCTION}`
*
* @params `{TYPE}` `{PARAMETER NAME}` `{PARAMETER DESCRIPTION}`
*
* @return `{TYPE}` `{RETURN VALUE DESCRIPTION}`
*/
Indentazione
If then else
Corretto
if ($cond)
{
echo "x";
} else {
echo "y";
}
Non corretto
if ($cond){
echo "x";
} else
{
echo "y";
}
if ($cond)
{
echo "x";
} else {
echo "y";
}
Parameter with lambda functions
Corretto
array_map(
function ($item)
{
return $item->display_name;
},
$ass_tags
);
Non Corretto
array_map(
function ($item)
{
return $item->display_name;
},
$ass_tags
);
Mixed PHP and HTML
Corretto
<?php if ($cond) : ?>
``
<?php endif ?>
Non corretto
<?php if ($cond)
{ ?>
``
<?php } ?>
<?php
if ($cond)
{
?>
``
<?php
}
?>
<?php if ($cond)
{ ?>
``
<?php
} ?>