| 1 |
<?php |
| 2 |
/** |
| 3 |
* The two saved views added to the Pages screen (spec 054 FR-007). |
| 4 |
* |
| 5 |
* Only these two. The views originally asked for beyond them — outdated, |
| 6 |
* synchronized, client-approved — each need a per-item comparison against cloud |
| 7 |
* state that no capability provides today. They are v2, and are named in the |
| 8 |
* spec's Out of Scope so their absence reads as a decision. |
| 9 |
* |
| 10 |
* PHP 7.2 SYNTAX ONLY. |
| 11 |
* |
| 12 |
* @package Templately |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace Templately\Modules\SiteEditorViews; |
| 16 |
|
| 17 |
defined( 'ABSPATH' ) || exit; |
| 18 |
|
| 19 |
class Views { |
| 20 |
|
| 21 |
const SLUG_IMPORTED = 'templately-imported'; |
| 22 |
const SLUG_RECENT = 'templately-recent'; |
| 23 |
|
| 24 |
/** What "recently" means. A display convention, not retention (spec Assumptions). */ |
| 25 |
const RECENT_DAYS = 30; |
| 26 |
|
| 27 |
/** |
| 28 |
* The views to contribute, or an empty list. |
| 29 |
* |
| 30 |
* Empty when the site has no Templately-imported content: a site with none |
| 31 |
* must look exactly like a site without the plugin, and two views listing |
| 32 |
* nothing would advertise a feature that has nothing to show (FR-011). |
| 33 |
* |
| 34 |
* @return array<int, array<string, mixed>> |
| 35 |
*/ |
| 36 |
public function definitions(): array { |
| 37 |
if ( ! $this->site_has_imported_content() ) { |
| 38 |
return []; |
| 39 |
} |
| 40 |
|
| 41 |
// NOTE THE NESTING. A view_list entry is `{ title, slug, view }`, and the |
| 42 |
// filters belong INSIDE `view` — that is the host's own schema (see |
| 43 |
// class-wp-rest-view-config-controller.php, get_view_base_schema). Put |
| 44 |
// `filters` at the top level and the schema simply drops them: no error, |
| 45 |
// no warning, no filter parameter on the query, and a view that lists |
| 46 |
// everything. This shipped that way once, and the symptom was |
| 47 |
// indistinguishable from an unsupported operator. |
| 48 |
return [ |
| 49 |
[ |
| 50 |
'title' => __( 'Templately imported', 'templately' ), |
| 51 |
'slug' => self::SLUG_IMPORTED, |
| 52 |
'view' => [ |
| 53 |
'filters' => [ |
| 54 |
[ |
| 55 |
// `contains`, not `isNotEmpty`. The host's operator |
| 56 |
// vocabulary is fixed and `isNotEmpty` is not in it, |
| 57 |
// and naming an operator it lacks fails the same |
| 58 |
// silent way. Every provenance-carrying item's source |
| 59 |
// field starts with "Templately" and nothing else's |
| 60 |
// does, so `contains` selects exactly the right rows. |
| 61 |
'field' => Fields::FIELD_SOURCE, |
| 62 |
'operator' => 'contains', |
| 63 |
'value' => 'Templately', |
| 64 |
], |
| 65 |
], |
| 66 |
], |
| 67 |
], |
| 68 |
[ |
| 69 |
'title' => __( 'Recently imported', 'templately' ), |
| 70 |
'slug' => self::SLUG_RECENT, |
| 71 |
'view' => [ |
| 72 |
'filters' => [ |
| 73 |
[ |
| 74 |
'field' => Fields::FIELD_IMPORTED, |
| 75 |
'operator' => 'after', |
| 76 |
'value' => gmdate( 'c', time() - ( self::RECENT_DAYS * DAY_IN_SECONDS ) ), |
| 77 |
], |
| 78 |
], |
| 79 |
], |
| 80 |
], |
| 81 |
]; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Whether anything on this site carries provenance. |
| 86 |
* |
| 87 |
* This runs on a screen load, so it must cost at most one cached lookup and |
| 88 |
* must never issue a per-row query — an unbounded scan here is precisely the |
| 89 |
* performance defect SC-005 forbids. The answer is cached until an import |
| 90 |
* or a revert changes it. |
| 91 |
* |
| 92 |
* @return bool |
| 93 |
*/ |
| 94 |
protected function site_has_imported_content(): bool { |
| 95 |
$cached = get_transient( 'templately_has_imported_content' ); |
| 96 |
|
| 97 |
if ( false !== $cached ) { |
| 98 |
return '1' === $cached; |
| 99 |
} |
| 100 |
|
| 101 |
global $wpdb; |
| 102 |
|
| 103 |
// LIMIT 1 — the question is "any?", not "how many?". |
| 104 |
// |
| 105 |
// KEY_IMPORTED_AT, never KEY_SESSION: the pipeline has written a session |
| 106 |
// id since long before this feature existed, so asking about it would |
| 107 |
// answer "yes" on any site that ever imported, and offer views listing |
| 108 |
// content with nothing to show (see Provenance::has()). |
| 109 |
$found = $wpdb->get_var( |
| 110 |
$wpdb->prepare( |
| 111 |
"SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s LIMIT 1", |
| 112 |
Provenance::KEY_IMPORTED_AT |
| 113 |
) |
| 114 |
); |
| 115 |
|
| 116 |
$answer = null !== $found; |
| 117 |
|
| 118 |
set_transient( 'templately_has_imported_content', $answer ? '1' : '0', DAY_IN_SECONDS ); |
| 119 |
|
| 120 |
return $answer; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Forget the cached answer. |
| 125 |
* |
| 126 |
* Called after an import or a revert, both of which can change it. |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
public static function forget(): void { |
| 131 |
delete_transient( 'templately_has_imported_content' ); |
| 132 |
} |
| 133 |
} |
| 134 |
|