Skip to content

Commit

Permalink
Merge pull request Yoast#21219 from Yoast/21128-version_compare--str_…
Browse files Browse the repository at this point in the history
…replace---deprecated-in-php-82

Add defensive coding to stop deprecation warnings
  • Loading branch information
thijsoo authored Mar 13, 2024
2 parents a422150 + 27efa2d commit afe0eee
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 44 deletions.
12 changes: 8 additions & 4 deletions inc/class-addon-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,13 @@ protected function has_subscription_expired( $subscription ) {
* @return stdClass The converted subscription.
*/
protected function convert_subscription_to_plugin( $subscription, $yoast_free_data = null, $plugin_info = false, $plugin_file = '' ) {
// We need to replace h2's and h3's with h4's because the styling expects that.
$changelog = str_replace( '</h2', '</h4', str_replace( '<h2', '<h4', $subscription->product->changelog ) );
$changelog = str_replace( '</h3', '</h4', str_replace( '<h3', '<h4', $changelog ) );
$changelog = '';
if ( isset( $subscription->product->changelog ) ) {
// We need to replace h2's and h3's with h4's because the styling expects that.
$changelog = str_replace( '</h2', '</h4', str_replace( '<h2', '<h4', $subscription->product->changelog ) );
$changelog = str_replace( '</h3', '</h4', str_replace( '<h3', '<h4', $changelog ) );

}

// If we're running this because we want to just show the plugin info in the version details modal, we can fallback to the Yoast Free constants, since that modal will not be accessible anyway in the event that the new Free version increases those constants.
$defaults = [
Expand All @@ -563,7 +567,7 @@ protected function convert_subscription_to_plugin( $subscription, $yoast_free_da
];

return (object) [
'new_version' => $subscription->product->version,
'new_version' => ( $subscription->product->version ?? '' ),
'name' => $subscription->product->name,
'slug' => $subscription->product->slug,
'plugin' => $plugin_file,
Expand Down
111 changes: 71 additions & 40 deletions tests/Unit/Inc/Addon_Manager_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,54 +643,85 @@ public function test_get_slug_by_plugin_file() {
/**
* Tests the conversion from a subscription to a plugin array.
*
* @dataProvider convert_subscription_to_plugin_dataprovider
*
* @covers ::convert_subscription_to_plugin
*
* @param object $subscription The subscription to convert.
* @param object $expected_result The expected result.
*
* @return void
*/
public function test_convert_subscription_to_plugin() {
$this->stubTranslationFunctions();
public function test_convert_subscription_to_plugin( $subscription, $expected_result ) {

$this->assertEquals(
(object) [
'new_version' => '10.0',
'name' => 'Extension',
'slug' => 'yoast-seo-wordpress-premium',
'plugin' => '',
'url' => 'https://example.org/store',
'last_update' => 'yesterday',
'homepage' => 'https://example.org/store',
'download_link' => 'https://example.org/extension.zip',
'package' => 'https://example.org/extension.zip',
'sections' => [
'changelog' => 'changelog',
'support' => '<h4>Need support?</h4><p>You can probably find an answer to your question in our <a href="https://yoast.com/help/">help center</a>. If you still need support and have an active subscription for this product, please email <a href="mailto:support@yoast.com">support@yoast.com</a>.</p>',
],
'icons' => [
'2x' => 'https://yoa.st/yoast-seo-icon',
$result = $this->instance->convert_subscription_to_plugin( $subscription );
$this->assertEquals( $expected_result, $result );
}

/**
* Data provider for test_convert_subscription_to_plugin.
*
* @return array<string, array<string, object>> The data for test_convert_subscription_to_plugin.
*/
public static function convert_subscription_to_plugin_dataprovider() {
$full_subscription = [
'version' => '10.0',
'name' => 'Extension',
'slug' => 'yoast-seo-wordpress-premium',
'last_updated' => 'yesterday',
'store_url' => 'https://example.org/store',
'download' => 'https://example.org/extension.zip',
'changelog' => 'changelog',
];
$partial_subscription = $full_subscription;
unset( $partial_subscription['changelog'] );
unset( $partial_subscription['version'] );

$expected_plugin_conversion_with_proper_subscription_data = [
'new_version' => '10.0',
'name' => 'Extension',
'slug' => 'yoast-seo-wordpress-premium',
'plugin' => '',
'url' => 'https://example.org/store',
'last_update' => 'yesterday',
'homepage' => 'https://example.org/store',
'download_link' => 'https://example.org/extension.zip',
'package' => 'https://example.org/extension.zip',
'sections' => [
'changelog' => 'changelog',
'support' => '<h4>Need support?</h4><p>You can probably find an answer to your question in our <a href="https://yoast.com/help/">help center</a>. If you still need support and have an active subscription for this product, please email <a href="mailto:support@yoast.com">support@yoast.com</a>.</p>',
],
'icons' => [
'2x' => 'https://yoa.st/yoast-seo-icon',
],
'update_supported' => true,
'banners' => [
'high' => 'https://yoa.st/yoast-seo-banner-premium',
'low' => 'https://yoa.st/yoast-seo-banner-low-premium',
],
'tested' => \YOAST_SEO_WP_TESTED,
'requires_php' => \YOAST_SEO_PHP_REQUIRED,
'requires' => null,
];
$expected_plugin_conversion_with_partial_subscription_data = $expected_plugin_conversion_with_proper_subscription_data;

$expected_plugin_conversion_with_partial_subscription_data['sections']['changelog'] = '';
$expected_plugin_conversion_with_partial_subscription_data['new_version'] = '';

return [
'Converting a subscription with full data' => [
'subscription' => (object) [
'product' => (object) $full_subscription,
],
'update_supported' => true,
'banners' => [
'high' => 'https://yoa.st/yoast-seo-banner-premium',
'low' => 'https://yoa.st/yoast-seo-banner-low-premium',
'expected_result' => (object) $expected_plugin_conversion_with_proper_subscription_data,
],
'Converting a subscription with partial data' => [
'subscription' => (object) [
'product' => (object) $partial_subscription,
],
'tested' => \YOAST_SEO_WP_TESTED,
'requires_php' => \YOAST_SEO_PHP_REQUIRED,
'requires' => null,
'expected_result' => (object) $expected_plugin_conversion_with_partial_subscription_data,
],
$this->instance->convert_subscription_to_plugin(
(object) [
'product' => (object) [
'version' => '10.0',
'name' => 'Extension',
'slug' => 'yoast-seo-wordpress-premium',
'last_updated' => 'yesterday',
'store_url' => 'https://example.org/store',
'download' => 'https://example.org/extension.zip',
'changelog' => 'changelog',
],
]
)
);
];
}

/**
Expand Down

0 comments on commit afe0eee

Please sign in to comment.