Sunday, April 26, 2015

How to implement numeric pagination in wordpress

http://www.wpbeginner.com/wp-themes/how-to-add-numeric-pagination-in-your-wordpress-theme/

- Create following function in function.php and include in loop.php of blog post list page.

function wpbeginner_numeric_posts_nav() {

 if( is_singular() )
  return;

 global $wp_query;

 /** Stop execution if there's only 1 page */
 if( $wp_query->max_num_pages <= 1 )
  return;

 $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
 $max   = intval( $wp_query->max_num_pages );

 /** Add current page to the array */
 if ( $paged >= 1 )
  $links[] = $paged;

 /** Add the pages around the current page to the array */
 if ( $paged >= 3 ) {
  $links[] = $paged - 1;
  $links[] = $paged - 2;
 }

 if ( ( $paged + 2 ) <= $max ) {
  $links[] = $paged + 2;
  $links[] = $paged + 1;
 }

 echo '<div class="navigation"><ul>' . "\n";

 /** Previous Post Link */
 if ( get_previous_posts_link() )
  printf( '<li>%s</li>' . "\n", get_previous_posts_link() );

 /** Link to first page, plus ellipses if necessary */
 if ( ! in_array( 1, $links ) ) {
  $class = 1 == $paged ? ' class="active"' : '';

  printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

  if ( ! in_array( 2, $links ) )
   echo '<li>…</li>';
 }

 /** Link to current page, plus 2 pages in either direction if necessary */
 sort( $links );
 foreach ( (array) $links as $link ) {
  $class = $paged == $link ? ' class="active"' : '';
  printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
 }

 /** Link to last page, plus ellipses if necessary */
 if ( ! in_array( $max, $links ) ) {
  if ( ! in_array( $max - 1, $links ) )
   echo '<li>…</li>' . "\n";

  $class = $paged == $max ? ' class="active"' : '';
  printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
 }

 /** Next Post Link */
 if ( get_next_posts_link() )
  printf( '<li>%s</li>' . "\n", get_next_posts_link() );

 echo '</ul></div>' . "\n";

}

Wednesday, April 8, 2015

Google style guide line for JS


source: http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml

Google JavaScript Style Guide

Revision 2.93
Aaron Whyte
Bob Jervis
Dan Pupius
Erik Arvidsson
Fritz Schneider
Robby Walker
Each style point has a summary for which additional information is available by toggling the accompanying arrow button that looks this way: . You may toggle all summaries with the big arrow button:
 Toggle all summaries

Important Note

Displaying Hidden Details in this Guide

This style guide contains many details that are initially hidden from view. They are marked by the triangle icon, which you see here on your left. Click it now. You should see "Hooray" appear below.

Background

JavaScript is the main client-side scripting language used by many of Google's open-source projects. This style guide is a list of dos and don'ts for JavaScript programs.

JavaScript Language Rules

var

link
Declarations with var: Always
Decision:When you fail to specify var, the variable gets placed in the global context, potentially clobbering existing values. Also, if there's no declaration, it's hard to tell in what scope a variable lives (e.g., it could be in the Document or Window just as easily as in the local scope). So always declare with var.

Constants

link
  • Use NAMES_LIKE_THIS for constant values.
  • Use @const to indicate a constant (non-overwritable) pointer (a variable or property).
  • Never use the const keyword as it's not supported in Internet Explorer.
Decision:
Constant values
If a value is intended to be constant and immutable, it should be given a name in CONSTANT_VALUE_CASEALL_CAPS additionally implies @const (that the value is not overwritable).
Primitive types (numberstringboolean) are constant values.
Objects' immutability is more subjective — objects should be considered immutable only if they do not demonstrate observable state change. This is not enforced by the compiler.

Constant pointers (variables and properties)
The @const annotation on a variable or property implies that it is not overwritable. This is enforced by the compiler at build time. This behavior is consistent with the constkeyword (which we do not use due to the lack of support in Internet Explorer).
@const annotation on a method additionally implies that the method cannot not be overridden in subclasses.
@const annotation on a constructor implies the class cannot be subclassed (akin to final in Java).

Examples
Note that @const does not necessarily imply CONSTANT_VALUES_CASE. However, CONSTANT_VALUES_CASE does imply @const.
/**
 * Request timeout in milliseconds.
 * @type {number}
 */
