| 1 |
<?php |
| 2 |
/** |
| 3 |
* The closed set of pro plugins Templately may obtain on a user's behalf. |
| 4 |
* |
| 5 |
* Two entries, hard-coded. This is deliberately NOT configurable and deliberately NOT |
| 6 |
* something the cloud can widen: the question it answers is "which plugin archives may we |
| 7 |
* download from a third party and execute on this site", and a dependency descriptor that |
| 8 |
* merely CLAIMS to be provisionable is not evidence. A compromised or spoofed dependency |
| 9 |
* response could otherwise induce a download-and-activate of anything. |
| 10 |
* |
| 11 |
* The client-side twin lives in `assets/js/isProvisionable.ts` and lists the same |
| 12 |
* plugin files. `tests/unit/test-CatalogParity.php` fails if the two drift, because a |
| 13 |
* drift means the UI offers a checkbox for something the backend will refuse — the |
| 14 |
* confusing half-state the fallback rules exist to avoid. |
| 15 |
* |
| 16 |
* @package Templately\Modules\ProPluginProvisioning |
| 17 |
*/ |
| 18 |
|
| 19 |
namespace Templately\Modules\ProPluginProvisioning; |
| 20 |
|
| 21 |
class Catalog { |
| 22 |
|
| 23 |
/** |
| 24 |
* Provisionable plugins, keyed by their WordPress plugin file. |
| 25 |
* |
| 26 |
* `platform` is the value the download service's `platform` parameter takes, and it is |
| 27 |
* read from HERE rather than from the import request — the request's platform describes |
| 28 |
* the pack, this describes the plugin, and only the second is safe to put in a URL. |
| 29 |
* |
| 30 |
* `free_slug` records which free edition must be present first. Ordering is enforced by |
| 31 |
* the full-site dependency runner's own sort, not by this table; the column exists so |
| 32 |
* the relationship is written down once instead of inferred from a sort array. |
| 33 |
*/ |
| 34 |
const PLUGINS = [ |
| 35 |
'essential-addons-elementor/essential_adons_elementor.php' => [ |
| 36 |
'slug' => 'essential-addons-elementor', |
| 37 |
'plugin_file' => 'essential-addons-elementor/essential_adons_elementor.php', |
| 38 |
'platform' => 'elementor', |
| 39 |
'name' => 'Essential Addons Pro', |
| 40 |
'main_file' => 'essential_adons_elementor.php', |
| 41 |
'free_slug' => 'essential-addons-for-elementor-lite', |
| 42 |
], |
| 43 |
'essential-blocks-pro/essential-blocks-pro.php' => [ |
| 44 |
'slug' => 'essential-blocks-pro', |
| 45 |
'plugin_file' => 'essential-blocks-pro/essential-blocks-pro.php', |
| 46 |
'platform' => 'gutenberg', |
| 47 |
'name' => 'Essential Blocks Pro', |
| 48 |
'main_file' => 'essential-blocks-pro.php', |
| 49 |
'free_slug' => 'essential-blocks', |
| 50 |
], |
| 51 |
]; |
| 52 |
|
| 53 |
/** |
| 54 |
* Resolve a dependency descriptor to its catalog entry, or null. |
| 55 |
* |
| 56 |
* Matches on `plugin_file` FIRST and `slug` only as a fallback: a slug can be reused by |
| 57 |
* an unrelated plugin, a plugin file cannot. A descriptor whose slug matches but whose |
| 58 |
* plugin file does not is NOT a match — that combination is exactly what a spoofed |
| 59 |
* entry would look like. |
| 60 |
* |
| 61 |
* @param array $dependency Dependency descriptor (`plugin_file`, `slug`, `plugin_original_slug`). |
| 62 |
* @return array|null |
| 63 |
*/ |
| 64 |
public static function find( array $dependency ) { |
| 65 |
$plugin_file = isset( $dependency['plugin_file'] ) && is_string( $dependency['plugin_file'] ) |
| 66 |
? $dependency['plugin_file'] |
| 67 |
: ''; |
| 68 |
|
| 69 |
if ( '' !== $plugin_file && isset( self::PLUGINS[ $plugin_file ] ) ) { |
| 70 |
return self::PLUGINS[ $plugin_file ]; |
| 71 |
} |
| 72 |
|
| 73 |
// A descriptor that names a plugin file we do not know is not a match, even if its |
| 74 |
// slug is one of ours — the file is the identity. |
| 75 |
if ( '' !== $plugin_file ) { |
| 76 |
return null; |
| 77 |
} |
| 78 |
|
| 79 |
foreach ( [ 'slug', 'plugin_original_slug' ] as $key ) { |
| 80 |
$slug = isset( $dependency[ $key ] ) && is_string( $dependency[ $key ] ) ? $dependency[ $key ] : ''; |
| 81 |
|
| 82 |
if ( '' === $slug ) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
|
| 86 |
foreach ( self::PLUGINS as $entry ) { |
| 87 |
if ( $entry['slug'] === $slug ) { |
| 88 |
return $entry; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Whether this dependency is one Templately may obtain. |
| 98 |
* |
| 99 |
* @param array $dependency Dependency descriptor. |
| 100 |
* @return bool |
| 101 |
*/ |
| 102 |
public static function is_provisionable( array $dependency ): bool { |
| 103 |
return null !== self::find( $dependency ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Every provisionable plugin file, for parity checks and tests. |
| 108 |
* |
| 109 |
* @return string[] |
| 110 |
*/ |
| 111 |
public static function plugin_files(): array { |
| 112 |
return array_keys( self::PLUGINS ); |
| 113 |
} |
| 114 |
} |
| 115 |
|