Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix network creation #184

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
12ce2cf
add wp-multi-network-functions
mlwilkerson Oct 25, 2022
04471bf
WIP: working toward a test of adding a network after activating this …
mlwilkerson Oct 25, 2022
d812349
complete a failing test for adding a network after activating this pl…
mlwilkerson Oct 25, 2022
f27c5c6
Fix ReleaseProvider options storage in multisite to use main network
mlwilkerson Oct 25, 2022
c9ad87f
code cleanup and auto-formatting
mlwilkerson Oct 25, 2022
e637cac
add failing tests for multisite upgrade
mlwilkerson Oct 25, 2022
8f4700c
test refactoring
mlwilkerson Oct 25, 2022
2ed559b
add exception for multisite upgrade
mlwilkerson Oct 25, 2022
6cf8697
implement multisite upgrade for moving release metadata network optio…
mlwilkerson Oct 25, 2022
6705c24
when delete_network_option when cleaning up ReleaseProvider
mlwilkerson Oct 25, 2022
15e7027
include network option deletion in the cleanup utility plugin
mlwilkerson Oct 25, 2022
2e05680
bump to verson 4.3.2-1
mlwilkerson Oct 25, 2022
3861bc7
fix and test multisite uninstall
mlwilkerson Oct 26, 2022
7d26a84
WIP: multisite test refactoring
mlwilkerson Oct 26, 2022
0029e3c
fix multisite-upgrade test
mlwilkerson Oct 26, 2022
15572f3
add additional assertion to multisite-activation test
mlwilkerson Oct 26, 2022
e5af8e0
more multisite test refactoring
mlwilkerson Oct 26, 2022
4169edd
test cleanup
mlwilkerson Oct 26, 2022
0e1e7fe
auto-formatting
mlwilkerson Oct 26, 2022
e61cf13
ignoreFile for phpcs
mlwilkerson Oct 26, 2022
bb4f7c9
add missing @throws
mlwilkerson Oct 26, 2022
55c94a7
exclude new multisite tests from non-multisite test config
mlwilkerson Oct 26, 2022
548ea9f
bump tested up to 6.1
mlwilkerson Oct 26, 2022
fa31ce5
use literal ints instead of constants to avoid problems on php 5.6
mlwilkerson Oct 26, 2022
7a1fb00
update test matrix to test with 6.1-RC3
mlwilkerson Oct 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- php: 7.4
wordpress: trunk
- php: 8.0.14
wordpress: 5.9-RC2
wordpress: 6.1-RC3
# There are test errors in CI that aren't happening locally. What's different?
# - php: 8.1
# wordpress: 5.9-RC2
Expand Down
4 changes: 2 additions & 2 deletions admin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion admin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "font-awesome-admin",
"version": "4.3.1",
"version": "4.3.2-1",
"private": true,
"dependencies": {
"@fortawesome/fa-icon-chooser-react": "0.4.1",
Expand Down
4 changes: 2 additions & 2 deletions compat-js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion compat-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fa-compat-js",
"version": "4.3.1",
"version": "4.3.2-1",
"private": true,
"dependencies": {
"@wordpress/api-fetch": "^5.1.1",
Expand Down
4 changes: 3 additions & 1 deletion includes/class-fontawesome-deactivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ function( $blog_id ) {
} else {
self::delete_options();
}

// This one handles its own multisite considerations.
FontAwesome_Release_Provider::delete_option();
}

private static function delete_options() {
delete_option( FontAwesome::OPTIONS_KEY );
FontAwesome_Release_Provider::delete_option();
delete_option( FontAwesome::CONFLICT_DETECTION_OPTIONS_KEY );
delete_option( FontAwesome_API_Settings::OPTIONS_KEY );
}
Expand Down
15 changes: 15 additions & 0 deletions includes/class-fontawesome-exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,4 +591,19 @@ public static function main_option_delete() {
)
);
}

