I have written the following class which is part of a pagination system in Wordpress. The class works as expected and also works as expected when used in the system.
Just as background, the class performs the following task
Takes an array of post ID's and creating links to the posts which ID's is supplied in the array.
Uses Wordpress functions
get_permalink()
,add_query_arg()
andget_post_field()
to retrieve the appropriate information to build the links
I need a review to asses the correctness of my code, basically how correct is my code, and also, how correct is my use of PHPDoc blocks and the information in these doc blocks.
Here is my class: (I have left out the interface)
<?php
namespace PG\Single\Post\Navigation;
/**
* PostLinks class
*
* Creates clickable post links for post ID's given
*
* @param (array) $postIDs Array of post IDs
* @param (array) $extraQueryVars Array of query variables to add to the URL's
* @param (array) $args Array of arguments. See below
* - @param (bool) previous Whether or not to get adjacent post older or newer to current post Default true
* - @param (bool) boundary Whether or not to get the boundary posts Default false
* - @param (bool) first Whether or not to get the first or last post when the boundary parameter is set to true Default true
* - @param (string) anchorText Text to be used as an anchor text Default %anchor uses post title
* - @param (string) postLinkText Text to be used as link text Default %text uses post title
* - @param (string) spanTextOldest Text to be used as oldest post link Default Oldest post:
* - @param (string) spanTextNewest Text to be used as newest post link Default Newest post:
* - @param (string) spanTextPrevious Text to be used as older post link Default Older post:
* - @param (string) spanTextNext Text to be used as newer post link Default Newer post:
*
* @since 1.0.0
*/
class PostLinks implements PostLinksInterface
{
/**
* @since 1.0.0
* @access protected
* @var (array) $postIDs
*/
protected $postIDs;
/**
* @since 1.0.0
* @access protected
* @var (array) $extraQueryVars
*/
protected $extraQueryVars;
/**
* @since 1.0.0
* @access protected
* @var (array) $args
*/
protected $args;
/**
* Sets the default arguments.
*
* @since 1.0.0
* @access protected
* @var (array) $defaults
*/
protected $defaults = [
'previous' => true,
'boundary' => false,
'first' => true,
'anchorText' => '%anchor',
'postLinkText' => '%text',
'spanTextOldest' => 'Oldest post: ',
'spanTextNewest' => 'Newest post: ',
'spanTextPrevious' => 'Older post: ',
'spanTextNext' => 'Newer post: ',
];
/**
* Public constructor method
*
* @param (array) $postIDs Array of post IDs
* @param (array) $extraQueryVars Array of query variables to add to the URL's
* @param (array) $args Array of arguments. See below
* - @param (bool) previous Whether or not to get adjacent post older or newer to current post Default true
* - @param (bool) boundary Whether or not to get the boundary posts Default false
* - @param (bool) first Whether or not to get the first or last post when the boundary parameter is set to true Default true
* - @param (string) anchorText Text to be used as an anchor text Default %anchor
* - @param (string) postLinkText Text to be used as link text Default %text
* - @param (string) spanTextOldest Text to be used as oldest post link Default Oldest post:
* - @param (string) spanTextNewest Text to be used as newest post link Default Newest post:
* - @param (string) spanTextPrevious Text to be used as older post link Default Older post:
* - @param (string) spanTextNext Text to be used as newer post link Default Newer post:
*
* @since 1.0.0
*/
public function __construct($postIDs = null, $extraQueryVars = null, $args = [])
{
$this->setPostIDs($postIDs);
$this->setExtraQueryVars($extraQueryVars);
$this->setArgs($args);
}
/**
* Setter setPostLinks()
*
* Sets an array of posts IDs
*
* @since 1.0.0
* @param $postIDs
* @return $this
*/
public function setPostIDs($postIDs)
{
$this->postIDs = filter_var($postIDs, FILTER_VALIDATE_INT, ['flags' => FILTER_FORCE_ARRAY]);
return $this;
}
/**
* Returns the posts IDs.
*
* @since 1.0.0
* @return (array) $this->postIDs
*/
public function getPostIDs()
{
return $this->postIDs;
}
/**
* Setter setExtraQueryVars()
*
* Sets an array of additional query variables to add to the URL's
*
* @since 1.0.0
* @param $extraQueryVars
* @return $this
*/
public function setExtraQueryVars($extraQueryVars)
{
$this->extraQueryVars = $extraQueryVars;
return $this;
}
/**
* Returns the array of query variables.
*
* @since 1.0.0
* @return (array) $this->extraQueryVars
*/
public function getExtraQueryVars()
{
return $this->extraQueryVars;
}
/**
* Setter setArgs
*
* Sets the arguments and merges them with the defaults and also cast array to an object.
*
* @since 1.0.0
* @param $args
* @return $this
*/
public function setArgs($args)
{
$this->args = (object) array_merge($this->defaults, $args);
return $this;
}
/**
* Returns an object of arguments.
*
* @since 1.0.0
* @return (object) $this->args
*/
public function getArgs()
{
return $this->args;
}
/**
* Conditional tag to check if the boundaryPosts parameter is set to true. Any other value returns false.
*
* @access private
* @since 1.0.0
* @return (bool) true on success false on failure
*/
private function isBoundary()
{
return $this->args->boundary === true ? true : false;
}
/**
* Conditional tag to check if the previous parameter is set to true. Any other value returns false.
*
* @access private
* @since 1.0.0
* @return (bool) true on success false on failure
*/
private function isPrevious()
{
return $this->args->previous === true ? true : false;
}
/**
* Conditional tag to check if the first parameter is set to true. Any other value returns false.
*
* @access private
* @since 1.0.0
* @return (bool) true on success false on failure
*/
private function isFirst()
{
return $this->args->first === true ? true : false;
}
/**
* Text to be used as post link pre-text according to parameter values set
* by previous, boundary and first
*
* @access private
* @since 1.0.0
* @return (string) $text
*/
private function spanTextText()
{
$text = null;
if ($this->isBoundary() !== true) {
if ($this->isPrevious()) {
$text = filter_var($this->args->spanTextPrevious, FILTER_SANITIZE_STRING);
} else {
$text = filter_var($this->args->spanTextNext, FILTER_SANITIZE_STRING);
}
} else {
if ($this->isFirst()) {
$text = filter_var($this->args->spanTextOldest, FILTER_SANITIZE_STRING);
} else {
$text = filter_var($this->args->spanTextNewest, FILTER_SANITIZE_STRING);
}
}
return $text;
}
/**
* CSS classes to be used for post links according to parameter values set
* by previous, boundary and first.
*
* @access private
* @since 1.0.0
* @return (string) $classes
*/
private function linkclasses()
{
$classes = null;
if ($this->isBoundary() !== true) {
if ($this->isPrevious()) {
$classes = 'previous';
} else {
$classes = 'next';
}
} else {
if ($this->isFirst()) {
$classes = 'oldest';
} else {
$classes = 'newest';
}
}
return $classes;
}
/**
* Create the post links according to input values of the class
*
* @since 1.0.0
* @return (string) $link
*/
public function links()
{
$link = '';
if ($this->postIDs !== null) {
$link .= '<div class"paginate-nav-links ' . $this->linkclasses() . '">';
foreach ($this->postIDs as $key=>$postID) {
/*
* Get post post_title according to ID.
*
* @uses get_post_field()
* @see http://codex.wordpress.org/Function_Reference/get_post_field
*/
$postTitle = get_post_field('post_title', $postID);
/*
* Test to see if WP_Error is not triggered. If so, continue
*/
if (is_wp_error($postTitle))
continue;
/*
* Made it to here, build the post links.
*/
if ($this->extraQueryVars === null) {
/**
* Get the post permalink.
*
* @uses get_permalink()
* @see https://codex.wordpress.org/Function_Reference/get_permalink
*/
$url = get_permalink($postID);
} else {
/*
* Test if $this->extraQueryVars is a valid array. Throw exception on error
*/
if (!is_array($this->extraQueryVars)) {
throw new \InvalidArgumentException(
sprintf(
__('%s: The value of $extraQueryVars should be an array. Please recheck the the $extraQueryVars input'),
__METHOD__
)
);
}
/*
* If an array of query vars is set. sanitize the array and add to the URL
*/
foreach ($this->extraQueryVars as $k=>$v)
$vars[filter_var($k, FILTER_SANITIZE_STRING)] = filter_var($v, FILTER_SANITIZE_STRING);
/**
* Add the custom query variables to the post URL
*
* @uses add_query_arg()
* @see https://codex.wordpress.org/Function_Reference/add_query_arg
*/
$url = add_query_arg($vars, get_permalink($postID));
}
/*
* If defaults are used, $anchor and $linkText will default to post titles
*/
$anchor = $this->args->anchorText == '%anchor' ? $postTitle : $this->args->anchorText;
$linkText = $this->args->postLinkText == '%text' ? $postTitle : $this->args->postLinkText;
if ($key === 0) {
/*
* Don't print any mark-up if $this->spanTextText() is empty or null
*/
if ($this->spanTextText()) {
$link .= '<div class"paginate-nav-links ' . $this->linkclasses() . '-text">';
$link .= filter_var($this->spanTextText(), FILTER_SANITIZE_STRING);
$link .= '</div>';
}
$link .= '<div class"paginate-nav-links links">';
}
$link .= '<div class"paginate-nav-links link-' . ($key + 1) . '">';
$link .= '<a href="' . filter_var($url, FILTER_SANITIZE_URL) . '" title="' . filter_var($anchor, FILTER_SANITIZE_STRING) . '">';
$link .= filter_var($linkText, FILTER_SANITIZE_STRING);
$link .= '</a>';
$link .= '</div>';
if (!array_key_exists(($key + 1), $this->postIDs)) {
$link .= '</div>';
}
}
$link .= '</div>';
}
/*
* return null if no post links exists
*/
if (!$link)
$link = null;
/*
* return a string holding the post links
*/
return $link;
}
}