tp TouchPointWP Utilities - TenthPres/TouchPoint-WP GitHub Wiki

tp\TouchPointWP\Utilities

A few tools for managing things.


  • Full name: \tp\TouchPointWP\Utilities
  • This class is an Abstract class

Constants

Constant Visibility Type Value
PLUGIN_UPDATE_TRANSIENT public \tp\TouchPointWP\TouchPointWP::SETTINGS_PREFIX . "plugin_update_data"
PLUGIN_UPDATE_TRANSIENT_TTL public 43200

Methods

toFloatOrNull

public static toFloatOrNull(mixed $numeric, bool|int $round = false): float|null
  • This method is static. Parameters:
Parameter Type Description
$numeric mixed
$round bool|int False to skip rounding. Otherwise, precision passed to round().

See Also:

  • \tp\TouchPointWP\round()

dateTimeNow

public static dateTimeNow(): \DateTimeImmutable
  • This method is static.

dateTimeTodayAtMidnight

public static dateTimeTodayAtMidnight(): \DateTimeImmutable
  • This method is static.

dateTimeNowPlus1Y

public static dateTimeNowPlus1Y(): \DateTimeImmutable
  • This method is static.

dateTimeNowPlus90D

public static dateTimeNowPlus90D(): \DateTimeImmutable
  • This method is static.

dateTimeNowPlus1D

public static dateTimeNowPlus1D(): \DateTimeImmutable
  • This method is static.

dateTimeNowMinus1D

public static dateTimeNowMinus1D(): \DateTimeImmutable
  • This method is static.

utcTimeZone

public static utcTimeZone(): \DateTimeZone
  • This method is static.

getPluralDayOfWeekNameForNumber

Gets the plural form of a weekday name.

public static getPluralDayOfWeekNameForNumber(int $dayNum): string
  • This method is static. Parameters:
Parameter Type Description
$dayNum int

Return Value:

Plural weekday (e.g. Mondays)


getPluralDayOfWeekNameForNumber_noI18n

Gets the plural form of a weekday name, but without translation for use in places like slugs.

public static getPluralDayOfWeekNameForNumber_noI18n(int $dayNum): string
  • This method is static. Parameters:
Parameter Type Description
$dayNum int

Return Value:

Plural weekday (e.g. Mondays)


getDayOfWeekShortForNumber

public static getDayOfWeekShortForNumber(int $dayNum): string
  • This method is static. Parameters:
Parameter Type Description
$dayNum int

getDaysOfWeekShort

Get the short form of the day of week, translated. Assumed to be usable as both singular and plural.

public static getDaysOfWeekShort(): string[]
  • This method is static.

getDayOfWeekShortForNumber_noI18n

NOT internationalized, such as for slugs

public static getDayOfWeekShortForNumber_noI18n(int $dayNum): string
  • This method is static. Parameters:
Parameter Type Description
$dayNum int

getTimeOfDayTermForTime

Gets the non-specific time of day in words.

public static getTimeOfDayTermForTime(\DateTimeInterface $dt, bool $i18n = true): string

Translation: These are deliberately not scoped to TouchPoint-WP, so if the translation exists globally, it should work here.

  • This method is static. Parameters:
Parameter Type Description
$dt \DateTimeInterface
$i18n bool

getTimeOfDayTermForTime_noI18n

public static getTimeOfDayTermForTime_noI18n(\DateTimeInterface $dt): string
  • This method is static. Parameters:
Parameter Type Description
$dt \DateTimeInterface

stringArrayToListString

Join an array of strings into a properly-formatted (English-style) list. Uses commas and ampersands by default.

public static stringArrayToListString(string[] $strings, int $limit = \tp\TouchPointWP\PHP_INT_MAX, bool $andOthers = false): string

This will switch to written "and" when an ampersand is present in a string, and will use semi-colons instead of commas when commas are already present.

Turn ['apples', 'oranges', 'pears'] into "apples, oranges & pears"

  • This method is static. Parameters:
Parameter Type Description
$strings string[]
$limit int The maximum number of items to include. Default is very, very high.
$andOthers bool If true, the last item will be "others" instead of the actual last item.

idArrayToIntArray

Convert a list (string or array) to an int array. Strips out non-numerics and explodes.

public static idArrayToIntArray(array|string $r, bool $explode = true): int[]|string
  • This method is static. Parameters:
Parameter Type Description
$r array|string
$explode bool

getPostContentWithShortcode

Gets the post content for all posts that contain a particular shortcode.

public static getPostContentWithShortcode(mixed $shortcode): object[]
  • This method is static. Parameters:
Parameter Type Description
$shortcode mixed

TODO MULTI: does not update for all sites in the network. |


getColorFor

Arbitrarily pick a unique-ish color for a value.

public static getColorFor(string $itemName, string $setName): string
  • This method is static. Parameters:
Parameter Type Description
$itemName string The name of the item. e.g. PA
$setName string The name of the set to which the item belongs, within which there should be uniqueness.
e.g. States

Return Value:

The color in hex, starting with '#'.


hslToHex

Convert HSL color to RGB Color

public static hslToHex(int $h, int $s, int $l): string
  • This method is static. Parameters:
Parameter Type Description
$h int Hue (0-365)
$s int Saturation (0-100)
$l int Luminosity (0-100)

getRegisteredPostTypesAsKVArray

Get the registered post types as a Key-Value array. Excludes post types that start with 'tp_'.

public static getRegisteredPostTypesAsKVArray(): string[]
  • This method is static.

createGuid

Generates a Microsoft-friendly globally unique identifier (Guid).

