PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.25
Advanced Ads – Ad Manager & AdSense v2.0.25
2.0.25 2.0.24 2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 1.41.0 1.42.0 1.42.1 1.42.2 1.43.0 1.44.0 1.44.1 1.45.0 1.45.1 1.46.0 1.47.0 1.47.1 1.47.2 1.47.3 1.47.4 1.47.5 1.48.0 1.48.1 1.49.0 1.5.0 1.5.0.1 1.5.1 1.5.2 1.5.2.1 1.5.4 1.5.4.1 1.5.5 1.50.0 1.51.0 1.51.1 1.51.2 1.51.3 1.52.0 1.52.1 1.52.2 1.52.3 1.52.4 1.53.0 1.53.1 1.53.2 1.54.0 1.54.1 1.55.0 1.56.0 1.56.1 1.56.2 1.56.3 1.56.4 1.6 1.6.1 1.6.10 1.6.10.1 1.6.10.2 1.6.11 1.6.11.1 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.17.1 1.6.17.2 1.6.2 1.6.2.1 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.6.1 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.6.8.3 1.6.9 1.6.9.1 1.6.9.2 1.6.9.3 1.6.9.4 1.7 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.1.1 1.7.1.2 1.7.1.3 1.7.1.4 1.7.1.5 1.7.10 trunk 1.7.11 1.0.1 1.7.12 1.0.2 1.7.13 1.0.3 1.7.14 1.1.0 1.7.15 1.1.1 1.7.16 1.1.2 1.7.17 1.1.3 1.7.18 1.10 1.7.19 1.10.1 1.7.2 1.10.10 1.7.2.1 1.10.11 1.7.20 1.10.12 1.7.21 1.10.2 1.7.22 1.10.3 1.7.23 1.10.4 1.7.24 1.10.5 1.7.25 1.10.6 1.7.3 1.10.7 1.7.4 1.10.8 1.7.4.1 1.10.9 1.7.4.2 1.11 1.7.4.3 1.11.1 1.7.4.4 1.11.2 1.7.4.5 1.12 1.7.5 1.13 1.7.5.1 1.13.1 1.7.6 1.13.2 1.7.7 1.13.3 1.7.8 1.13.4 1.7.9 1.13.5 1.7.9.1 1.13.6 1.7.9.2 1.13.7 1.7.9.3 1.13.8 1.8 1.14 1.8.1 1.14.1 1.8.10 1.14.10 1.8.11 1.14.11 1.8.12 1.14.2 1.8.13 1.14.3 1.8.14 1.14.4 1.8.15 1.14.5 1.8.16 1.14.6 1.8.17 1.14.7 1.8.18 1.14.8 1.8.19 1.14.9 1.8.2 1.15 1.8.20 1.16 1.8.21 1.16.1 1.8.22 1.17 1.8.23 1.17.1 1.8.24 1.17.10 1.8.25 1.17.10-rc.1 1.8.26 1.17.11 1.8.27 1.17.12 1.8.28 1.17.12-rc.1 1.8.29 1.17.2 1.8.3 1.17.3 1.8.30 1.17.4 1.8.4 1.17.5 1.8.5 1.17.6 1.8.6 1.17.7 1.8.7 1.17.8 1.8.8 1.17.9 1.8.9 1.17.9-beta.1 1.9 1.18.0 2.0.0 1.19.0 2.0.1 1.19.1 2.0.10 1.2 2.0.11 1.2.1 2.0.12 1.2.2 2.0.13 1.2.3 2.0.14 1.2.4 2.0.15 1.2.5 2.0.16 1.2.6 2.0.17 1.2.7 2.0.18 1.20.0 2.0.19 1.20.0-rc.1 2.0.2 1.20.0-rc.2 2.0.20 1.20.1 2.0.3 1.20.2 2.0.4 1.20.3 2.0.5 1.21.0 2.0.6 1.21.1 2.0.7 1.22.0 2.0.8 1.22.1 2.0.9 1.22.2 1.23.0 1.23.1 1.23.2 1.24.0 1.24.1 1.24.2 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.29.0 1.29.1 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.30.0 1.30.1 1.30.2 1.30.2-rc.1 1.30.3 1.30.4 1.30.4-rc.1 1.30.5 1.31.0 1.31.1 1.32.0 1.32.0-rc.1 1.33.0 1.33.1 1.33.2 1.34.0 1.35.0 1.35.1 1.36.0 1.36.1 1.36.2 1.36.3 1.37.0 1.37.1 1.37.2
advanced-ads / includes / admin / class-plugin-auto-update.php
advanced-ads / includes / admin Last commit date
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