goog.example.TIMEOUT_IN_MILLISECONDS = 60;
The number of seconds in a minute never changes. It is a constant value. ALL_CAPS also implies @const, so the constant cannot be overwritten.
The open source compiler will allow the symbol to be overwritten because the constant is not marked as @const.
/**
 * Map of URL to response string.
 * @const
 */
MyClass.fetchedUrlCache_ = new goog.structs.Map();
/**
 * Class that cannot be subclassed.
 * @const
 * @constructor
 */
sloth.MyFinalClass = function() {};
In this case, the pointer can never be overwritten, but value is highly mutable and not constant (and thus in camelCase, not ALL_CAPS).

Semicolons

link
Always use semicolons.
Relying on implicit insertion can cause subtle, hard to debug problems. Don't do it. You're better than that.
There are a couple places where missing semicolons are particularly dangerous:
// 1.
MyClass.prototype.myMethod = function() {
  return 42;
}  // No semicolon here.

(function() {
  // Some initialization code wrapped in a function to create a scope for locals.
})();


var x = {
  'i': 1,
  'j': 2
}  // No semicolon here.

// 2.  Trying to do one thing on Internet Explorer and another on Firefox.
// I know you'd never write code like this, but throw me a bone.
[ffVersion, ieVersion][isIE]();


var THINGS_TO_EAT = [apples, oysters, sprayOnCheese]  // No semicolon here.

// 3. conditional execution a la bash
-1 == resultOfOperation() || die();
So what happens?
  1. JavaScript error - first the function returning 42 is called with the second function as a parameter, then the number 42 is "called" resulting in an error.
  2. You will most likely get a 'no such property in undefined' error at runtime as it tries to call x[ffVersion, ieVersion][isIE]().
  3. die is always called since the array minus 1 is NaN which is never equal to anything (not even if resultOfOperation() returns NaN) and THINGS_TO_EAT gets assigned the result of die().

Why?
JavaScript requires statements to end with a semicolon, except when it thinks it can safely infer their existence. In each of these examples, a function declaration or object or array literal is used inside a statement. The closing brackets are not enough to signal the end of the statement. Javascript never ends a statement if the next token is an infix or bracket operator.
This has really surprised people, so make sure your assignments end with semicolons.

Clarification: Semicolons and functions
Semicolons should be included at the end of function expressions, but not at the end of function declarations. The distinction is best illustrated with an example:
var foo = function() {
  return true;
};  // semicolon here.

function foo() {
  return true;
}  // no semicolon here.

Nested functions

link
Yes
Nested functions can be very useful, for example in the creation of continuations and for the task of hiding helper functions. Feel free to use them.

Function Declarations Within Blocks

link
No
Do not do this:
if (x) {
  function foo() {}
}
While most script engines support Function Declarations within blocks it is not part of ECMAScript (see ECMA-262, clause 13 and 14). Worse implementations are inconsistent with each other and with future EcmaScript proposals. ECMAScript only allows for Function Declarations in the root statement list of a script or function. Instead use a variable initialized with a Function Expression to define a function within a block:
if (x) {
  var foo = function() {};
}

Exceptions

link
Yes
You basically can't avoid exceptions if you're doing something non-trivial (using an application development framework, etc.). Go for it.

Custom exceptions

link
Yes
Without custom exceptions, returning error information from a function that also returns a value can be tricky, not to mention inelegant. Bad solutions include passing in a reference type to hold error information or always returning Objects with a potential error member. These basically amount to a primitive exception handling hack. Feel free to use custom exceptions when appropriate.

Standards features

link
Always preferred over non-standards features
For maximum portability and compatibility, always prefer standards features over non-standards features (e.g., string.charAt(3) over string[3] and element access with DOM functions instead of using an application-specific shorthand).

Wrapper objects for primitive types

link
No
There's no reason to use wrapper objects for primitive types, plus they're dangerous:
var x = new Boolean(false);
if (x) {
  alert('hi');  // Shows 'hi'.
}
Don't do it!
However type casting is fine.
var x = Boolean(0);
if (x) {
  alert('hi');  // This will never be alerted.
}
typeof Boolean(0) == 'boolean';
typeof new Boolean(0) == 'object';
This is very useful for casting things to numberstring and boolean.

Multi-level prototype hierarchies

