Skip to content

Commit

Permalink
reformatted some code to help readability, escape html elements from …
Browse files Browse the repository at this point in the history
…input
  • Loading branch information
nlannon27 committed Apr 2, 2023
1 parent 658f8e7 commit 195e0a4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 34 deletions.
10 changes: 9 additions & 1 deletion assets/google-sheets-fetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,16 @@ function fetchSheetData($sheetCSVURL, $dataCoordinates) {
return "ERR";
}

$column = $coordinates[0];
$row = $coordinates[1];

$numColumns = count($data[0]);
$numRows = count($data);

//return $column . "," . $row . " " . $numColumns . "," . $numRows;

// if the coordinate input is outside of the range, throw an error
if($coordinates[1] > count($data) || $coordinates[0] < 0 || $coordinates[0] > count($data[$coordinates[1]]) || $coordinates[1] < 0) {
if($column > $numColumns - 1 || $column < 0 || $row > $numRows - 1 || $row < 0) {
error_log('Google Sheet Integration Error: Coordinate Input outside of Sheet Range');
return "ERR";
}
Expand Down
103 changes: 70 additions & 33 deletions src/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,58 +27,95 @@ function pullDescription($attributes) {
return fetchSheetData($attributes["sheetCSVURL"], $attributes["descriptionLocation"]);
}

// renders the block
function create_block_google_sheets_block_render($attributes, $content, $block) {
$wrapper_attributes = get_block_wrapper_attributes();
// creates a new description element
function createDescriptionElement($attributes) {
$descriptionSize = $attributes["descriptionSize"];
$descriptionAlignment = $attributes["descriptionAlignment"];
$descriptionColor = $attributes["descriptionColor"];

$flipPositions = $attributes["flipPositions"];

$enableManualDescription = !array_key_exists("descriptionLocation", $attributes) || $attributes["descriptionLocation"] == "";
$descriptionValue = $enableManualDescription ? esc_html($attributes["description"]) : esc_html(pullDescription($attributes));

$description = "<p ";
$description .= "style='";
$description .= "font-size: " . $descriptionSize . "px; ";
$description .= "text-align: " . $descriptionAlignment . "; ";
$description .= "color: " . $descriptionColor . "; ";
$description .= ($flipPositions ? "margin-bottom: 0; " : "margin-top: 0; ");
$description .= ($flipPositions ? "padding-bottom: 0; " : "padding-top: 0; ");
$description .= "'";
$description .= ">" . $descriptionValue . "</p>";

return $description;
}

// creates the data element
function createDataElement($attributes) {
$dataValue = esc_html(pullData($attributes));

$dataSize = $attributes["dataSize"];
$dataAlignment = $attributes["dataAlignment"];
$dataColor = $attributes["dataColor"];

// whether or not the user has set a description manually
$manualDescription = !array_key_exists("descriptionLocation", $attributes) || $attributes["descriptionLocation"] == "";
$flipPositions = $attributes["flipPositions"];

$dataValue = pullData($attributes);
$enableAnimatedCounting = $attributes["animatedCounting"];
$animatedCountDuration = $attributes["countDuration"];

$dataPrefix = esc_html($attributes["dataPrefix"]);
$enableDataPrefix = $dataPrefix && $dataPrefix != "";

$dataSuffix = esc_html($attributes["dataSuffix"]);
$enableDataSuffix = $dataSuffix && $dataSuffix != "";

$data = "<p ";
if($attributes["animatedCounting"]) { // sets the attributes if this will be animated
if($enbleAnimatedCounting) { // sets the attributes if this will be animated
$data .= "class='google-sheets-counter' ";
$data .= "duration='" . $attributes["countDuration"] . "' ";
$data .= "duration='" . $animatedCountDuration . "' ";
$data .= "countgoal='" . $dataValue . "' ";

if($attributes["dataPrefix"] && $attributes["dataPrefix"] != "") {
$data .= "prefix='" . $attributes["dataPrefix"] . "' ";
// add prefix and suffix to attributes, allows me to readd them with javascript
if($enableDataPrefix) {
$data .= "prefix='" . $dataPrefix . "' ";
}

if($attributes["dataSuffix"] && $attributes["dataSuffix"] != "") {
$data .= "suffix='" . $attributes["dataSuffix"] . "' ";
if($enableDataSuffix) {
$data .= "suffix='" . $dataSuffix . "' ";
}
}

$data .= "style='";
$data .= "font-size: " . $attributes["dataSize"] . "px; ";
$data .= "text-align: " . $attributes["dataAlignment"] . "; ";
$data .= "color: " . $attributes["dataColor"] . "; ";
$data .= ($attributes["flipPositions"] ? "margin-top: 0; " : "margin-bottom: 0; ");
$data .= ($attributes["flipPositions"] ? "padding-top: 0; " : "padding-bottom: 0; ");
$data .= "font-size: " . $dataSize . "px; ";
$data .= "text-align: " . $dataAlignment . "; ";
$data .= "color: " . $dataColor . "; ";
$data .= ($flipPositions ? "margin-top: 0; " : "margin-bottom: 0; ");
$data .= ($flipPositions ? "padding-top: 0; " : "padding-bottom: 0; ");
$data .= "'";

// don't show value immediately if counting
if($attributes["animatedCounting"]) {
$data .= ">" . $attributes["dataPrefix"] . "0" . $attributes["dataSuffix"] . "</p>";
// if animated counting is enabled, make it start at 0.
if($enableAnimatedCounting) {
$data .= ">" . $dataPrefix . "0" . $dataSuffix . "</p>";
} else {
$data .= ">" . $attributes["dataPrefix"] . $dataValue . $attributes["dataSuffix"] . "</p>";
$data .= ">" . $dataPrefix . $dataValue . $dataSuffix . "</p>";
}

$description = "<p ";
$description .= "style='";
$description .= "font-size: " . $attributes["descriptionSize"] . "px; ";
$description .= "text-align: " . $attributes["descriptionAlignment"] . "; ";
$description .= "color: " . $attributes["descriptionColor"] . "; ";
$description .= ($attributes["flipPositions"] ? "margin-bottom: 0; " : "margin-top: 0; ");
$description .= ($attributes["flipPositions"] ? "padding-bottom: 0; " : "padding-top: 0; ");
$description .= "'";
$description .= ">" . ($manualDescription ? $attributes["description"] : pullDescription($attributes)) . "</p>";

return $data;
}

// renders the block
function create_block_google_sheets_block_render($attributes, $content, $block) {
$wrapper_attributes = get_block_wrapper_attributes();

$flipPositions = $attributes["flipPositions"];

$description = createDescriptionElement($attributes);
$data = createDataElement($attributes);

return
"<div " . $wrapper_attributes . ">"
. ($attributes["flipPositions"] ? $description : $data)
. ($attributes["flipPositions"] ? $data : $description) .
. ($flipPositions ? $description : $data)
. ($flipPositions ? $data : $description) .
"</div>";
}

0 comments on commit 195e0a4

Please sign in to comment.