1
0
Fork 0
forked from lino/radar-wp

Tidied up code comments pre-release.

This commit is contained in:
ekes 2019-05-12 15:37:07 +02:00
parent eab5592dd5
commit 65db102f6f
4 changed files with 224 additions and 81 deletions

View file

@ -1,7 +1,27 @@
<?php
/**
* Squat Radar Events Formatter.
*
* Provides filters to format the output of Squat Radar Events.
*
* 'squat_radar_format_event' allows the whole event to be formatted.
* Default filter Squat_Radar_Formatter::format_event().
*
* 'squat_radar_field_html' formats individual fields.
* Basic implementation Squat_Radar_Formatter::field_html().
*
* @package squat-radar
* @since 2.0.0
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
class Squat_Radar_Formatter {
/**
* Register filters with Wordpress.
*/
static public function register() {
// Filter to go through fields and then call filters to turn these into HTML.
add_filter('squat_radar_format_event', [__CLASS__, 'format_event'], 10, 3);
@ -28,6 +48,20 @@ class Squat_Radar_Formatter {
add_filter('squat_radar_field_html', [__CLASS__, 'field_title_html'], 15, 4);
}
/**
* Implementation of 'squat_radar_format_event'.
*
* Formats an API event array into HTML.
*
* @param array $event
* The event array from the API. Nested field names with values.
* @param array $fields
* The field names required for display. Colons used to denote nesting.
* @param array $context
*
* @return string
* HTML.
*/
static public function format_event($event, $fields, $context) {
$context['event'] = $event;
@ -44,7 +78,75 @@ class Squat_Radar_Formatter {
return $output;
}
/**
* Basic implementation of 'squat_radar_field_html' filter.
*
* Put the output into HTML.
*
* @param array|string $value
* The field value being manipulated to become HTML to be displayed.
* @param array|string $original
* The original value of the field before any changes by filters.
* @param array $field
* The field tree. $field[0] being the name of the present field. $field[1]
* being any parent etc.
* @param array $context
*
* @return string
* Flattend array with additional default classes.
*/
static public function field_html($value, $original, $field, $context) {
if ($value != $original) {
return $value;
}
if (is_array($value)) {
if ( ! empty($value['value']) ) {
$value = $value['value'];
}
elseif ( ! empty($value['title']) ) {
$value = $value['title'];
}
elseif ( ! empty($value['name']) ) {
$value = $value['name'];
}
elseif ( ! empty($value[0]['value']) ) {
foreach ($value as $row) {
$values[] = $row['value'];
}
$value = $values;
}
elseif ( ! empty($value[0]['title']) ) {
foreach ($value as $row) {
$titles[] = $row['title'];
}
$value = $titles;
}
elseif ( ! empty($value[0]['name']) ) {
foreach ($value as $row) {
$names[] = $row['name'];
}
$value = $names;
}
}
if (is_array($value)) {
$output = '<ul class="squat-radar-' . sanitize_html_class($field[0]) . ' squat-radar-list">';
foreach ($value as $row) {
$output .= '<li class="squat-radar-item-' . sanitize_html_class($field[0]) . '">' . sanitize_text_field( $row ) . '</li>';
}
$output .= '</ul>';
return $output;
}
else {
$value = '<span class="squat-radar-' . sanitize_html_class($field[0]) . '">' . wp_kses_post( $value ) . '</span>';
}
return $value;
}
/**
* Date field formatting implementation of 'squat_radar_field_html' filter.
*/
@ -257,11 +359,10 @@ class Squat_Radar_Formatter {
return $value;
}
/**
* Format image implementation of 'squat_radar_field_html' filter.
*
* Deliberatly run after field_link_html. Showing how to override an existing filter.
* Intentionally run after field_link_html. Showing how to override an existing filter.
* image:file:url
*/
static public function field_image_html($value, $original, $field, $context) {
@ -272,76 +373,9 @@ class Squat_Radar_Formatter {
return $value;
}
/**
* Basic implementation of 'squat_radar_field_html' filter.
*
* Put the output into HTML.
*
* @param array|string $value
* The field value being manipulated to become HTML to be displayed.
* @param array|string $original
* The original value of the field before any changes by filters.
* @param array $field
* The field tree. $field[0] being the name of the present field. $field[1]
* being any parent etc.
* @param array $context
*
* @return string
* Flattend array with additional default classes.
*/
static public function field_html($value, $original, $field, $context) {
if ($value != $original) {
return $value;
}
if (is_array($value)) {
if ( ! empty($value['value']) ) {
$value = $value['value'];
}
elseif ( ! empty($value['title']) ) {
$value = $value['title'];
}
elseif ( ! empty($value['name']) ) {
$value = $value['name'];
}
elseif ( ! empty($value[0]['value']) ) {
foreach ($value as $row) {
$values[] = $row['value'];
}
$value = $values;
}
elseif ( ! empty($value[0]['title']) ) {
foreach ($value as $row) {
$titles[] = $row['title'];
}
$value = $titles;
}
elseif ( ! empty($value[0]['name']) ) {
foreach ($value as $row) {
$names[] = $row['name'];
}
$value = $names;
}
}
if (is_array($value)) {
$output = '<ul class="squat-radar-' . sanitize_html_class($field[0]) . ' squat-radar-list">';
foreach ($value as $row) {
$output .= '<li class="squat-radar-item-' . sanitize_html_class($field[0]) . '">' . sanitize_text_field( $row ) . '</li>';
}
$output .= '</ul>';
return $output;
}
else {
$value = '<span class="squat-radar-' . sanitize_html_class($field[0]) . '">' . wp_kses_post( $value ) . '</span>';
}
return $value;
}
/**
* Title field HTML implentation of 'squat_radar_field_html' filter.
* Title field HTML implementation of 'squat_radar_field_html' filter.
*/
static public function field_title_html($value, $original, $field, $context) {
if (($field[0] == 'title' || $field[0] == 'title_field') && count($field) == 1) {