link
Not preferred
Multi-level prototype hierarchies are how JavaScript implements inheritance. You have a multi-level hierarchy if you have a user-defined class D with another user-defined class B as its prototype. These hierarchies are much harder to get right than they first appear!
For that reason, it is best to use goog.inherits() from the Closure Library or a similar library function.
function D() {
  goog.base(this)
}
goog.inherits(D, B);

D.prototype.method = function() {
  ...
};

Method and property definitions

link
/** @constructor */ function SomeConstructor() { this.someProperty = 1; } Foo.prototype.someMethod = function() { ... };
While there are several ways to attach methods and properties to an object created via "new", the preferred style for methods is:
Foo.prototype.bar = function() {
  /* ... */
};
The preferred style for other properties is to initialize the field in the constructor:
/** @constructor */
function Foo() {
  this.bar = value;
}
Why?
Current JavaScript engines optimize based on the "shape" of an object, adding a property to an object (including overriding a value set on the prototype) changes the shape and can degrade performance.

delete

Prefer this.foo = null.

Closures

link
Yes, but be careful.
The ability to create closures is perhaps the most useful and often overlooked feature of JS. Here is a good description of how closures work.
One thing to keep in mind, however, is that a closure keeps a pointer to its enclosing scope. As a result, attaching a closure to a DOM element can create a circular reference and thus, a memory leak. For example, in the following code:
function foo(element, a, b) {
  element.onclick = function() { /* uses a and b */ };
}
the function closure keeps a reference to elementa, and b even if it never uses element. Since element also keeps a reference to the closure, we have a cycle that won't be cleaned up by garbage collection. In these situations, the code can be structured as follows:
function foo(element, a, b) {
  element.onclick = bar(a, b);
}

function bar(a, b) {
  return function() { /* uses a and b */ };
}

eval()

link
Only for code loaders and REPL (Read–eval–print loop)
eval() makes for confusing semantics and is dangerous to use if the string being eval()'d contains user input. There's usually a better, clearer, and safer way to write your code, so its use is generally not permitted.
For RPC you can always use JSON and read the result using JSON.parse() instead of eval().
Let's assume we have a server that returns something like this:
{
  "name": "Alice",
  "id": 31502,
  "email": "looking_glass@example.com"
}
var userInfo = eval(feed);
var email = userInfo['email'];
If the feed was modified to include malicious JavaScript code, then if we use eval then that code will be executed.
var userInfo = JSON.parse(feed);
var email = userInfo['email'];
With JSON.parse, invalid JSON (including all executable JavaScript) will cause an exception to be thrown.

with() {}

link
No
Using with clouds the semantics of your program. Because the object of the with can have properties that collide with local variables, it can drastically change the meaning of your program. For example, what does this do?
with (foo) {
  var x = 3;
  return x;
}
Answer: anything. The local variable x could be clobbered by a property of foo and perhaps it even has a setter, in which case assigning 3 could cause lots of other code to execute. Don't use with.

this

link
Only in object constructors, methods, and in setting up closures
The semantics of this can be tricky. At times it refers to the global object (in most places), the scope of the caller (in eval), a node in the DOM tree (when attached using an event handler HTML attribute), a newly created object (in a constructor), or some other object (if function was call()ed or apply()ed).
Because this is so easy to get wrong, limit its use to those places where it is required:
  • in constructors
  • in methods of objects (including in the creation of closures)

for-in loop

link
Only for iterating over keys in an object/map/hash
for-in loops are often incorrectly used to loop over the elements in an Array. This is however very error prone because it does not loop from 0 to length - 1 but over all the present keys in the object and its prototype chain. Here are a few cases where it fails:
function printArray(arr) {
  for (var key in arr) {
    print(arr[key]);
  }
}

printArray([0,1,2,3]);  // This works.

var a = new Array(10);
printArray(a);  // This is wrong.

a = document.getElementsByTagName('*');
printArray(a);  // This is wrong.

a = [0,1,2,3];
a.buhu = 'wine';
printArray(a);  // This is wrong again.

a = new Array;
a[3] = 3;
printArray(a);  // This is wrong again.
Always use normal for loops when using arrays.
function printArray(arr) {
  var l = arr.length;
  for (var i = 0; i < l; i++) {
    print(arr[i]);
  }
}

Associative Arrays

