tp TouchPointWP Utilities - TenthPres/TouchPoint-WP GitHub Wiki

tp\TouchPointWP\Utilities

A few tools for managing things.

  • Class name: Utilities
  • Namespace: \tp\TouchPointWP
  • This is an abstract class

Constants

PLUGIN_UPDATE_TRANSIENT

const PLUGIN_UPDATE_TRANSIENT = \tp\TouchPointWP\TouchPointWP::SETTINGS_PREFIX . "plugin_update_data"

PLUGIN_UPDATE_TRANSIENT_TTL

const PLUGIN_UPDATE_TRANSIENT_TTL = 43200

Methods

toFloatOrNull

float|null tp\TouchPointWP\Utilities::toFloatOrNull(mixed numeric, bool|int round)
  • Visibility: public
  • This method is static.

Arguments

  • numeric mixed* round bool|int - False to skip rounding. Otherwise, precision passed to round().

Returns

  • float|null -

dateTimeNow

\DateTimeImmutable tp\TouchPointWP\Utilities::dateTimeNow()
  • Visibility: public
  • This method is static.

Returns

  • \DateTimeImmutable -

dateTimeTodayAtMidnight

\DateTimeImmutable tp\TouchPointWP\Utilities::dateTimeTodayAtMidnight()
  • Visibility: public
  • This method is static.

Returns

  • \DateTimeImmutable -

dateTimeNowPlus1Y

\DateTimeImmutable tp\TouchPointWP\Utilities::dateTimeNowPlus1Y()
  • Visibility: public
  • This method is static.

Returns

  • \DateTimeImmutable -

dateTimeNowPlus1D

\DateTimeImmutable tp\TouchPointWP\Utilities::dateTimeNowPlus1D()
  • Visibility: public
  • This method is static.

Returns

  • \DateTimeImmutable -

dateTimeNowMinus1D

\DateTimeImmutable tp\TouchPointWP\Utilities::dateTimeNowMinus1D()
  • Visibility: public
  • This method is static.

Returns

  • \DateTimeImmutable -

utcTimeZone

\DateTimeZone tp\TouchPointWP\Utilities::utcTimeZone()
  • Visibility: public
  • This method is static.

Returns

  • \DateTimeZone -

getPluralDayOfWeekNameForNumber

string tp\TouchPointWP\Utilities::getPluralDayOfWeekNameForNumber(int dayNum)

Gets the plural form of a weekday name.

  • Visibility: public
  • This method is static.

Arguments

  • dayNum int

Returns

  • string - Plural weekday (e.g. Mondays)

getPluralDayOfWeekNameForNumber_noI18n

string tp\TouchPointWP\Utilities::getPluralDayOfWeekNameForNumber_noI18n(int dayNum)

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

  • Visibility: public
  • This method is static.

Arguments

  • dayNum int

Returns

  • string - Plural weekday (e.g. Mondays)

getDayOfWeekShortForNumber

string tp\TouchPointWP\Utilities::getDayOfWeekShortForNumber(int dayNum)
  • Visibility: public
  • This method is static.

Arguments

  • dayNum int

Returns

  • string -

getDaysOfWeekShort

string[] tp\TouchPointWP\Utilities::getDaysOfWeekShort()

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

  • Visibility: public
  • This method is static.

Returns

  • string[] -

getDayOfWeekShortForNumber_noI18n

string tp\TouchPointWP\Utilities::getDayOfWeekShortForNumber_noI18n(int dayNum)

NOT internationalized, such as for slugs

  • Visibility: public
  • This method is static.

Arguments

  • dayNum int

Returns

  • string -

getTimeOfDayTermForTime

string tp\TouchPointWP\Utilities::getTimeOfDayTermForTime(\DateTimeInterface dt, bool i18n)

Gets the non-specific time of day in words.

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

  • Visibility: public
  • This method is static.

Arguments

  • dt DateTimeInterface* i18n bool

Returns

  • string -

getTimeOfDayTermForTime_noI18n

mixed tp\TouchPointWP\Utilities::getTimeOfDayTermForTime_noI18n(\DateTimeInterface dt)
  • Visibility: public
  • This method is static.

Arguments

  • dt DateTimeInterface

Returns

  • mixed

stringArrayToListString

string tp\TouchPointWP\Utilities::stringArrayToListString(string[] strings, int limit, bool andOthers)

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

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"

  • Visibility: public
  • This method is static.

Arguments

  • 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.

Returns

  • string -

idArrayToIntArray

int[]|string tp\TouchPointWP\Utilities::idArrayToIntArray(array|string r, bool explode)

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

  • Visibility: public
  • This method is static.