public static createGuid(): string
  • This method is static. Return Value:

A new random globally unique identifier.


var_dump_expandable

Do a var_dump, but within a container that can be expanded or contracted.

public static var_dump_expandable(mixed $args): void
  • This method is static. Parameters:
Parameter Type Description
$args mixed

getAllHeaders

Get all HTTP request headers.

public static getAllHeaders(): array
  • This method is static.

ownThumbnailId

Get the attachment ID of a post's own featured image, ignoring any image inherited from an ancestor.

public static ownThumbnailId(int $postId): int

Sync code should use this, not get_post_thumbnail_id(), which returns an inherited image for involvement and meeting posts. Otherwise, a child could be taken to own (and then delete or replace) its parent's image.

  • This method is static. Parameters:
Parameter Type Description
$postId int

Return Value:

0 if the post has no image of its own.


updatePostImageFromUrl

Updates or removes a post's featured image from a URL (e.g. from TouchPoint).

public static updatePostImageFromUrl(int $postId, string|null $newUrl, string $title, bool $verbose = false, array|null& $replacedAttIds = null): int

If the $newUrl is blank or null, the image is removed.

The image that's replaced is deleted from the media library right away, unless an array is provided for $replacedAttIds. Then, it's left in place, and its attachment ID is added to the array, so the caller can use deleteAttachmentIfUnused() once other posts using it have been updated.

  • This method is static. Parameters:
Parameter Type Description
$postId int
$newUrl string|null
$title string
$verbose bool
$replacedAttIds array|null Provide an array to defer deleting the replaced image.

Return Value:

The attachmentId for the image. Can be reused for other posts.


deleteAttachmentIfUnused

Delete an image from the media library, unless a post still uses it as its featured image. Archived meetings keep the image they had, so an image that was replaced is only deleted when nothing is holding on to it.

public static deleteAttachmentIfUnused(int $attachmentId, bool $verbose = false): bool
  • This method is static. Parameters:
Parameter Type Description
$attachmentId int
$verbose bool

Return Value:

True if the image was deleted.


standardizeHTags

public static standardizeHTags(int $maxAllowed, string $input): string
  • This method is static. Parameters:
Parameter Type Description
$maxAllowed int 1 to 6, corresponding to h1 to h6.
$input string The string within which headings should be standardized.

standardizeHtml

public static standardizeHtml(?string $html, ?string $context = null): string
  • This method is static. Parameters:
Parameter Type Description
$html ?string The HTML to be standardized.
$context ?string A context string to pass to hooks.

forceSlugUpdate

Sometimes WordPress tries to be smarter than we want it to be.

public static forceSlugUpdate(mixed $postId, mixed $newSlug): void

THIS DOES NOT DO ANY KIND OF VALIDATION OR REPLACEMENT. It is assumed that the input is already completely ready to go and won't cause collisions.

  • This method is static. Parameters:
Parameter Type Description
$postId mixed
$newSlug mixed

stringToSlug

Convert a string to something that's acceptable for use as a slug.

public static stringToSlug(string $s): string
  • This method is static. Parameters:
Parameter Type Description
$s string

titleWithoutPrefix

Remove a prefix from the start of a title, such as a parent event's title from the titles of its parts. For example, "Global Outreach Conference: Q&A Luncheon" becomes "Q&A Luncheon" when the prefix is "Global Outreach Conference".

public static titleWithoutPrefix(string $title, string $prefix): string

The prefix is only removed if it's followed by a separator (a colon, bar, middle dot, bullet, en dash, em dash, or a hyphen with a space beside it), so "Christmas Eve Service" isn't shortened to "Eve Service" by "Christmas". Case, and whether quotation marks and apostrophes are straight or curly, don't matter.

  • This method is static. Parameters:
Parameter Type Description
$title string The full title.
$prefix string The prefix to remove.

Return Value:

The title without the prefix and separator. If the title doesn't start with them, or nothing would be left, the title is returned as it was (trimmed).


checkForUpdate

Returns true if a new release is available.

public static checkForUpdate(): ?object
  • This method is static.

checkForUpdate_transient

public static checkForUpdate_transient(mixed $transient): mixed
  • This method is static. Parameters:
Parameter Type Description
$transient mixed

fileHeadersFromWeb

public static fileHeadersFromWeb(string $url, array $headers = []): ?array
  • This method is static. Parameters:
Parameter Type Description
$url string
$headers array

fileHeadersFromString

public static fileHeadersFromString(string $data, array $headers = []): ?array
  • This method is static. Parameters:
Parameter Type Description
$data string
$headers array

getClientIp

public static getClientIp(): ?string
  • This method is static.

validateRegistrantEmailAddress

Determine if an email address or user should be accepted as a new registrant. (e.g. informal auth)

public static validateRegistrantEmailAddress(?string $nickname, ?string $emailAddress, ?string& $resultComment = null): bool
  • This method is static. Parameters:
Parameter Type Description
$nickname ?string
$emailAddress ?string
$resultComment ?string If a spam provider provides a comment about why content was allowed or rejected,
it goes here.

Return Value:

True if acceptable, false if not acceptable.


validateMessage

Determine if an email address or user should be accepted as a new registrant. (e.g. informal auth)

public static validateMessage(?string $nickname, ?string $emailAddress, ?string $message, ?string& $resultComment = null): bool
  • This method is static. Parameters:
Parameter Type Description
$nickname ?string
$emailAddress ?string
$message ?string
$resultComment ?string If a spam provider provides a comment about why content was allowed or rejected,
it goes here.

Return Value:

True if acceptable, false if not acceptable.