[General] | [PHP Tag] | [Indenting] | [Filenames] | [Classes] | [Functions] | [Vars] | [Constants] |
[Switch] | [If Else] | [Long if's] | [Foreach, For, While] | [Try, Catch] | [Function Declaration] | [Function Calls] | [Arrays] |
If you decide to write code for Oxwall regardless if it is for the software package core itself or as a plugin developer: Make sure you observe code style standards described in this policy. Some policies apply to core coding, some apply to plugin coding and some apply to both. We will specify how the rule applies in each rule that follows.
You don't have to make your code look fancy to write a great plugin. However, think how others looking at your code would appreciate if it's written like the rest of the Oxwall code. All rules in this policy are not because it is just our preference. There is a reason behind each policy with regards to security, readability, portability, compatibility, fail-safety and general acceptability in mind.
New policies have been marked with [New]
$config = OW::getConfig();
if( !$config->configExists('plugin_name_goes_here', 'config_name_goes_here') )
{
$config->addConfig('plugin_name_goes_here', 'config_name_goes_here', 'initial_value_goes_here');
}
[New] Using Smarty Functions: Since it is known that the > (greater than sign) could break
smartys xml parser, when coding html for smarty in your html pages please try to use the smarty functions (gt, eq, lt) greater than, equalto,
less than.
//the true option is if you want to allow this option for guests, for false ignore the option (leave it out)
$authorization = OW::getAuthorization();
$groupName = 'plugin_name_goes_here';
$authorization->addGroup($groupName);
$authorization->addAction($groupName, 'add_plugin_name_goes_here',true);
$authorization->addAction($groupName, 'view_plugin_name_goes_here',true);
$authorization->addAction($groupName, 'add_comment');
$authorization->addAction($groupName, 'delete_comment_byowner');
//here is where you add the language labels you want to use for your user role options
//role auth
function pluginname_add_auth_labels(BASE_CLASS_EventCollector $event)
{
$language = OW::getLanguage();
$event->add(array(
'pluginname' =>array(
'label' => $language->text('pluginname', 'auth_group_label'),
'actions' =>array(
'add_pluginname' => $language->text('pluginname', 'auth_action_label_add'),
'view_pluginname' => $language->text('pluginname', 'auth_action_label_view'),
'add_comment' => $language->text('pluginname', 'auth_action_label_add_comment'),
'delete_comment_byowner' => $language->text('pluginname', 'auth_action_label_delete_comment_byowner')
)
)
)
);
}//close function
OW::getEventManager()->bind('admin.add_auth_labels', 'pluginname_add_auth_labels');
[New] Internal Documentation: We ask that you add internal documentation text to your code when appropriate. It is not required
that you document every line of code, but enough so someone can follow your logic. All internal docuemntation should be in plain english. Having some internal documentation of what
is happening in the code speeds up approval time as well as other benefits. So please document when you can.
//Right way
If($donuts == $sweet)
{
if($value == $value)
{
$value1 = $value;
}else{
$value1 = '';
}//close else value
}//close if
//Wrong way
If($donuts == $sweet){ if($value == $value) { $value1 = $value; }else{ $value1 = ''; }//close if value }//close if
All coding should follow this tiering structure below: (unless doing so will deform the parsed output - example: using pre tag).
-
-
-
-
-
-
-
//init variables
if(!isset($value)) { $value = ''; }
if(!isset($value1)) { $value1 = ''; }
if(!isset($value2)) { $value2 = 0; }
... and so on
//init arrays
$array1 = array();
$array2 = array();
.... and so on
Remember that since php 5.4 php has allows shorthand arrays: (visit Arrays section in this document)
//init arrays
$array1 = [];
$array2 = [];
.... and so on
if(class_exists('PRIVACY_BOL_ActionService'))
{
//do something here
}else{
//must show error message on screen
}
Check it by check for plugin active
if(OW::getPluginManager()->isPluginActive('privacy'))
{
//do something here
}else{
//must show error on the screen
}
Always use full-form of PHP code tags when opening and closeing php: Short tag <? ?> is NOT allowed.
<?php //php open tag ?> //php close must be ommitted if file contains ONLY php code
The exception to this policy is if you are doing something like an inline variable assignment, then you can use the <?= ?> (notice the = sign) short tag like in this example where we are building a url:
<a href="example.com?value1=<?= $value1;?>">press here</a>
Supporting Information: The short tag <? has been depricated due to requiring the php.ini flag and will be removed as of php 8.0. However, the <?= short tag with equal sign is unaffected because as of PHP 5.4 it is always available.
include_once and require_once are statements, not functions. Parentheses should not surround the subject filename.
//RIGHT require_once 'header.php'; //WRONG require_once('header.php');
Use 4 spaces instead of tabs for an indent.
view.php base_dao.php my_super_class.php
class MySuperClass { //code here } class PREFIX_MySuperClass { //code here }
function connect() function camelCaseFunction() function fooBar()global functions
function my_global_function()
public $myVar; private $hisVar; protected $x;
define("MY_MEGA_CONSTANT","Hello world");
foreach, for, while, if, switch, try, catch etc.
There should be one space between the control keyword and opening parenthesis in control statements, which will distinguish them from function calls.
Use curly braces even in case they are optional, as this will make the code more readable and help to avoid logic errors appearing when new lines are added.
You can use either break or return in switch code. We would prefer you use break so we have standards. Howerver, if you must use return rather than break then just add comment to let us know why. Place the break or return vertically under the case item.
switch ( condition ) { case 1:action1(); break; case 2: action2(); break; default:defaultAction(); break; }
or using return
switch ( condition ) { case 1:action1(); return; case 2: action2(); return; default:defaultAction(); return; }
Split up if and if else statements onto several lines
if ( condition1 ) { //code here }
if ( condition1 ) { //code here }else{ //code here }
Split long if statements onto several lines
if ( condition1 || condition2 && condition3 ) { //code here }
foreach ( $a as $v ) { echo $v; }
try { //code here } catch ( Exception $e ) { //code here }
All function names must be in a camelCase. Global functions are an exception. They should consist of words in lowercase and underscores.
There should be whitespaces before and after parameter list.
There should be no space between a function name and an opening parenthesis.
There should be a new line before return statement.
function fooBar( $param1, $param2 ) { if ( $param1 !=== $param2 ) { //code here } return true; }
You should always declare a type of parameter when possible:
function doSomethingGood( MyClass $obj ) { //code here }
Global function example:
function print_var( $var, $echo = false ) { //code here }
myCoolFunction(1, 2, 3); $this->myCoolMethod(1, 2, 3);
$arr = [1, 2, 'no', 'pain', 'no', 'gain' ]; $longArr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
$assoc = [ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' ];
You can place the closing parenthesis on the same line or the following line. If you use the following line please line it up with the open parenthesis. Like so:
$assoc = [ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' ];© 2020 Oxwall.com All rights reserved