Arguments

  • r array|string* explode bool

Returns

  • int[]|string -

getPostContentWithShortcode

object[] tp\TouchPointWP\Utilities::getPostContentWithShortcode(mixed shortcode)

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

  • Visibility: public
  • This method is static.

Arguments

  • shortcode mixed -

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

Returns

  • object[] -

getColorFor

string tp\TouchPointWP\Utilities::getColorFor(string itemName, string setName)

Arbitrarily pick a unique-ish color for a value.

  • Visibility: public
  • This method is static.

Arguments

  • 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

Returns

  • string - The color in hex, starting with '#'.

hslToHex

string tp\TouchPointWP\Utilities::hslToHex(int h, int s, int l)

Convert HSL color to RGB Color

  • Visibility: public
  • This method is static.

Arguments

  • h int - Hue (0-365)
  • s int - Saturation (0-100)
  • l int - Luminosity (0-100)

Returns

  • string -

getRegisteredPostTypesAsKVArray

string[] tp\TouchPointWP\Utilities::getRegisteredPostTypesAsKVArray()

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

  • Visibility: public
  • This method is static.

Returns

  • string[] -

createGuid

string tp\TouchPointWP\Utilities::createGuid()

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

  • Visibility: public
  • This method is static.

Returns

  • string - A new random globally unique identifier.

var_dump_expandable

void tp\TouchPointWP\Utilities::var_dump_expandable(mixed args)

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

  • Visibility: public
  • This method is static.

Arguments

  • args mixed

getAllHeaders

array tp\TouchPointWP\Utilities::getAllHeaders()

Get all HTTP request headers.

  • Visibility: public
  • This method is static.

Returns

  • array -

updatePostImageFromUrl

int|string tp\TouchPointWP\Utilities::updatePostImageFromUrl(int postId, string|null newUrl, string title)

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

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

  • Visibility: public
  • This method is static.

Arguments

  • postId int* newUrl string|null* title string

Returns

  • int|string - The attachmentId for the image. Can be reused for other posts.

standardizeHTags

string tp\TouchPointWP\Utilities::standardizeHTags(int maxAllowed, string input)
  • Visibility: public
  • This method is static.

Arguments

  • maxAllowed int - 1 to 6, corresponding to h1 to h6.
  • input string - The string within which headings should be standardized.

Returns

  • string -

standardizeHtml

string tp\TouchPointWP\Utilities::standardizeHtml(?string html, ?string context)
  • Visibility: public
  • This method is static.

Arguments

  • html ?string - The HTML to be standardized.
  • context ?string - A context string to pass to hooks.

Returns

  • string -

forceSlugUpdate

void tp\TouchPointWP\Utilities::forceSlugUpdate(mixed postId, mixed newSlug)

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

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.

  • Visibility: public
  • This method is static.

Arguments

  • postId mixed* newSlug mixed

checkForUpdate

?object tp\TouchPointWP\Utilities::checkForUpdate()

Returns true if a new release is available.

  • Visibility: public
  • This method is static.

Returns

  • ?object -

checkForUpdate_transient

mixed tp\TouchPointWP\Utilities::checkForUpdate_transient(mixed transient)
  • Visibility: public
  • This method is static.

Arguments

  • transient mixed

Returns

  • mixed

fileHeadersFromWeb

mixed tp\TouchPointWP\Utilities::fileHeadersFromWeb(string url, array headers)
  • Visibility: public
  • This method is static.

Arguments

  • url string* headers array

Returns

  • mixed

fileHeadersFromString

mixed tp\TouchPointWP\Utilities::fileHeadersFromString(string data, array headers)
  • Visibility: public
  • This method is static.

Arguments

  • data string* headers array

Returns

  • mixed

getClientIp

mixed tp\TouchPointWP\Utilities::getClientIp()
  • Visibility: public
  • This method is static.

Returns

  • mixed

validateRegistrantEmailAddress

bool tp\TouchPointWP\Utilities::validateRegistrantEmailAddress(?string nickname, ?string emailAddress, ?string resultComment)

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

  • Visibility: public
  • This method is static.

Arguments

  • nickname ?string* emailAddress ?string* resultComment ?string - If a spam provider provides a comment about why content was allowed or rejected, it goes here.

Returns

  • bool - True if acceptable, false if not acceptable.

validateMessage

bool tp\TouchPointWP\Utilities::validateMessage(?string nickname, ?string emailAddress, ?string message, ?string resultComment)

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

  • Visibility: public
  • This method is static.

Arguments

  • 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.

Returns

  • bool - True if acceptable, false if not acceptable.