PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.24
Advanced Ads – Ad Manager & AdSense v2.0.24
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 / modules / ad-blocker / admin / admin.php
advanced-ads / modules / ad-blocker / admin Last commit date
views 1 year ago admin.php 1 year ago
admin.php
557 lines
1 <?php // phpcs:ignoreFile
2 /**
3 * Ad blocker admin functionality.
4 */
5 class Advanced_Ads_Ad_Blocker_Admin {
6 /**
7 * Singleton instance of the plugin
8 *
9 * @var Advanced_Ads_Ad_Blocker_Admin
10 */
11 protected static $instance;
12
13 /**
14 * Module options
15 *
16 * @var array (if loaded)
17 */
18 protected $options;
19
20 /**
21 * Pattern to search assets using preg_match. The string ends with .css/.js/.png/.gif
22 *
23 * @var string
24 */
25 protected $search_file_pattern = '/(css|js|png|gif)$/';
26
27 /**
28 * Pattern to exclide directories from search. The string does not contain '/vendor/' or '/lib/' or '/admin/' or /node_modules/
29 *
30 * @var string
31 */
32 protected $exclude_dir_pattern = '/(\/vendor\/|\/lib\/|\/admin\/|\/node_modules\/)/';
33
34 /**
35 * Array, containing path information on the currently configured uploads directory
36 *
37 * @var array
38 */
39 protected $upload_dir;
40
41 /**
42 * Error messages for user
43 *
44 * @var WP_Error
45 */
46 protected $error_messages;
47
48 /**
49 * Initialize the module
50 *
51 */
52 private function __construct() {
53 // add module settings to Advanced Ads settings page
54 add_action( 'advanced-ads-settings-init', [ $this, 'settings_init' ], 9 );
55
56 $is_main_site = is_main_site( get_current_blog_id() );
57 if ( ! $is_main_site ) {
58 return;
59 }
60
61 // Get the most recent options values
62 $this->options = Advanced_Ads_Ad_Blocker::get_instance()->options();
63 $this->upload_dir = $this->options['upload_dir'];
64
65 add_action( 'admin_init', [ $this, 'process_auto_update' ] );
66
67 $this->error_messages = new WP_Error();
68 }
69
70 /**
71 * Return an instance of Advanced_Ads_Ad_Blocker
72 *
73 * @return Advanced_Ads_Ad_Blocker_Admin
74 */
75 public static function get_instance() {
76 // If the single instance hasn't been set, set it now.
77 if (null === self::$instance)
78 {
79 self::$instance = new self;
80 }
81
82 return self::$instance;
83 }
84
85 /**
86 * Add settings to settings page.
87 */
88 public function settings_init() {
89 add_settings_field(
90 'use-adblocker',
91 __( 'Ad blocker disguise', 'advanced-ads' ),
92 [ $this, 'render_settings_use_adblocker' ],
93 ADVADS_SETTINGS_ADBLOCKER,
94 'advanced_ads_adblocker_setting_section'
95 );
96 }
97
98 /**
99 * Render setting to enable/disable 'adblocker disguise'.
100 */
101 public function render_settings_use_adblocker() {
102 $is_main_site = is_main_site( get_current_blog_id() );
103 $checked = ! empty( Advanced_Ads::get_instance()->get_adblocker_options()['use-adblocker'] );
104
105 include ADVADS_AB_BASE_PATH . 'admin/views/setting-use-adblocker.php';
106
107 // if this is a sub site in a network, don't run the rebuild form code.
108 if ( ! $is_main_site ) {
109 return;
110 }
111
112 // add the rebuild form directly after the settings
113 ?>
114 <div id="advads-adblocker-wrapper" <?php echo( $checked ? '' : 'style="display: none;"' ); ?>>
115 <?php
116 $button_disabled = true;
117 $upload_dir = $this->upload_dir;
118 $options = $this->options;
119
120 include ADVADS_AB_BASE_PATH . 'admin/views/rebuild_form.php';
121 ?>
122 </div>
123 <?php
124 }
125
126 /**
127 * Render the ad-blocker rebuild assets form
128 *
129 */
130 public function add_asset_rebuild_form() {
131 global $wp_filesystem;
132 $success = false;
133 $message = '';
134
135 $fs_connect = Advanced_Ads_Filesystem::get_instance()->fs_connect( $this->upload_dir['basedir'] );
136
137 if ( $fs_connect === false || is_wp_error( $fs_connect ) ) {
138 $message = __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'advanced-ads' );
139
140 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
141 $message = esc_html( $wp_filesystem->errors->get_error_message() );
142 }
143 if ( is_wp_error( $fs_connect ) && $fs_connect->get_error_code() ) {
144 $message = esc_html( $fs_connect->get_error_message() );
145 }
146 } else {
147 $output = $this->process_form();
148 if ( is_wp_error( $output ) ) {
149 $message = $output->get_error_message();
150 } else {
151 $success = true;
152 $message = __( 'The asset folder was rebuilt successfully', 'advanced-ads' );
153 }
154 }
155
156 $upload_dir = $this->upload_dir;
157 $button_disabled = false;
158 $options = Advanced_Ads_Ad_Blocker::get_instance()->options( true );
159
160 include ADVADS_AB_BASE_PATH . 'admin/views/rebuild_form.php';
161 }
162
163 /**
164 * Perform processing of the rebuild_form, sent by user
165 *
166 * @return true|WP_Error true on success, WP_Error in case of error
167 **/
168 private function process_form() {
169 // at this point we do not need ftp/ssh credentials anymore
170 $form_post_fields = array_intersect_key( $_POST, [ 'advads_ab_assign_new_folder' => false ] );
171
172 $this->create_dummy_plugin( $form_post_fields );
173
174 if ( $error_messages = $this->error_messages->get_error_messages() ) {
175 foreach ( $error_messages as $error_message ) {
176 Advanced_Ads::log( __METHOD__ . ': ' . $error_message );
177 }
178
179 return $this->error_messages;
180 }
181
182 return true;
183 }
184
185 /**
186 * Creates dummy plugin and return new options, that need to be stored in database.
187 *
188 * @param array $form_post_fields options, POST data sent by user.
189 * @return array $new_options - options, that need to be stored in database.
190 */
191 public function create_dummy_plugin( $form_post_fields = [] ) {
192 global $wp_filesystem;
193
194 $need_assign_new_name = isset( $form_post_fields['advads_ab_assign_new_folder'] );
195
196 if ( ! $this->upload_dir ) {
197 $message = __( 'There is no writable upload folder', 'advanced-ads' );
198 $this->error_messages->add( 'create_dummy_1', $message);
199 return false;
200 }
201
202 $new_options = [
203 'lookup_table' => isset( $this->options['lookup_table'] ) ? $this->options['lookup_table'] : [],
204 ];
205 $new_options_error = $new_options;
206 // $new_options_error does not have the 'module_can_work' key - ad-blocker script will be inactive and the asset folder will be rebuilt next time
207 $new_options['module_can_work'] = true;
208
209 $existing_files = @scandir( $this->upload_dir['basedir'] );
210 if ( $existing_files ) {
211 $existing_files = array_diff( $existing_files, [ '..', '.' ] );
212 } else {
213 $existing_files = [];
214 }
215
216 if ( ! empty( $this->options['folder_name'] ) ) {
217 $new_options['folder_name'] = $new_options_error['folder_name'] = $this->options['folder_name'];
218
219 $old_folder_normalized = Advanced_Ads_Filesystem::get_instance()->normalize_path( trailingslashit( $this->upload_dir['basedir'] ) ) . $this->options['folder_name'];
220
221 if ( $wp_filesystem->exists( $old_folder_normalized ) ) {
222
223 if ( $need_assign_new_name ) {
224 $existing_files[] = (string) $new_options['folder_name'];
225 $new_folder_name = $this->generate_unique_name( $existing_files );
226 $new_folder_normalized = Advanced_Ads_Filesystem::get_instance()->normalize_path( trailingslashit( $this->upload_dir['basedir'] ) ) . $new_folder_name;
227
228 if ( ! $wp_filesystem->move( $old_folder_normalized, $new_folder_normalized ) ) {
229 /* translators: %s old folder name */
230 $message = sprintf( __( 'Unable to rename "%s" directory', 'advanced-ads' ), $old_folder_normalized );
231 $this->error_messages->add( 'create_dummy_2', $message);
232 return false;
233 }
234 $new_options['folder_name'] = $new_options_error['folder_name'] = $new_folder_name;
235
236 }
237
238 $is_rebuild_needed = count( $this->get_assets() );
239
240 // we have an error while the method is being executed
241 Advanced_Ads_Ad_Blocker::get_instance()->update_options( $new_options_error );
242
243 if ( $is_rebuild_needed ) {
244 $lookup_table = $this->copy_assets( $new_options['folder_name'], $need_assign_new_name );
245 if ( ! $lookup_table ) {
246 /* translators: %s folder name */
247 $message = sprintf( __( 'Unable to copy assets to the "%s" directory', 'advanced-ads' ), $new_options['folder_name'] );
248 $this->error_messages->add( 'create_dummy_3', $message);
249 return false;
250 }
251 $new_options['lookup_table'] = $lookup_table;
252 }
253
254 } else {
255 // we have an error while the method is being executed
256 Advanced_Ads_Ad_Blocker::get_instance()->update_options( $new_options_error );
257 // old folder does not exist, let's create it
258 $lookup_table = $this->copy_assets( $new_options['folder_name'] );
259 if ( ! $lookup_table ) {
260 /* translators: %s folder name */
261 $message = sprintf( __( 'Unable to copy assets to the "%s" directory', 'advanced-ads' ), $new_options['folder_name'] );
262 $this->error_messages->add( 'create_dummy_4', $message);
263 return false;
264 }
265 $new_options['lookup_table'] = $lookup_table;
266 }
267 } else {
268 // It seems this is the first time this plugin was ran, let's create everything we need in order to
269 // have this plugin function normally.
270 $new_folder_name = $this->generate_unique_name( $existing_files );
271 // Create a unique folder name
272 $new_options['folder_name'] = $new_options_error['folder_name'] = $new_folder_name;
273 // we have an error while the method is being executed
274 Advanced_Ads_Ad_Blocker::get_instance()->update_options( $new_options_error );
275 // Copy the assets
276 $lookup_table = $this->copy_assets( $new_options['folder_name'] );
277 if ( ! $lookup_table ) {
278 $message = sprintf( __( 'Unable to copy assets to the "%s" directory', 'advanced-ads' ), $new_options['folder_name'] );
279 $this->error_messages->add( 'create_dummy_5', $message);
280 return false;
281 }
282 $new_options['lookup_table'] = $lookup_table;
283 }
284 // successful result, save options and rewrite previous error options
285 Advanced_Ads_Ad_Blocker::get_instance()->update_options( $new_options );
286 Advanced_Ads_Ad_Health_Notices::get_instance()->remove( 'assets_expired' );
287 }
288
289 /**
290 * Copy all assets (JS/CSS) to the magic directory.
291 *
292 * @param string $folder_name Destination folder.
293 * @param bool $need_assign_new_name True if we need to assign new random names to assets.
294 * @return bool/array Bool false on failure, array lookup table on success.
295 */
296 public function copy_assets( $folder_name, $need_assign_new_name = false ) {
297 global $wp_filesystem;
298
299 // Are we completely rebuilding the assets folder?
300 $asset_path = trailingslashit( $this->upload_dir['basedir'] ) . $folder_name ;
301 $asset_path_normalized = Advanced_Ads_Filesystem::get_instance()->normalize_path( trailingslashit( $this->upload_dir['basedir'] ) ) . $folder_name;
302
303 // already saved associations (original name => replaced name)
304 $rand_asset_names = [];
305
306 if ( $need_assign_new_name ) {
307 // Check if there is a previous asset folder
308 if ( $wp_filesystem->exists( $asset_path_normalized ) ) {
309 // Remove the old directory and its contents
310 if ( ! $wp_filesystem->rmdir( trailingslashit( $asset_path_normalized ), true ) ) {
311 /* translators: %s directory path */
312 $message = sprintf( __( 'We do not have direct write access to the "%s" directory', 'advanced-ads' ), $asset_path_normalized );
313 $this->error_messages->add( 'copy_assets_1', $message);
314 return false;
315 }
316 }
317 } elseif ( isset( $this->options['lookup_table'] ) ) {
318 foreach ( $this->options['lookup_table'] as $orig_path => $replaced_info ) {
319 $replaced_path = is_array( $replaced_info ) ? $replaced_info['path'] : $replaced_info;
320
321 $orig_path_components = preg_split('/\//', $orig_path, -1, PREG_SPLIT_NO_EMPTY);
322 $replaced_path_components = preg_split('/\//', $replaced_path, -1, PREG_SPLIT_NO_EMPTY);
323
324 // (css, style.css) => (1, 2.css)
325 foreach ( $orig_path_components as $k=> $orig_path_part ) {
326 $rand_asset_names[ $orig_path_part] = (string) $replaced_path_components[$k];
327 }
328 }
329 }
330
331
332 // Lookup_table contains associations between the original path of the asset and it path within our magic folder.
333 // I.e: [advanced-ads-layer/admin/assets/css/admin.css] => array( path => /12/34/56/78/1347107783.css, mtime => 99 ).
334 $assets = $this->get_assets();
335 if ( $need_assign_new_name ) {
336 $lookup_table = [];
337 } else {
338 $lookup_table = isset( $this->options['lookup_table'] ) ? $this->options['lookup_table'] : [];
339 }
340
341 /* Do not rename assets and folders. If, for example, some library uses in file.css something like this:
342 'background: url(/img/image.png)', you should add 'img') to this array */
343 $not_rename_assets = [ 'public', 'assets', 'js', 'css', 'fancybox', 'advanced.js', 'jquery.fancybox-1.3.4.css' ];
344
345 // Loop through all the found assets
346 foreach ( $assets as $file => $filemtime ) {
347 if ( ! file_exists( $file ) ) {
348 continue;
349 }
350
351 $first_cleanup = str_replace( WP_PLUGIN_DIR , '', $file );
352 $first_cleanup_dir = dirname( $first_cleanup );
353 $first_cleanup_filename = basename( $first_cleanup );
354 $first_cleanup_file_extension = pathinfo( $first_cleanup, PATHINFO_EXTENSION );
355 $path_components = preg_split('/\//', $first_cleanup_dir, -1, PREG_SPLIT_NO_EMPTY);
356 $path_components_new = [];
357
358 // Interate over directories.
359 foreach ( $path_components as $k => $dir ) {
360 if ( in_array( $dir, $not_rename_assets ) ) {
361 $path_components_new[ $k ] = $dir;
362 } elseif ( array_key_exists( $dir, $rand_asset_names ) ) {
363 $path_components_new[ $k ] = $rand_asset_names[ $dir ];
364 } else {
365 $new_rand_folder_name = $this->generate_unique_name( array_values( $rand_asset_names ) );
366 $path_components_new[ $k ] = $new_rand_folder_name;
367 $rand_asset_names[ $dir ] = (string) $new_rand_folder_name;
368 }
369 }
370
371 $new_dir_full = trailingslashit( $asset_path ) . trailingslashit( implode( '/', $path_components_new ) );
372 $new_dir_full_normalized = trailingslashit( $asset_path_normalized ) . trailingslashit( implode( '/', $path_components_new ) );
373 $new_dir = trailingslashit( implode( '/', $path_components_new ) );
374
375
376
377
378 if ( ! in_array( $first_cleanup_filename, $not_rename_assets ) && ( $first_cleanup_file_extension == 'js' || $first_cleanup_file_extension == 'css' ) ) {
379 if ( array_key_exists( $first_cleanup_filename, $rand_asset_names ) ) {
380 $new_abs_file = $new_dir_full_normalized . $rand_asset_names[$first_cleanup_filename];
381 $new_rel_file = $new_dir . $rand_asset_names[$first_cleanup_filename];
382 } else {
383 $new_filename = $this->generate_unique_name( array_values( $rand_asset_names ), $first_cleanup_file_extension );
384 $rand_asset_names[$first_cleanup_filename] = (string) $new_filename;
385 $new_abs_file = $new_dir_full_normalized . $new_filename;
386 $new_rel_file = $new_dir . $new_filename;
387 }
388 } else {
389 $new_abs_file = $new_dir_full_normalized . $first_cleanup_filename;
390 $new_rel_file = $new_dir . $first_cleanup_filename;
391 }
392
393
394 if ( ! file_exists( $new_dir_full_normalized ) ) {
395 // Create the path if it doesn't exist (prevents the copy() function from failing)
396 if ( ! Advanced_Ads_Filesystem::get_instance()->mkdir_p( $new_dir_full_normalized ) ) {
397 $message = sprintf( __( 'We do not have direct write access to the "%s" directory', 'advanced-ads' ), $this->upload_dir['basedir'] );
398 $this->error_messages->add( 'copy_assets_4', $message);
399 return false;
400 }
401 }
402
403
404 $file_normalized = Advanced_Ads_Filesystem::get_instance()->normalize_path( trailingslashit( dirname( $file ) ) ) . basename( $file );
405
406 // Copy the file to our new magic directory,
407 if ( ! $wp_filesystem->copy( $file_normalized, $new_abs_file, true, FS_CHMOD_FILE ) ) {
408 /* translators: %s directory path */
409 $message = sprintf( __( 'Unable to copy files to %s', 'advanced-ads' ), $asset_path_normalized );
410 $this->error_messages->add( 'copy_assets_5', $message);
411 return false;
412 }
413
414 $lookup_table[ $first_cleanup ] = [
415 'path' => $new_rel_file,
416 'mtime' => $filemtime,
417 ];
418 }
419
420 return $lookup_table;
421 }
422
423 /**
424 * This function recursively searches for assets
425 *
426 * @param string $dir The directory to search in.
427 * @return Array with pairs: abs_filename => mtime.
428 */
429 public function recursive_search_assets( $dir ) {
430 $assets = [];
431
432 $tree = glob( rtrim( $dir, '/' ) . '/*' );
433 if ( is_array( $tree ) ) {
434 foreach ( $tree as $file ) {
435 if ( is_dir( $file ) && ! preg_match( $this->exclude_dir_pattern, $file ) ) {
436 $assets = array_merge( $assets, $this->recursive_search_assets( $file ) );
437 } elseif ( is_file( $file ) && preg_match( $this->search_file_pattern, $file ) ) {
438 $assets[ $file ] = @filemtime( $file );
439 }
440 }
441 }
442
443 return $assets;
444 }
445
446 /**
447 * Returns new or modified assets and their mtimes.
448 *
449 * @return array
450 */
451 public function get_assets() {
452 $new_files_info = $this->recursive_search_assets( trailingslashit( WP_PLUGIN_DIR ) . 'advanced-ads*' );
453
454 if ( ! isset( $this->options['lookup_table'] ) || ! isset( $this->upload_dir['basedir'] ) || ! isset( $this->options['folder_name'] ) ) {
455 return $new_files_info;
456 }
457
458 $asset_path = trailingslashit( trailingslashit( $this->upload_dir['basedir'] ) . $this->options['folder_name'] ) ;
459 $new_files = [];
460
461 foreach ( $new_files_info as $abs_file => $mtime ) {
462 $rel_file = str_replace( WP_PLUGIN_DIR , '', $abs_file );
463
464 if ( ! isset( $this->options['lookup_table'][ $rel_file ]['mtime'] ) ||
465 $this->options['lookup_table'][ $rel_file ]['mtime'] !== $mtime ||
466 ! file_exists( $asset_path . $this->options['lookup_table'][$rel_file]['path'] )
467 ) {
468 $new_files[ $abs_file ] = $mtime;
469 }
470 }
471
472 return $new_files;
473 }
474
475 /**
476 * Automatically updates assets
477 *
478 */
479 public function process_auto_update() {
480 $advads_options = Advanced_Ads::get_instance()->get_adblocker_options();
481
482 if ( ! isset( $advads_options['use-adblocker'] )
483 || ! $this->upload_dir
484 ) { return; }
485
486 //if module is working without errors and there are new assets
487 if ( ! empty( $this->options['module_can_work'] ) && count( $this->get_assets() ) ) {
488 $fs_connect = Advanced_Ads_Filesystem::get_instance()->fs_connect( $this->upload_dir['basedir'] );
489
490 if ( false === $fs_connect || is_wp_error( $fs_connect ) ) {
491 // we can not update assets automatically. The user should visit the setting page and update assets manually
492 // disable module and show notice
493 unset( $this->options['module_can_work'] );
494 Advanced_Ads_Ad_Blocker::get_instance()->update_options( $this->options );
495 return;
496 }
497
498 $this->create_dummy_plugin();
499
500 // write errors to the log
501 if ( $error_messages = $this->error_messages->get_error_messages() ) {
502 foreach ( $error_messages as $error_message ) {
503 Advanced_Ads::log( __METHOD__ . ': ' . $error_message );
504 }
505 }
506 }
507
508 }
509
510 /**
511 * Generate unique name
512 *
513 * @param array $haystack array to check, that the returned string does not exist in this array
514 * @param string $extension Extension to append to the name.
515 * @return string unique name
516 */
517 function generate_unique_name( $haystack = false, $extension = '' ) {
518 $extension = $extension ? '.' . $extension : '';
519 if ( $haystack ) {
520 $i = 0;
521
522 do {
523 $rand = (string) mt_rand( 1, 999 );
524 if ( ++$i < 100 ) {
525 $needle = (string) $rand . $extension;
526 } else {
527 $needle = (string) $rand . '_' . $i . $extension;
528 }
529
530 } while( in_array( $needle, $haystack ) );
531
532 return $needle;
533 }
534
535 $needle = (string) mt_rand( 1, 999 ) . $extension;
536 return $needle;
537 }
538
539 /**
540 * Clear assets (on uninstall)
541 */
542 function clear_assets() {
543 $advads_options = Advanced_Ads::get_instance()->options();
544
545 if ( ! empty( $this->options['folder_name'] )
546 && ! empty( $this->options['module_can_work'] )
547 && $this->upload_dir
548 && class_exists( 'WP_Filesystem_Direct', false )
549 ) {
550 $wp_filesystem = new WP_Filesystem_Direct( new StdClass() );
551 $path = trailingslashit( $this->upload_dir['basedir'] ) . trailingslashit( $this->options['folder_name'] );
552 $wp_filesystem->rmdir( $path, true );
553 }
554 }
555
556 }
557