array(self::TYPE_AUTHOR, STRING_EMPTY),
1 => array(self::TYPE_COAUTHOR, STRING_EMPTY),
2 => array(self::TYPE_EDITOR, STRING_EMPTY),
3 => array(self::TYPE_REVIEWER, STRING_EMPTY),
4 => array(self::TYPE_THANKS, STRING_EMPTY));
private $num_of_types = 5;
private $num_of_values = 0;
/**
* Generates an array of data which has the type, as well as the value for each type.
* The default array is global variable arr_types, but additional elements may be added by this method.
*/
function all_due_credit_generate_data_array($post, $arr_types, &$num_of_values, $num_of_types){
$num_of_values = 0;
if(!empty($arr_types)){
//Go through the all types, and if we have something to display, modify the
//value in the array,as it is used later when showing ADC.
for ($row = 0; $row < $num_of_types; $row++){
$str_value = get_post_meta($post->ID, self::ADC_PREFIX . $arr_types[$row][self::INDEX_KEY], true);
if($arr_types[$row][self::INDEX_KEY] != self::TYPE_THANKS){
//Split the value simply so we can verify the name.
$arr_split = explode(COMMA, $str_value);
//Was a name provided?
// John Doe,jdoe@provider.com,jdoeswebsite.com
// ^^^^^^^^
if(!empty($arr_split[self::INDEX_VALUE_NAME])){
$num_of_values += 1;
//Update the value directly in the type array.
//The value we are setting could look like this:
// John Doe,jdoe@provider.com,jdoeswebsite.com
//This is not a problem, as the Show method handles splitting.
$arr_types[$row][self::INDEX_VALUE] = $str_value;
}
}else{
//Consider the special type: "Thanks". It can have multiple thanks in one meta field.
//The format is the same as usual, but is semi-colon delimited to signal multiple thanks.
//John Doe,jdoe@provider.com,jdoeswebsite.com;John Doe,jdoe@provider.com,jdoeswebsite.com
$arr_thanks_data = explode(SEMI_COLON, $str_value);
for($i = 0; $i < count($arr_thanks_data); $i++){
//Split the value simply so we can verify the name.
$arr_split = explode(COMMA, $arr_thanks_data[$i]);
//Was a name provided?
// John Doe,jdoe@provider.com,jdoeswebsite.com
// ^^^^^^^^
if(!empty($arr_split[self::INDEX_VALUE_NAME])){
//We're adding a new, valid data - so update the count.
$num_of_values += 1;
//Add a new "Thanks to" type. If multiple "Thanks to" values are provided in the meta,
//we may end up adding multiple "Thanks to" types. This is acceptable because we later
//iterate through the types using the array index id. Duplicates therefore don't cause any problems.
array_push($arr_types, array(self::TYPE_THANKS, $arr_thanks_data[$i]));
}
}
}
}
}
return $arr_types;
}
/**
* Appends the necessary HTML code to open a row of credits.
*/
function all_due_credit_add_credit_row_open($str_credits){
$str_credits .= "
" . CRLF;
return $str_credits;
}
/**
* Appends the necessary HTML code to close a row of credits.
*/
function all_due_credit_add_credit_row_close($str_credits){
$str_credits .= '
' . CRLF;
return $str_credits;
}
/**
* Given one particular arr_type entry, this method will add the required
* HTML elements to represent one cell in a row of credits.
*/
function all_due_credit_add_credit_cell($str_credits, $arr_type){
$str_email = STRING_EMPTY;
$str_website = STRING_EMPTY;
$str_protocol = STRING_EMPTY;
$bln_added_url = false;
//At this point, we expect something like this:
// John Doe,jdoe@provider.com,jdoeswebsite.com
//Decided to use comma here because I felt it was the most intuitive.
$arr_data = explode(COMMA, $arr_type[self::INDEX_VALUE]);
//Make sure the array elements exist prior to using.
if(array_key_exists(self::INDEX_VALUE_EMAIL, $arr_data))
$str_email = $arr_data[self::INDEX_VALUE_EMAIL];
if(array_key_exists(self::INDEX_VALUE_WEBSITE, $arr_data))
$str_website = $arr_data[self::INDEX_VALUE_WEBSITE];
$str_credits .= TAB . "' . CRLF;
return $str_credits;
}
/**
* Contains the master logic behind the generation of the All Due Credits HTML.
* Responsible for adding the resulting HTML to the post on-the-fly.
*/
function all_due_credit_show($content){
global $post;
$str_credits = STRING_EMPTY;
$int_arr_count = 0;
$this->num_of_values = 0;
$this->arr_types = $this->all_due_credit_generate_data_array($post, $this->arr_types, $this->num_of_values, $this->num_of_types);
//Only continue if we're not on the home page.
//ADC should only appear on a specific post page.
if(!is_home()){
$row_cells_used = 0;
if($this->num_of_values > 0){
$int_arr_count = count($this->arr_types);
for ($row = 0; $row < $int_arr_count; $row++){
$str_value = $this->arr_types[$row][self::INDEX_VALUE];
if(!empty($str_value)){
if($row_cells_used == 0)
$str_credits = $this->all_due_credit_add_credit_row_open($str_credits);
$str_credits = $this->all_due_credit_add_credit_cell($str_credits, $this->arr_types[$row]);
$row_cells_used = $row_cells_used + 1;
//Close the row when:
// - We've reached the column limit.
// - The cell we just wrote is the last in the loop.
if($row_cells_used == self::MAXIMUM_COLS || ($this->num_of_values == 1)){
$str_credits = $this->all_due_credit_add_credit_row_close($str_credits);
$row_cells_used = 0;
}
$this->num_of_values = $this->num_of_values - 1;
}
}
}
}
//check if the post meta has been set.
return $content . $str_credits;
}
/**
* Register and enqueue the All Due Credit CSS file.
* Uses the suggested approach as per the API documentation:
* http://codex.wordpress.org/Function_Reference/wp_enqueue_style
* The approach taken requires Wordpress 2.7 to function.
*/
function all_due_credit_add_styles() {
$adc_style_url = WP_PLUGIN_URL . self::PATH_TO_CSS;
$adc_style_file = WP_PLUGIN_DIR . self::PATH_TO_CSS;
if ( file_exists($adc_style_file) ) {
wp_register_style('all-due-credit', $adc_style_url);
wp_enqueue_style( 'all-due-credit');
}
}
}
function all_due_credit_add_styles_wrapper(){
global $obj_all_due_credit;
if(isset($obj_all_due_credit)){
$obj_all_due_credit->all_due_credit_add_styles();
}
}
function all_due_credit_show_wrapper($content){
global $obj_all_due_credit;
if(isset($obj_all_due_credit)){
$content = $obj_all_due_credit->all_due_credit_show($content);
}
return $content;
}
?>