link
Never use Array as a map/hash/associative array
Associative Arrays are not allowed... or more precisely you are not allowed to use non number indexes for arrays. If you need a map/hash use Object instead of Array in these cases because the features that you want are actually features of Object and not of ArrayArray just happens to extend Object (like any other object in JS and therefore you might as well have used DateRegExp or String).

Multiline string literals

link
No
Do not do this:
var myString = 'A rather long string of English text, an error message \
                actually that just keeps going and going -- an error \
                message to make the Energizer bunny blush (right through \
                those Schwarzenegger shades)! Where was I? Oh yes, \
                you\'ve got an error and all the extraneous whitespace is \
                just gravy.  Have a nice day.';
The whitespace at the beginning of each line can't be safely stripped at compile time; whitespace after the slash will result in tricky errors; and while most script engines support this, it is not part of ECMAScript.
Use string concatenation instead:
var myString = 'A rather long string of English text, an error message ' +
    'actually that just keeps going and going -- an error ' +
    'message to make the Energizer bunny blush (right through ' +
    'those Schwarzenegger shades)! Where was I? Oh yes, ' +
    'you\'ve got an error and all the extraneous whitespace is ' +
    'just gravy.  Have a nice day.';

Array and Object literals

link
Yes
Use Array and Object literals instead of Array and Object constructors.
Array constructors are error-prone due to their arguments.
// Length is 3.
var a1 = new Array(x1, x2, x3);

// Length is 2.
var a2 = new Array(x1, x2);

// If x1 is a number and it is a natural number the length will be x1.
// If x1 is a number but not a natural number this will throw an exception.
// Otherwise the array will have one element with x1 as its value.
var a3 = new Array(x1);

// Length is 0.
var a4 = new Array();
Because of this, if someone changes the code to pass 1 argument instead of 2 arguments, the array might not have the expected length.
To avoid these kinds of weird cases, always use the more readable array literal.
var a = [x1, x2, x3];
var a2 = [x1, x2];
var a3 = [x1];
var a4 = [];
Object constructors don't have the same problems, but for readability and consistency object literals should be used.
var o = new Object();

var o2 = new Object();
o2.a = 0;
o2.b = 1;
o2.c = 2;
o2['strange key'] = 3;
Should be written as:
var o = {};

var o2 = {
  a: 0,
  b: 1,
  c: 2,
  'strange key': 3
};

Modifying prototypes of builtin objects

link
No
Modifying builtins like Object.prototype and Array.prototype are strictly forbidden. Modifying other builtins like Function.prototype is less dangerous but still leads to hard to debug issues in production and should be avoided.

Internet Explorer's Conditional Comments

link
No
Don't do this:
var f = function () {
    /*@cc_on if (@_jscript) { return 2* @*/  3; /*@ } @*/
};
Conditional Comments hinder automated tools as they can vary the JavaScript syntax tree at runtime.

JavaScript Style Rules

Naming

In general, use functionNamesLikeThisvariableNamesLikeThisClassNamesLikeThisEnumNamesLikeThismethodNamesLikeThis,CONSTANT_VALUES_LIKE_THISfoo.namespaceNamesLikeThis.bar, and filenameslikethis.js.

Custom toString() methods

link
Must always succeed without side effects.
You can control how your objects string-ify themselves by defining a custom toString() method. This is fine, but you need to ensure that your method (1) always succeeds and (2) does not have side-effects. If your method doesn't meet these criteria, it's very easy to run into serious problems. For example, if toString() calls a method that does an assertassert might try to output the name of the object in which it failed, which of course requires calling toString().

Explicit scope

Always

Code formatting

Expand for more information.

Parentheses

Only where required

Strings

Prefer ' over "

Visibility (private and protected fields)

link
Encouraged, use JSDoc annotations @private and @protected
We recommend the use of the JSDoc annotations @private and @protected to indicate visibility levels for classes, functions, and properties.
The --jscomp_warning=visibility compiler flag turns on compiler warnings for visibility violations. See Closure Compiler Warnings.
@private global variables and functions are only accessible to code in the same file.
Constructors marked @private may only be instantiated by code in the same file and by their static and instance members. @private constructors may also be accessed anywhere in the same file for their public static properties and by the instanceof operator.
Global variables, functions, and constructors should never be annotated @protected.
// File 1.
// AA_PrivateClass_ and AA_init_ are accessible because they are global
// and in the same file.

