| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { __, _n, sprintf } from '@wordpress/i18n'; |
| 5 |
|
| 6 |
export const DASHBOARD_BASE_URL = 'https://dash.parsely.com'; |
| 7 |
export const PUBLIC_API_BASE_URL = 'https://api.parsely.com/v2'; |
| 8 |
|
| 9 |
/** |
| 10 |
* The maximum length of a free-text (custom) tone or persona, in characters. |
| 11 |
* |
| 12 |
* @since 3.24.0 |
| 13 |
*/ |
| 14 |
export const MAX_CUSTOM_VALUE_LENGTH = 32; |
| 15 |
|
| 16 |
/** |
| 17 |
* The key denoting that a free-text (custom) tone or persona is in use. |
| 18 |
* |
| 19 |
* @since 3.24.0 |
| 20 |
*/ |
| 21 |
export const CUSTOM_VALUE = 'custom'; |
| 22 |
|
| 23 |
/** |
| 24 |
* The tone and persona shipped with the plugin, shared by the suggestion |
| 25 |
* features. Only a last resort: PHP injects the site's own defaults, which |
| 26 |
* take precedence. |
| 27 |
* |
| 28 |
* Kept in sync with DEFAULT_PERSONA and DEFAULT_TONE in Suggestion_Defaults. |
| 29 |
* |
| 30 |
* @since 3.24.0 |
| 31 |
*/ |
| 32 |
export const DEFAULT_PERSONA = 'journalist'; |
| 33 |
export const DEFAULT_TONE = 'neutral'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Periods that are available in Content Intelligence. |
| 37 |
* |
| 38 |
* @since 3.10.0 |
| 39 |
* @since 3.11.0 Moved to constants.ts. |
| 40 |
*/ |
| 41 |
export enum Period { |
| 42 |
Minutes10 = '10m', |
| 43 |
Hour = '1h', |
| 44 |
Hours2 = '2h', |
| 45 |
Hours4 = '4h', |
| 46 |
Hours24 = '24h', |
| 47 |
Days7 = '7d', |
| 48 |
Days30 = '30d' |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Metrics that are available in Content Intelligence. |
| 53 |
* |
| 54 |
* @since 3.10.0 |
| 55 |
* @since 3.11.0 Moved to constants.ts. |
| 56 |
*/ |
| 57 |
export enum Metric { |
| 58 |
Views = 'views', |
| 59 |
AvgEngaged = 'avg_engaged' |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Post filter types that are available in Content Intelligence. |
| 64 |
* |
| 65 |
* @since 3.11.0 |
| 66 |
*/ |
| 67 |
export enum PostFilterType { |
| 68 |
Author = 'author', |
| 69 |
Section = 'section', |
| 70 |
Tag = 'tag', |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Defines the structure of Post filters. |
| 75 |
* |
| 76 |
* @since 3.18.0 |
| 77 |
*/ |
| 78 |
export interface PostFilters { |
| 79 |
author: string; |
| 80 |
section: string; |
| 81 |
tags: string[]; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns whether the passed value is present in the given enum. |
| 86 |
* |
| 87 |
* @since 3.11.0 |
| 88 |
* |
| 89 |
* @param {string|number} value The value to check for. |
| 90 |
* @param {Object} enumObject The enum to check against. |
| 91 |
*/ |
| 92 |
export const isInEnum = <T extends object>( |
| 93 |
value: string|number, enumObject: T |
| 94 |
): boolean => { |
| 95 |
return Object.values( enumObject ).includes( value ); |
| 96 |
}; |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns a text description representing the passed period. |
| 100 |
* |
| 101 |
* @since 3.11.0 |
| 102 |
* |
| 103 |
* @param {Period} period The period for which to create the description. |
| 104 |
* @param {boolean} lowercase Whether to return the description in lowercase. |
| 105 |
* |
| 106 |
* @return {string} The period description. |
| 107 |
*/ |
| 108 |
export function getPeriodDescription( |
| 109 |
period: Period, lowercase: boolean = false |
| 110 |
): string { |
| 111 |
const timeValue = parseInt( period, 10 ); |
| 112 |
const timeUnit = period.charAt( period.length - 1 ); |
| 113 |
|
| 114 |
let description: string = __( 'Unknown Period', 'wp-parsely' ); |
| 115 |
|
| 116 |
/* eslint-disable @wordpress/valid-sprintf */ |
| 117 |
switch ( timeUnit ) { |
| 118 |
case 'm': |
| 119 |
if ( 1 === timeValue ) { |
| 120 |
description = __( 'Last Minute', 'wp-parsely' ); |
| 121 |
break; |
| 122 |
} |
| 123 |
description = sprintf( /* translators: 1: Number of minutes */ |
| 124 |
_n( 'Last %1$d Minute', 'Last %1$d Minutes', timeValue, 'wp-parsely' ), |
| 125 |
timeValue |
| 126 |
); |
| 127 |
break; |
| 128 |
case 'h': |
| 129 |
if ( 1 === timeValue ) { |
| 130 |
description = __( 'Last Hour', 'wp-parsely' ); |
| 131 |
break; |
| 132 |
} |
| 133 |
description = sprintf( /* translators: 1: Number of hours */ |
| 134 |
_n( 'Last %1$d Hour', 'Last %1$d Hours', timeValue, 'wp-parsely' ), |
| 135 |
timeValue |
| 136 |
); |
| 137 |
break; |
| 138 |
case 'd': |
| 139 |
if ( 1 === timeValue ) { |
| 140 |
description = __( 'Last Day', 'wp-parsely' ); |
| 141 |
break; |
| 142 |
} |
| 143 |
description = sprintf( /* translators: 1: Number of days */ |
| 144 |
_n( 'Last %1$d Day', 'Last %1$d Days', timeValue, 'wp-parsely' ), |
| 145 |
timeValue |
| 146 |
); |
| 147 |
break; |
| 148 |
} |
| 149 |
/* eslint-enable @wordpress/valid-sprintf */ |
| 150 |
|
| 151 |
if ( lowercase ) { |
| 152 |
return description.toLocaleLowerCase(); |
| 153 |
} |
| 154 |
|
| 155 |
return description; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns a text description representing the passed metric. |
| 160 |
* |
| 161 |
* @since 3.11.0 |
| 162 |
* |
| 163 |
* @param {Metric} metric The metric for which to create the description. |
| 164 |
* |
| 165 |
* @return {string} The metric description. |
| 166 |
*/ |
| 167 |
export function getMetricDescription( metric: Metric ): string { |
| 168 |
switch ( metric ) { |
| 169 |
case Metric.Views: |
| 170 |
return __( 'Page Views', 'wp-parsely' ); |
| 171 |
case Metric.AvgEngaged: |
| 172 |
return __( 'Avg. Time', 'wp-parsely' ); |
| 173 |
default: |
| 174 |
return __( 'Unknown Metric', 'wp-parsely' ); |
| 175 |
} |
| 176 |
} |
| 177 |
|