-
Notifications
You must be signed in to change notification settings - Fork 326
Email data section part and builder classes #11732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
benbowler
wants to merge
6
commits into
develop
Choose a base branch
from
enhancement/11680-email-data-section-classes
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b076bec
Create email report section part and builder classes with TDD.
benbowler 747077f
Remove function_exists checks for WP functions added before min suppo…
benbowler 22e9f26
Refactor report processor to new class and other small improvements.
benbowler 87dfa65
Merge remote-tracking branch 'origin/develop' into enhancement/11680-…
benbowler 44c018b
Merge remote-tracking branch 'origin/develop' into enhancement/11680-…
benbowler b26254e
Address multiple code review comments.
benbowler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
256 changes: 256 additions & 0 deletions
256
includes/Core/Email_Reporting/Email_Report_Data_Section_Part.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| <?php | ||
| /** | ||
| * Class Google\Site_Kit\Core\Email_Reporting\Email_Report_Data_Section_Part | ||
| * | ||
| * @package Google\Site_Kit\Core\Email_Reporting | ||
| * @copyright 2025 Google LLC | ||
| * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 | ||
| * @link https://sitekit.withgoogle.com | ||
| */ | ||
|
|
||
| namespace Google\Site_Kit\Core\Email_Reporting; | ||
|
|
||
| use InvalidArgumentException; | ||
|
|
||
| /** | ||
| * Single email report section part. | ||
| * | ||
| * @since n.e.x.t | ||
| * @access private | ||
| * @ignore | ||
| */ | ||
| class Email_Report_Data_Section_Part { | ||
|
|
||
| /** | ||
| * Unique section key. | ||
| * | ||
| * @var string | ||
| */ | ||
| private $section_key; | ||
|
|
||
| /** | ||
| * Section title. | ||
| * | ||
| * @var string | ||
| */ | ||
| private $title; | ||
|
|
||
| /** | ||
| * Labels for the section rows/series. | ||
| * | ||
| * @var array | ||
| */ | ||
| private $labels; | ||
|
|
||
| /** | ||
| * Values formatted as strings for output. | ||
| * | ||
| * @var array | ||
| */ | ||
| private $values; | ||
|
|
||
| /** | ||
| * Optional trends matching values. | ||
| * | ||
| * @var array|null | ||
| */ | ||
| private $trends; | ||
|
|
||
| /** | ||
| * Optional date range data. | ||
| * | ||
| * @var array|null | ||
| */ | ||
| private $date_range; | ||
|
|
||
| /** | ||
| * Optional dashboard deeplink URL. | ||
| * | ||
| * @var string|null | ||
| */ | ||
| private $dashboard_link; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param string $section_key Unique section key. | ||
| * @param string $title Section title. | ||
| * @param array $labels Labels (strings) for the section rows/series. | ||
| * @param array $values Values (already formatted to strings for output). | ||
| * @param array|null $trends Optional trends (strings) matching values. | ||
| * @param array|null $date_range Optional date range array with 'startDate' and 'endDate'. | ||
| * @param string|null $dashboard_link Optional dashboard deeplink. | ||
| * | ||
| * @throws InvalidArgumentException When validation fails. | ||
| */ | ||
| public function __construct( | ||
| $section_key, | ||
| $title, | ||
| array $labels, | ||
| array $values, | ||
zutigrm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $trends = null, | ||
| $date_range = null, | ||
| $dashboard_link = null | ||
| ) { | ||
| $section_key = is_string( $section_key ) ? $section_key : ''; | ||
| $title = is_string( $title ) ? $title : ''; | ||
|
|
||
| if ( '' === $section_key ) { | ||
| throw new InvalidArgumentException( 'section_key must be a non-empty string' ); | ||
| } | ||
| if ( '' === $title ) { | ||
| throw new InvalidArgumentException( 'title must be a non-empty string' ); | ||
| } | ||
zutigrm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| $this->labels = array_map( 'strval', $labels ); | ||
| $this->values = array_map( 'strval', $values ); | ||
|
|
||
| if ( ! is_array( $labels ) ) { | ||
| throw new InvalidArgumentException( 'labels must be an array' ); | ||
| } | ||
| if ( ! is_array( $values ) ) { | ||
| throw new InvalidArgumentException( 'values must be an array' ); | ||
| } | ||
zutigrm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if ( null !== $trends ) { | ||
| if ( ! is_array( $trends ) ) { | ||
| throw new InvalidArgumentException( 'trends must be an array or null' ); | ||
| } | ||
| $this->trends = array_map( 'strval', $trends ); | ||
| } else { | ||
| $this->trends = null; | ||
| } | ||
zutigrm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if ( null !== $date_range ) { | ||
| if ( ! is_array( $date_range ) ) { | ||
| throw new InvalidArgumentException( 'date_range must be an array or null' ); | ||
| } | ||
| // Validate presence of keys if provided. | ||
| $start = isset( $date_range['startDate'] ) ? (string) $date_range['startDate'] : null; | ||
| $end = isset( $date_range['endDate'] ) ? (string) $date_range['endDate'] : null; | ||
| if ( null === $start || null === $end ) { | ||
| throw new InvalidArgumentException( 'date_range must contain startDate and endDate' ); | ||
| } | ||
zutigrm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $compare_start_provided = array_key_exists( 'compareStartDate', $date_range ); | ||
| $compare_end_provided = array_key_exists( 'compareEndDate', $date_range ); | ||
| if ( $compare_start_provided xor $compare_end_provided ) { | ||
| throw new InvalidArgumentException( 'date_range must contain both compareStartDate and compareEndDate when comparison dates are provided' ); | ||
| } | ||
| $this->date_range = array( | ||
| 'startDate' => $start, | ||
| 'endDate' => $end, | ||
| ); | ||
zutigrm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ( $compare_start_provided && $compare_end_provided ) { | ||
| $this->date_range['compareStartDate'] = (string) $date_range['compareStartDate']; | ||
| $this->date_range['compareEndDate'] = (string) $date_range['compareEndDate']; | ||
| } | ||
| } else { | ||
| $this->date_range = null; | ||
| } | ||
zutigrm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if ( null !== $dashboard_link && ! is_string( $dashboard_link ) ) { | ||
| throw new InvalidArgumentException( 'dashboard_link must be a string or null' ); | ||
| } | ||
|
|
||
| $this->section_key = $section_key; | ||
| $this->title = $title; | ||
| $this->dashboard_link = $dashboard_link; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the section key. | ||
| * | ||
| * @return string | ||
| */ | ||
zutigrm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public function get_section_key() { | ||
| return $this->section_key; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the title. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function get_title() { | ||
| return $this->title; | ||
| } | ||
|
|
||
| /** | ||
| * Gets labels. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function get_labels() { | ||
| return $this->labels; | ||
| } | ||
|
|
||
| /** | ||
| * Gets values. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function get_values() { | ||
| return $this->values; | ||
| } | ||
|
|
||
| /** | ||
| * Gets trends. | ||
| * | ||
| * @return array|null | ||
| */ | ||
| public function get_trends() { | ||
| return $this->trends; | ||
| } | ||
|
|
||
| /** | ||
| * Gets date range. | ||
| * | ||
| * @return array|null | ||
| */ | ||
| public function get_date_range() { | ||
| return $this->date_range; | ||
| } | ||
|
|
||
| /** | ||
| * Gets dashboard link. | ||
| * | ||
| * @return string|null | ||
| */ | ||
| public function get_dashboard_link() { | ||
| return $this->dashboard_link; | ||
| } | ||
|
|
||
| /** | ||
| * Whether the section is empty (no values or all empty strings). | ||
| * | ||
| * @return bool | ||
| */ | ||
zutigrm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public function is_empty() { | ||
| if ( empty( $this->values ) ) { | ||
| return true; | ||
| } | ||
| foreach ( $this->values as $value ) { | ||
| if ( '' !== trim( (string) $value ) ) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
zutigrm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Returns normalized array representation for templates. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function to_array() { | ||
| return array( | ||
| 'section_key' => $this->section_key, | ||
| 'title' => $this->title, | ||
| 'labels' => $this->labels, | ||
| 'values' => $this->values, | ||
| 'trends' => $this->trends, | ||
| 'date_range' => $this->date_range, | ||
| 'dashboard_link' => $this->dashboard_link, | ||
| ); | ||
| } | ||
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.