/**
* Internal use only.
*
* @internal
* @ignore
*/
public static function multisite_network_option_update() {
return new static(
esc_html__(
'Failed updating release metadata on a main network option when trying to upgrade the Font Awesome plugin in multisite mode.',
'font-awesome'
)
);
}
}
22 changes: 18 additions & 4 deletions includes/class-fontawesome-release-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public function latest_version_6() {
*/
public static function update_option( $option_value ) {
if ( is_multisite() ) {
$network_id = get_current_network_id();
$network_id = get_main_network_id();
return update_network_option( $network_id, self::OPTIONS_KEY, $option_value );
} else {
return update_option( self::OPTIONS_KEY, $option_value, false );
Expand All @@ -484,7 +484,7 @@ public static function update_option( $option_value ) {
*/
public static function get_option() {
if ( is_multisite() ) {
$network_id = get_current_network_id();
$network_id = get_main_network_id();
return get_network_option( $network_id, self::OPTIONS_KEY );
} else {
return get_option( self::OPTIONS_KEY );
Expand All @@ -499,8 +499,22 @@ public static function get_option() {
*/
public static function delete_option() {
if ( is_multisite() ) {
$network_id = get_current_network_id();
return delete_network_option( $network_id, self::OPTIONS_KEY );
$result_accumulator = true;

/**
* Delete the network option for all networks.
* In 4.3.1, it's possible that this option could have been created in
* any network, which ever one was the current network at the time the plugin
* refreshed releases metadata.
*
* Starting in 4.3.2, we only store the releases metadata on an option associated with the main network.
*/
foreach ( get_networks() as $network ) {
$current_result = delete_network_option( $network->id, self::OPTIONS_KEY );
$result_accumulator = $result_accumulator && $current_result;
}

return $result_accumulator;
} else {
return delete_option( self::OPTIONS_KEY );
}
Expand Down
85 changes: 82 additions & 3 deletions includes/class-fontawesome.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class FontAwesome {
*
* @since 4.0.0
*/
const PLUGIN_VERSION = '4.3.1';
const PLUGIN_VERSION = '4.3.2-1';
/**
* The namespace for this plugin's REST API.
*
Expand Down Expand Up @@ -489,12 +489,28 @@ public function try_upgrade() {
$should_upgrade = true;
}

if ( is_multisite() && ! boolval( get_network_option( get_main_network_id(), FontAwesome_Release_Provider::OPTIONS_KEY ) ) ) {
/**
* Handle possible multisite upgrade from 4.3.1.
* In 4.3.1 the release metadata might have been stored in a network
* option associated with a non-main network. As of 4.3.2, it's
* always stored on a network option associated with the main network.
* So if we're in multisite mode and we don't find the release metadata
* on a network option associated with the main network, we need to fix it up.
*/
$should_upgrade = true;
}

if ( $should_upgrade ) {
$this->validate_options( $options );

$this->maybe_update_last_used_release_schema_for_upgrade();

$this->maybe_move_release_metadata_for_upgrade();
if ( is_multisite() ) {
$this->maybe_move_release_metadata_for_upgrade_multisite();
} else {
$this->maybe_move_release_metadata_for_upgrade_single_site();
}

/**
* Delete the main option to make sure it's removed entirely, including
Expand Down Expand Up @@ -529,7 +545,7 @@ public function try_upgrade() {
* @ignore
* @internal
*/
private function maybe_move_release_metadata_for_upgrade() {
private function maybe_move_release_metadata_for_upgrade_single_site() {
if ( boolval( get_option( FontAwesome_Release_Provider::OPTIONS_KEY ) ) ) {
// If this option is set, then we're all caught up.
return;
Expand Down Expand Up @@ -568,6 +584,69 @@ private function maybe_move_release_metadata_for_upgrade() {
FontAwesome_Release_Provider::reset();
}

/**
* If upgrading from 4.3.1 to 4.3.2 or beyond, and we don't have the release
* metadata stored on a network option associated with the main network,
* we need to find it on a non-main network and move it to main network.
*
* If we can't find it on a non-main network, either, then that's an
* exception. We intentionally will not query the API server, since
* issuing a blocking request on upgrade is known to cause load problems
* and request timeouts.
*
* Internal use only.
*
* @throws ReleaseMetadataMissingException
* @throws UpgradeException
* @ignore
* @internal
*/
private function maybe_move_release_metadata_for_upgrade_multisite() {
if ( ! is_multisite() ) {
return;
}

if ( boolval( get_network_option( get_main_network_id(), FontAwesome_Release_Provider::OPTIONS_KEY ) ) ) {
// If there's already release metadata on a network option for the main network, we're done.
return;
}

foreach ( get_networks() as $network ) {
$option_value = get_network_option( $network->id, FontAwesome_Release_Provider::OPTIONS_KEY );

if ( is_array( $option_value ) ) {
$result = update_network_option( get_main_network_id(), FontAwesome_Release_Provider::OPTIONS_KEY, $option_value );

if ( ! $result ) {
throw UpgradeException::multisite_network_option_update();
}

delete_network_option( $network->id, FontAwesome_Release_Provider::OPTIONS_KEY );

/**
* Return early, once we've found what we're looking for.
* While it's possible that there are additional non-main networks that also
* have options with release metadata, it's unlikely. Regardless,
* since this upgrade process happens on a normal front-end page load,
* we don't want to do any unnecessary processing here. The only reason
* to go searching through other network options would be to clean up
* any additional obsolete data. We'll leave that clean up to the plugin's
* uninstall logic.
*/
return;
}
}

/**
* Now we'll reset the release provider.
*
* If we've fallen through to this point, and we haven't found the release
* metadata stored in one of the previous locations, then this will throw an
* exception.
*/
FontAwesome_Release_Provider::reset();
}

/**
* With 4.1.0, the name of one of the keys in the LAST_USED_RELEASE_TRANSIENT changed.
* We can fix it up.
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Font Awesome
* Plugin URI: https://fontawesome.com/how-to-use/on-the-web/using-with/wordpress
* Description: The official way to use Font Awesome Free or Pro icons on your site, brought to you by the Font Awesome team.
* Version: 4.3.1
* Version: 4.3.2-1
* Author: Font Awesome
* Author URI: https://fontawesome.com/
* License: GPLv2 (or later)
Expand Down
10 changes: 10 additions & 0 deletions integrations/plugins/font-awesome-cleanup/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ function( ) {
cleanup_site();
}
);

foreach ( get_options() as $option ) {
delete_network_option( get_current_network_id(), $option );
}
} else {
cleanup_site();
}
Expand Down Expand Up @@ -204,6 +208,8 @@ function display_cleanup_scope_multisite() {
if ( $is_cleanup_network_active ) {
$network_id = get_current_network_id();

$networks = get_networks();

for_each_blog(
function( $site ) use (&$sites) {
array_push( $sites, $site );
Expand All @@ -212,6 +218,10 @@ function( $site ) use (&$sites) {
?>
<p>Cleaning ALL sites in network with network_id: <?= $network_id ?>.</p>
<p>To clean up only one site, activate this cleanup plugin only on that one site instead of activating it network-wide.</p>
<?php if ( count( $networks ) > 0 ) { ?>
<p>There are <?php echo count( $networks ) - 1 ?> other networks that will not be affected by this cleanup.</p>
<p>To clean up multiple <em>networks</em>, network activate this cleanup plugin for each network, and run this cleanup for each network.</p>
<?php } ?>
<?php
} else {
array_push( $sites, get_site() );
Expand Down
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<directory prefix="test-" suffix=".php">./tests/</directory>
<exclude>./tests/loader/</exclude>
<exclude>./tests/test-multisite-activation.php</exclude>
<exclude>./tests/test-multisite-deactivation.php</exclude>
<exclude>./tests/test-multisite-upgrade.php</exclude>
</testsuite>
</testsuites>
</phpunit>
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: fontawesome, mlwilkerson, robmadole, frrrances, deathnfudge
Stable tag: 4.3.1
Tags: font, awesome, fontawesome, font-awesome, icon, svg, webfont
Requires at least: 4.7
Tested up to: 6.0.2
Tested up to: 6.1
Requires PHP: 5.6
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Expand Down
38 changes: 38 additions & 0 deletions tests/_support/font-awesome-phpunit-util.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,41 @@ function create_subsites($domains = ['alpha.example.com', 'beta.example.com']) {

return $results;
}

if ( is_multisite() ) :
require_once dirname( __FILE__ ) . '/wp-multi-network-functions.php';

function add_network() {
$sub_domain = dechex( wp_rand( -2147483648, 2147483647 ) );
$domain = "$sub_domain.example.com";
$path = '/';

$admin_user = get_users( array( 'role' => 'administrator' ) )[0];
$result = \add_network(
array(
'domain' => $domain,
'path' => '/',
'site_name' => $domain,
'network_name' => $domain,
'user_id' => $admin_user->ID,
'network_admin_id' => $admin_user->ID,
)
);

if ( is_wp_error( $result ) ) {
throw new \Exception( 'failed creating network' );
}

return $result;
}

function curry_add_network_handler( &$network_ids ) {
return function ( $network_id, $params ) use ( &$network_ids ) {
if ( ! is_array( $network_ids ) ) {
return null;
}

array_push( $network_ids, $network_id );
};
}
endif;
Loading