| 1 |
<?php |
| 2 |
/** |
| 3 |
* The three provenance fields offered to the Pages screen (spec 054 FR-006). |
| 4 |
* |
| 5 |
* Field ids are namespaced `templately_*` deliberately. The host applies our |
| 6 |
* patch by MERGING it key by key, so an id that collided with a core field id |
| 7 |
* would silently redefine someone else's column rather than adding ours. |
| 8 |
* |
| 9 |
* PHP 7.2 SYNTAX ONLY. |
| 10 |
* |
| 11 |
* @package Templately |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace Templately\Modules\SiteEditorViews; |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
class Fields { |
| 19 |
|
| 20 |
const FIELD_SOURCE = 'templately_source'; |
| 21 |
const FIELD_IMPORTED = 'templately_imported'; |
| 22 |
const FIELD_ORIGIN = 'templately_origin'; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var Provenance |
| 26 |
*/ |
| 27 |
protected $provenance; |
| 28 |
|
| 29 |
public function __construct( Provenance $provenance ) { |
| 30 |
$this->provenance = $provenance; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* The field ids, in the order they should appear. |
| 35 |
* |
| 36 |
* @return string[] |
| 37 |
*/ |
| 38 |
public function ids(): array { |
| 39 |
return [ self::FIELD_SOURCE, self::FIELD_IMPORTED, self::FIELD_ORIGIN ]; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* What to show for one item. |
| 44 |
* |
| 45 |
* Every value derives from the provenance record the screen has already |
| 46 |
* fetched — no lookup per row, which is FR-013 and the reason SC-005 holds. |
| 47 |
* |
| 48 |
* @param int $id |
| 49 |
* @param string $entity |
| 50 |
* @return array<string, string> Field id => display value. |
| 51 |
*/ |
| 52 |
public function values_for( $id, $entity = 'post' ): array { |
| 53 |
if ( ! $this->provenance->has( $id, $entity ) ) { |
| 54 |
// Not ours. Empty, not "unknown" — a page someone wrote themselves |
| 55 |
// should look untouched, not mislabelled (FR-011, US1 scenario 2). |
| 56 |
return [ |
| 57 |
self::FIELD_SOURCE => '', |
| 58 |
self::FIELD_IMPORTED => '', |
| 59 |
self::FIELD_ORIGIN => '', |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
return [ |
| 64 |
self::FIELD_SOURCE => $this->source_label( $this->provenance->read( $id, Provenance::KEY_PACK, $entity ) ), |
| 65 |
self::FIELD_IMPORTED => $this->provenance->read( $id, Provenance::KEY_IMPORTED_AT, $entity ), |
| 66 |
self::FIELD_ORIGIN => $this->origin_label( $this->provenance->read( $id, Provenance::KEY_SOURCE, $entity ) ), |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* "Templately", plus the pack when we can name it. |
| 72 |
* |
| 73 |
* The pack name can be unresolvable — an offline site, a pack since |
| 74 |
* withdrawn. Then the field still says Templately rather than showing a bare |
| 75 |
* identifier or an error, because "this came from Templately" is the useful |
| 76 |
* part and the id means nothing to the reader (FR-012). |
| 77 |
* |
| 78 |
* @param string $pack_id |
| 79 |
* @return string |
| 80 |
*/ |
| 81 |
public function source_label( $pack_id ): string { |
| 82 |
$name = ''; |
| 83 |
|
| 84 |
if ( '' !== (string) $pack_id ) { |
| 85 |
/** |
| 86 |
* Filters the human-readable name for a pack id. |
| 87 |
* |
| 88 |
* @since 3.8.0 |
| 89 |
* |
| 90 |
* @param string $name |
| 91 |
* @param string $pack_id |
| 92 |
*/ |
| 93 |
$name = (string) apply_filters( 'templately_provenance_pack_name', '', $pack_id ); |
| 94 |
} |
| 95 |
|
| 96 |
if ( '' === $name ) { |
| 97 |
return __( 'Templately', 'templately' ); |
| 98 |
} |
| 99 |
|
| 100 |
/* translators: %s: the name of the Templately pack the content came from. */ |
| 101 |
return sprintf( __( 'Templately — %s', 'templately' ), $name ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Generated, or curated. |
| 106 |
* |
| 107 |
* `cloud` and `curated` are both curated as far as a reader is concerned; |
| 108 |
* only `ai` is generated. |
| 109 |
* |
| 110 |
* @param string $source |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public function origin_label( $source ): string { |
| 114 |
if ( '' === (string) $source ) { |
| 115 |
return ''; |
| 116 |
} |
| 117 |
|
| 118 |
return Provenance::SOURCE_AI === $source |
| 119 |
? __( 'AI-generated', 'templately' ) |
| 120 |
: __( 'Curated', 'templately' ); |
| 121 |
} |
| 122 |
} |
| 123 |
|