ads
1 week ago
groups
1 week ago
metaboxes
1 year ago
pages
1 day ago
placements
1 day ago
class-action-links.php
1 week ago
class-addon-box.php
1 day ago
class-addon-updater.php
1 day ago
class-admin-menu.php
1 day ago
class-admin-notices.php
1 year ago
class-ajax.php
1 day ago
class-app.php
1 day ago
class-assets.php
1 day ago
class-authors.php
1 year ago
class-edd-updater.php
1 day ago
class-license-admin-post.php
1 day ago
class-list-filters.php
1 week ago
class-marketing.php
1 year ago
class-metabox-ad-settings.php
1 year ago
class-metabox-ad.php
1 year ago
class-misc.php
1 week ago
class-page-quick-edit.php
1 day ago
class-plugin-auto-update.php
1 day ago
class-plugin-installer.php
1 day ago
class-post-list.php
1 year ago
class-post-types.php
1 week ago
class-screen-options.php
3 months ago
class-settings.php
1 day ago
class-shortcode-creator.php
1 year ago
class-system-info.php
1 year ago
class-tinymce.php
2 years ago
class-translation-promo.php
1 year ago
class-upgrades.php
1 year ago
class-version-control.php
3 months ago
class-welcome.php
1 year ago
class-wordpress-dashboard.php
1 year ago
index.php
2 years ago
class-plugin-auto-update.php
302 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Per-plugin auto-update preferences (advanced-ads-{addon}-autoupdate = on|off). |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 2.0.21 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Admin; |
| 11 | |
| 12 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 13 | use AdvancedAds\Utilities\Addons; |
| 14 | use WP_Error; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | /** |
| 19 | * Plugin auto-update option storage and WordPress auto_update_plugins sync. |
| 20 | */ |
| 21 | class Plugin_Auto_Update implements Integration_Interface { |
| 22 | |
| 23 | /** |
| 24 | * Admin hooks (Plugins screen sync). |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function hooks(): void { |
| 29 | add_action( 'admin_init', [ self::class, 'maybe_sync_on_admin' ], 20 ); |
| 30 | add_filter( 'site_option_auto_update_plugins', [ self::class, 'filter_site_auto_update_plugins' ] ); |
| 31 | } |
| 32 | |
| 33 | public const STATE_ON = 'on'; |
| 34 | public const STATE_OFF = 'off'; |
| 35 | |
| 36 | /** |
| 37 | * Main plugin key in state maps. |
| 38 | */ |
| 39 | public const MAIN_ADDON_KEY = 'main'; |
| 40 | |
| 41 | /** |
| 42 | * All known short add-on ids. |
| 43 | * |
| 44 | * @return string[] |
| 45 | */ |
| 46 | public static function known_addon_ids(): array { |
| 47 | return Addons::known_addon_ids(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Option name for an add-on or the main plugin. |
| 52 | * |
| 53 | * @param string|null $addon_id Short id, empty, null, or "main" for base plugin. |
| 54 | * @return string |
| 55 | */ |
| 56 | public static function option_name( ?string $addon_id = null ): string { |
| 57 | $slug_base = defined( 'ADVADS_SLUG' ) ? ADVADS_SLUG : 'advanced-ads'; |
| 58 | $addon_id = self::normalize_addon_id( $addon_id ); |
| 59 | |
| 60 | if ( null === $addon_id ) { |
| 61 | return $slug_base . '-autoupdate'; |
| 62 | } |
| 63 | |
| 64 | return $slug_base . '-' . $addon_id . '-autoupdate'; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Whether auto-updates are enabled for the given plugin. |
| 69 | * |
| 70 | * @param string|null $addon_id Short id or main. |
| 71 | * @return bool |
| 72 | */ |
| 73 | public static function is_enabled( ?string $addon_id = null ): bool { |
| 74 | return self::STATE_ON === self::get_state( $addon_id ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Stored state (on|off), default off. |
| 79 | * |
| 80 | * @param string|null $addon_id Short id or main. |
| 81 | * @return string |
| 82 | */ |
| 83 | public static function get_state( ?string $addon_id = null ): string { |
| 84 | $value = get_option( self::option_name( $addon_id ), self::STATE_OFF ); |
| 85 | |
| 86 | return self::STATE_ON === $value ? self::STATE_ON : self::STATE_OFF; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Persist state and sync WordPress auto_update_plugins when applicable. |
| 91 | * |
| 92 | * @param string|null $addon_id Short id or main. |
| 93 | * @param string $state on|off. |
| 94 | * @return true|WP_Error |
| 95 | */ |
| 96 | public static function set_state( ?string $addon_id, string $state ) { |
| 97 | $addon_id = self::normalize_addon_id( $addon_id ); |
| 98 | $state = strtolower( sanitize_text_field( $state ) ); |
| 99 | |
| 100 | if ( self::STATE_ON !== $state && self::STATE_OFF !== $state ) { |
| 101 | return new WP_Error( 'invalid_state', __( 'Invalid auto-update state.', 'advanced-ads' ) ); |
| 102 | } |
| 103 | |
| 104 | if ( null !== $addon_id && ! Addons::is_known_addon( $addon_id ) ) { |
| 105 | return new WP_Error( 'invalid_addon', __( 'Unknown add-on.', 'advanced-ads' ) ); |
| 106 | } |
| 107 | |
| 108 | update_option( self::option_name( $addon_id ), $state, false ); |
| 109 | self::sync_all_from_custom_options(); |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Rebuild WordPress auto_update_plugins from all advanced-ads-*-autoupdate options. |
| 116 | * |
| 117 | * @return void |
| 118 | */ |
| 119 | public static function sync_all_from_custom_options(): void { |
| 120 | $auto_updates = (array) get_site_option( 'auto_update_plugins', [] ); |
| 121 | $targets = array_merge( [ null ], self::known_addon_ids() ); |
| 122 | |
| 123 | foreach ( $targets as $addon_id ) { |
| 124 | $plugin_file = self::resolve_installed_plugin_file( $addon_id ); |
| 125 | if ( null === $plugin_file ) { |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | if ( self::is_enabled( $addon_id ) ) { |
| 130 | if ( ! in_array( $plugin_file, $auto_updates, true ) ) { |
| 131 | $auto_updates[] = $plugin_file; |
| 132 | } |
| 133 | } else { |
| 134 | $auto_updates = array_values( array_diff( $auto_updates, [ $plugin_file ] ) ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | $installed = array_keys( Addons::get_plugins() ); |
| 139 | if ( ! empty( $installed ) ) { |
| 140 | $auto_updates = array_values( |
| 141 | array_unique( array_intersect( $auto_updates, $installed ) ) |
| 142 | ); |
| 143 | // Re-apply enabled plugins that use a non-canonical basename key. |
| 144 | foreach ( $targets as $addon_id ) { |
| 145 | if ( ! self::is_enabled( $addon_id ) ) { |
| 146 | continue; |
| 147 | } |
| 148 | $plugin_file = self::resolve_installed_plugin_file( $addon_id ); |
| 149 | if ( null !== $plugin_file && ! in_array( $plugin_file, $auto_updates, true ) ) { |
| 150 | $auto_updates[] = $plugin_file; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | update_site_option( 'auto_update_plugins', $auto_updates ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Installed plugin basename for an add-on (matches get_plugins() keys). |
| 160 | * |
| 161 | * @param string|null $addon_id Short id or main. |
| 162 | * @return string|null |
| 163 | */ |
| 164 | public static function resolve_installed_plugin_file( ?string $addon_id ): ?string { |
| 165 | $addon_id = self::normalize_addon_id( $addon_id ); |
| 166 | |
| 167 | if ( null === $addon_id ) { |
| 168 | $candidate = self::plugin_file_for_addon_id( null ); |
| 169 | if ( null === $candidate || ! self::plugin_path_exists( null, $candidate ) ) { |
| 170 | return null; |
| 171 | } |
| 172 | |
| 173 | return $candidate; |
| 174 | } |
| 175 | |
| 176 | return Addons::resolve_installed_plugin_file( $addon_id ); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Merge custom options into the value WordPress reads for the Plugins screen. |
| 181 | * |
| 182 | * @param mixed $value Site option value. |
| 183 | * @return array<string> |
| 184 | */ |
| 185 | public static function filter_site_auto_update_plugins( $value ): array { |
| 186 | $value = is_array( $value ) ? $value : []; |
| 187 | $targets = array_merge( [ null ], self::known_addon_ids() ); |
| 188 | |
| 189 | foreach ( $targets as $addon_id ) { |
| 190 | $plugin_file = self::resolve_installed_plugin_file( $addon_id ); |
| 191 | if ( null === $plugin_file ) { |
| 192 | continue; |
| 193 | } |
| 194 | |
| 195 | if ( self::is_enabled( $addon_id ) ) { |
| 196 | if ( ! in_array( $plugin_file, $value, true ) ) { |
| 197 | $value[] = $plugin_file; |
| 198 | } |
| 199 | } else { |
| 200 | $value = array_values( array_diff( $value, [ $plugin_file ] ) ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return array_values( array_unique( $value ) ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Persist auto_update_plugins when an admin with update_plugins loads wp-admin. |
| 209 | * |
| 210 | * @return void |
| 211 | */ |
| 212 | public static function maybe_sync_on_admin(): void { |
| 213 | if ( ! is_admin() || ! current_user_can( 'update_plugins' ) ) { |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | self::sync_all_from_custom_options(); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Map of addon id (or "main") => on|off. |
| 222 | * |
| 223 | * @param string[] $addon_ids Short ids to include. |
| 224 | * @return array<string, string> |
| 225 | */ |
| 226 | public static function get_states_for_addon_ids( array $addon_ids ): array { |
| 227 | $states = [ |
| 228 | self::MAIN_ADDON_KEY => self::get_state( null ), |
| 229 | ]; |
| 230 | |
| 231 | foreach ( $addon_ids as $addon_id ) { |
| 232 | $addon_id = sanitize_key( (string) $addon_id ); |
| 233 | if ( '' === $addon_id || ! Addons::is_known_addon( $addon_id ) ) { |
| 234 | continue; |
| 235 | } |
| 236 | $states[ $addon_id ] = self::get_state( $addon_id ); |
| 237 | } |
| 238 | |
| 239 | return $states; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Full map for all known add-ons plus main. |
| 244 | * |
| 245 | * @return array<string, string> |
| 246 | */ |
| 247 | public static function get_all_states(): array { |
| 248 | return self::get_states_for_addon_ids( self::known_addon_ids() ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Plugin basename relative to wp-content/plugins. |
| 253 | * |
| 254 | * @param string|null $addon_id Short id or main. |
| 255 | * @return string|null |
| 256 | */ |
| 257 | public static function plugin_file_for_addon_id( ?string $addon_id ): ?string { |
| 258 | $addon_id = self::normalize_addon_id( $addon_id ); |
| 259 | |
| 260 | if ( null === $addon_id ) { |
| 261 | if ( defined( 'ADVADS_FILE' ) ) { |
| 262 | return basename( dirname( ADVADS_FILE ) ) . '/' . basename( ADVADS_FILE ); |
| 263 | } |
| 264 | |
| 265 | return 'advanced-ads/advanced-ads.php'; |
| 266 | } |
| 267 | |
| 268 | return Addons::plugin_file( $addon_id ); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Normalize the addon ID |
| 273 | * |
| 274 | * @param string|null $addon_id Raw id from request. |
| 275 | * @return string|null Normalized short id or null for main plugin. |
| 276 | */ |
| 277 | private static function normalize_addon_id( ?string $addon_id ): ?string { |
| 278 | $addon_id = sanitize_key( (string) $addon_id ); |
| 279 | |
| 280 | if ( '' === $addon_id || self::MAIN_ADDON_KEY === $addon_id ) { |
| 281 | return null; |
| 282 | } |
| 283 | |
| 284 | return $addon_id; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Whether the plugin bootstrap file exists on disk. |
| 289 | * |
| 290 | * @param string|null $addon_id Normalized id or null for main. |
| 291 | * @param string $plugin_file Plugins-relative path. |
| 292 | * @return bool |
| 293 | */ |
| 294 | private static function plugin_path_exists( ?string $addon_id, string $plugin_file ): bool { |
| 295 | if ( null === $addon_id && defined( 'ADVADS_FILE' ) ) { |
| 296 | return file_exists( ADVADS_FILE ); |
| 297 | } |
| 298 | |
| 299 | return file_exists( wp_normalize_path( WP_PLUGIN_DIR . '/' . $plugin_file ) ); |
| 300 | } |
| 301 | } |
| 302 |