/**
 * @private
 * @constructor
 */
AA_PrivateClass_ = function() {
};

/** @private */
function AA_init_() {
  return new AA_PrivateClass_();
}

AA_init_();
@private properties are accessible to all code in the same file, plus all static methods and instance methods of that class that "owns" the property, if the property belongs to a class. They cannot be accessed or overridden from a subclass in a different file.
@protected properties are accessible to all code in the same file, plus any static methods and instance methods of any subclass of a class that "owns" the property.
Note that these semantics differ from those of C++ and Java, in that they grant private and protected access to all code in the same file, not just in the same class or class hierarchy. Also, unlike in C++, private properties cannot be overridden by a subclass.
// File 1.

/** @constructor */
AA_PublicClass = function() {
  /** @private */
  this.privateProp_ = 2;

  /** @protected */
  this.protectedProp = 4;
};

/** @private */
AA_PublicClass.staticPrivateProp_ = 1;

/** @protected */
AA_PublicClass.staticProtectedProp = 31;

/** @private */
AA_PublicClass.prototype.privateMethod_ = function() {};

/** @protected */
AA_PublicClass.prototype.protectedMethod = function() {};

// File 2.

/**
 * @return {number} The number of ducks we've arranged in a row.
 */
AA_PublicClass.prototype.method = function() {
  // Legal accesses of these two properties.
  return this.privateProp_ + AA_PublicClass.staticPrivateProp_;
};

// File 3.

/**
 * @constructor
 * @extends {AA_PublicClass}
 */
AA_SubClass = function() {
  // Legal access of a protected static property.
  AA_PublicClass.staticProtectedProp = this.method();
};
goog.inherits(AA_SubClass, AA_PublicClass);

/**
 * @return {number} The number of ducks we've arranged in a row.
 */
AA_SubClass.prototype.method = function() {
  // Legal access of a protected instance property.
  return this.protectedProp;
};
Notice that in JavaScript, there is no distinction between a type (like AA_PrivateClass_) and the constructor for that type. There is no way to express both that a type is public and its constructor is private (because the constructor could easily be aliased in a way that would defeat the privacy check).

JavaScript Types

Encouraged and enforced by the compiler.

Comments

Use JSDoc

Providing Dependencies With goog.provide

Only provide top-level symbols.

Compiling

Required

Tips and Tricks

JavaScript tidbits

Parting Words

BE CONSISTENT.
If you're editing code, take a few minutes to look at the code around you and determine its style. If they use spaces around all their arithmetic operators, you should too. If their comments have little boxes of hash marks around them, make your comments have little boxes of hash marks around them too.
The point of having style guidelines is to have a common vocabulary of coding so people can concentrate on what you're saying rather than on how you're saying it. We present global style rules here so people know the vocabulary, but local style is also important. If code you add to a file looks drastically different from the existing code around it, it throws readers out of their rhythm when they go to read it. Avoid this.
Revision 2.93
Aaron Whyte
Bob Jervis
Dan Pupius
Erik Arvidsson
Fritz Schneider
Robby Walker

Learn Angular js

Tuesday, April 7, 2015

Logic behind password encryption in joomla


Source : http://stackoverflow.com/questions/10428126/joomla-password-encryption

up vote36down voteaccepted
Joomla passwords are MD5 hashed, but the passwords are salted before being hashed. They are stored in the database as {hash}:{salt} this salt is a random string 32 characters in length.
So to create a new password hash you would do md5($password.$salt)
EDIT
Okay so for checking a password, say a user myguy enters the password mypassword, you would retrieve the row from the database that has username myguy.
In this row you'll find a password say 4e9e4bcc5752d6f939aedb42408fd3aa:0vURRbyY8Ea0tlvnTFn7xcKpjTFyn0YT. You split up the password hash and the salt:
$hashparts = preg_split (':' , $dbpassword);
echo $hashparts[0]; //this is the hash  4e9e4bcc5752d6f939aedb42408fd3aa
echo $hashparts[1]; //this is the salt  0vURRbyY8Ea0tlvnTFn7xcKpjTFyn0YT
now calculate the hash using this salt and the password myguy entered
$userhash = md5($userpassword.$hashparts[1]); // This would be 'mypassword' and the salt used in the original hash
Now if this $userhash and $hashparts[0] are identical the user has entered the correct password.
shareedit