class-ad-list-stats.php
1 week ago
class-addons.php
1 day ago
class-cache.php
1 week ago
class-conditional.php
1 day ago
class-content-injection.php
1 year ago
class-data.php
1 day ago
class-sanitize.php
1 year ago
class-testing.php
1 year ago
class-validation.php
1 day ago
class-wordpress.php
1 day ago
index.php
2 years ago
class-addons.php
240 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Paid add-on registry and installed-plugin discovery. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 2.0.24 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Utilities; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Canonical add-on paths and shared get_plugins() lookups. |
| 16 | */ |
| 17 | final class Addons { |
| 18 | |
| 19 | /** |
| 20 | * Plugin bootstrap paths keyed by short add-on id. |
| 21 | * |
| 22 | * @var array<string, string> |
| 23 | */ |
| 24 | private const PLUGIN_FILES = [ |
| 25 | 'pro' => 'advanced-ads-pro/advanced-ads-pro.php', |
| 26 | 'responsive' => 'advanced-ads-responsive/responsive-ads.php', |
| 27 | 'gam' => 'advanced-ads-gam/advanced-ads-gam.php', |
| 28 | 'layer' => 'advanced-ads-layer/layer-ads.php', |
| 29 | 'selling' => 'advanced-ads-selling/advanced-ads-selling.php', |
| 30 | 'sticky' => 'advanced-ads-sticky-ads/sticky-ads.php', |
| 31 | 'tracking' => 'advanced-ads-tracking/tracking.php', |
| 32 | 'slider-ads' => 'advanced-ads-slider/slider.php', |
| 33 | ]; |
| 34 | |
| 35 | /** |
| 36 | * Cached get_plugins() result for the current request. |
| 37 | * |
| 38 | * @var array<string, array>|null |
| 39 | */ |
| 40 | private static $plugins = null; |
| 41 | |
| 42 | /** |
| 43 | * Canonical plugin bootstrap paths for paid add-ons. |
| 44 | * |
| 45 | * @return array<string, string> Short add-on id => plugins-relative bootstrap path. |
| 46 | */ |
| 47 | public static function plugin_files(): array { |
| 48 | return self::PLUGIN_FILES; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * All known paid add-on short ids. |
| 53 | * |
| 54 | * @return string[] |
| 55 | */ |
| 56 | public static function known_addon_ids(): array { |
| 57 | return array_keys( self::PLUGIN_FILES ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Whether the short id belongs to a paid add-on in the catalog. |
| 62 | * |
| 63 | * @param string $addon_id Short add-on id. |
| 64 | * |
| 65 | * @return bool |
| 66 | */ |
| 67 | public static function is_known_addon( string $addon_id ): bool { |
| 68 | return isset( self::PLUGIN_FILES[ sanitize_key( $addon_id ) ] ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Canonical bootstrap path for a catalog add-on. |
| 73 | * |
| 74 | * @param string $addon_id Short add-on id. |
| 75 | * |
| 76 | * @return string|null Plugins-relative bootstrap path, or null when unknown. |
| 77 | */ |
| 78 | public static function plugin_file( string $addon_id ): ?string { |
| 79 | $addon_id = sanitize_key( $addon_id ); |
| 80 | |
| 81 | return self::PLUGIN_FILES[ $addon_id ] ?? null; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Installed plugins from WordPress. |
| 86 | * |
| 87 | * @param bool $refresh When true, bust the plugins object cache first. |
| 88 | * |
| 89 | * @return array<string, array> Plugin bootstrap path => plugin header data. |
| 90 | */ |
| 91 | public static function get_plugins( bool $refresh = false ): array { |
| 92 | if ( $refresh ) { |
| 93 | self::$plugins = null; |
| 94 | wp_cache_delete( 'plugins', 'plugins' ); |
| 95 | } |
| 96 | |
| 97 | if ( null !== self::$plugins ) { |
| 98 | return self::$plugins; |
| 99 | } |
| 100 | |
| 101 | if ( ! function_exists( 'get_plugins' ) ) { |
| 102 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 103 | } |
| 104 | |
| 105 | self::$plugins = get_plugins(); |
| 106 | |
| 107 | return self::$plugins; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Installed plugins indexed by TextDomain. |
| 112 | * |
| 113 | * @param bool $refresh When true, bust the plugins object cache first. |
| 114 | * |
| 115 | * @return array<string, array{file: string, version: string}> |
| 116 | */ |
| 117 | public static function get_plugins_by_text_domain( bool $refresh = false ): array { |
| 118 | $normalized = []; |
| 119 | |
| 120 | foreach ( self::get_plugins( $refresh ) as $plugin_file => $plugin_data ) { |
| 121 | $text_domain = $plugin_data['TextDomain'] ?? ''; |
| 122 | if ( '' === $text_domain ) { |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | $normalized[ $text_domain ] = [ |
| 127 | 'file' => $plugin_file, |
| 128 | 'version' => $plugin_data['Version'] ?? '0.0.1', |
| 129 | ]; |
| 130 | } |
| 131 | |
| 132 | return $normalized; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Resolve the installed bootstrap path for a catalog add-on. |
| 137 | * |
| 138 | * Falls back to TextDomain and plugin folder matching when the canonical path differs. |
| 139 | * |
| 140 | * @param string $addon_id Short add-on id. |
| 141 | * |
| 142 | * @return string|null Plugins-relative bootstrap path when installed, otherwise null. |
| 143 | */ |
| 144 | public static function resolve_installed_plugin_file( string $addon_id ): ?string { |
| 145 | $addon_id = sanitize_key( $addon_id ); |
| 146 | $file = self::plugin_file( $addon_id ); |
| 147 | if ( null === $file ) { |
| 148 | return null; |
| 149 | } |
| 150 | |
| 151 | $plugins = self::get_plugins(); |
| 152 | if ( isset( $plugins[ $file ] ) ) { |
| 153 | return $file; |
| 154 | } |
| 155 | |
| 156 | $text_domain = self::text_domain( $addon_id ); |
| 157 | foreach ( $plugins as $plugin_file => $plugin_data ) { |
| 158 | if ( ( $plugin_data['TextDomain'] ?? '' ) === $text_domain ) { |
| 159 | return $plugin_file; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | $folder = dirname( $file ); |
| 164 | foreach ( array_keys( $plugins ) as $plugin_file ) { |
| 165 | if ( dirname( $plugin_file ) === $folder ) { |
| 166 | return $plugin_file; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return file_exists( WP_PLUGIN_DIR . '/' . $file ) ? $file : null; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Whether a catalog add-on is present on disk. |
| 175 | * |
| 176 | * @param string $addon_id Short add-on id. |
| 177 | * |
| 178 | * @return bool |
| 179 | */ |
| 180 | public static function is_addon_on_disk( string $addon_id ): bool { |
| 181 | return null !== self::resolve_installed_plugin_file( $addon_id ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Installed paid add-ons with metadata for admin and license UIs. |
| 186 | * |
| 187 | * @return array<string, array{id: string, name: string, version: string, path: string, options_slug: string, uri: string}> TextDomain => add-on data. |
| 188 | */ |
| 189 | public static function get_installed_addons(): array { |
| 190 | $installed = []; |
| 191 | |
| 192 | foreach ( self::get_plugins() as $plugin_file => $plugin_data ) { |
| 193 | $text_domain = $plugin_data['TextDomain'] ?? ''; |
| 194 | $addon_id = self::addon_id_for_text_domain( $text_domain ); |
| 195 | if ( null === $addon_id ) { |
| 196 | continue; |
| 197 | } |
| 198 | |
| 199 | $installed[ $text_domain ] = [ |
| 200 | 'id' => $addon_id, |
| 201 | 'name' => str_replace( [ '– ', 'Advanced Ads ' ], '', $plugin_data['Name'] ), |
| 202 | 'version' => $plugin_data['Version'] ?? '0.0.1', |
| 203 | 'path' => $plugin_file, |
| 204 | 'options_slug' => $text_domain, |
| 205 | 'uri' => $plugin_data['PluginURI'] ?? 'https://wpadvancedads.com', |
| 206 | ]; |
| 207 | } |
| 208 | |
| 209 | return $installed; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Expected TextDomain for a catalog add-on id. |
| 214 | * |
| 215 | * @param string $addon_id Short add-on id. |
| 216 | * |
| 217 | * @return string |
| 218 | */ |
| 219 | private static function text_domain( string $addon_id ): string { |
| 220 | return 'slider-ads' === $addon_id ? 'slider-ads' : 'advanced-ads-' . $addon_id; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Map an installed plugin TextDomain back to a catalog add-on id. |
| 225 | * |
| 226 | * @param string $text_domain Plugin TextDomain. |
| 227 | * |
| 228 | * @return string|null Short add-on id when recognized, otherwise null. |
| 229 | */ |
| 230 | private static function addon_id_for_text_domain( string $text_domain ): ?string { |
| 231 | foreach ( self::PLUGIN_FILES as $addon_id => $file ) { |
| 232 | if ( self::text_domain( $addon_id ) === $text_domain ) { |
| 233 | return $addon_id; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return null; |
| 238 | } |
| 239 | } |
| 240 |