strlen( $suffix ) && 0 === strcasecmp( substr( $abs, -strlen( $suffix ) ), $suffix ) ) { $root = substr( $abs, 0, strlen( $abs ) - strlen( $suffix ) + 1 ); return is_dir( $root ) ? $root : $abs; } return $abs; } /** * Does a file carry the "Generated by aeoora" marker (any case: 1.0.0 wrote * "AEOORA")? Reads only its start and end. * * @param string $path File. * @return bool */ function aeoora_uninstall_marked( $path ) { if ( ! is_file( $path ) || ! is_readable( $path ) ) { return false; } $fh = @fopen( $path, 'rb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions if ( ! $fh ) { return false; } $text = (string) fread( $fh, 4096 ); // phpcs:ignore WordPress.WP.AlternativeFunctions $size = (int) filesize( $path ); if ( $size > 4096 ) { fseek( $fh, $size - 4096 ); $text .= (string) fread( $fh, 4096 ); // phpcs:ignore WordPress.WP.AlternativeFunctions } fclose( $fh ); // phpcs:ignore WordPress.WP.AlternativeFunctions return false !== stripos( $text, 'Generated by aeoora' ); } /** * Clean one site. * * @return bool Whether the data was removed (false with "Keep data"). */ function aeoora_uninstall_site() { global $wpdb; // Every scheduled event goes either way (the code that runs them is gone); // with "Keep data", the aeoora Cloud connection is kept for a reinstall. foreach ( array( 'aeoora_daily', 'aeoora_prune', 'aeoora_send_report', 'aeoora_regenerate_llms', 'aeoora_suggestions_run', 'aeoora_queue', 'aeoora_warm_render', 'aeoora_score_post', 'aeoora_cloud_send', 'aeoora_llms_refresh', 'aeoora_scan_site' ) as $hook ) { wp_unschedule_hook( $hook ); } $settings = get_option( 'aeoora_settings', array() ); if ( ! empty( $settings['general']['keep_data'] ) ) { return false; } foreach ( array( 'aeoora_hits', 'aeoora_daily', 'aeoora_referrals', 'aeoora_suggestions', 'aeoora_cache', 'aeoora_cloud_queue' ) as $t ) { $table = $wpdb->prefix . $t; $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange -- our own tables, on uninstall. } // Remove our block from a physical robots.txt, and static llms files we created. // They live in the site root, which differs from ABSPATH when WordPress has its own directory. $root = aeoora_uninstall_root(); $robots = $root . 'robots.txt'; if ( file_exists( $robots ) && wp_is_writable( $robots ) ) { $content = (string) file_get_contents( $robots ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( false !== stripos( $content, '# BEGIN aeoora' ) ) { $clean = preg_replace( '/\n*# BEGIN aeoora.*?# END aeoora\n*/si', "\n", $content ); if ( null !== $clean ) { file_put_contents( $robots, rtrim( $clean ) . "\n" ); // phpcs:ignore WordPress.WP.AlternativeFunctions } } } foreach ( array( 'llms.txt', 'llms-full.txt' ) as $name ) { if ( aeoora_uninstall_marked( $root . $name ) ) { wp_delete_file( $root . $name ); } } $uploads = wp_upload_dir( null, false ); $dir = trailingslashit( $uploads['basedir'] ) . 'aeoora'; if ( empty( $uploads['error'] ) && is_dir( $dir ) ) { $it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, FilesystemIterator::SKIP_DOTS ), RecursiveIteratorIterator::CHILD_FIRST ); foreach ( $it as $f ) { $f->isDir() ? rmdir( $f->getPathname() ) : wp_delete_file( $f->getPathname() ); // phpcs:ignore WordPress.WP.AlternativeFunctions } rmdir( $dir ); // phpcs:ignore WordPress.WP.AlternativeFunctions } $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s', $wpdb->options, $wpdb->esc_like( 'aeoora_' ) . '%', $wpdb->esc_like( '_transient_aeoora_' ) . '%', $wpdb->esc_like( '_transient_timeout_aeoora_' ) . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- our own options, on uninstall. $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE meta_key LIKE %s', $wpdb->postmeta, $wpdb->esc_like( '_aeoora_' ) . '%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- our own post meta, on uninstall. wp_cache_delete( 'alloptions', 'options' ); return true; } if ( function_exists( 'set_time_limit' ) ) { @set_time_limit( 600 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors, Squiz.PHP.DiscouragedFunctions.Discouraged -- every site of a network. } $aeoora_removed = false; if ( is_multisite() ) { $aeoora_offset = 0; do { $aeoora_sites = get_sites( array( 'fields' => 'ids', 'number' => 100, 'offset' => $aeoora_offset, ) ); foreach ( $aeoora_sites as $aeoora_site_id ) { switch_to_blog( $aeoora_site_id ); $aeoora_done = aeoora_uninstall_site(); if ( is_main_site() ) { $aeoora_removed = $aeoora_done; } restore_current_blog(); } $aeoora_offset += 100; } while ( 100 === count( $aeoora_sites ) ); } else { $aeoora_removed = aeoora_uninstall_site(); } // The dashboard widget's period choice (user meta, shared by every site of a network). if ( $aeoora_removed ) { delete_metadata( 'user', 0, 'aeoora_widget_days', '', true ); }