Skip to content

Commit

Permalink
Merge pull request #21598 from Yoast/21597-refactor-iswoocommerceactive
Browse files Browse the repository at this point in the history
21597 refactor iswoocommerceactive
  • Loading branch information
igorschoester committed Sep 10, 2024
2 parents 83d5e74 + e622c4d commit 4e7901a
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 28 deletions.
7 changes: 2 additions & 5 deletions admin/class-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

use Yoast\WP\SEO\Actions\Alert_Dismissal_Action;
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional;
use Yoast\WP\SEO\Integrations\Academy_Integration;
use Yoast\WP\SEO\Integrations\Settings_Integration;
use Yoast\WP\SEO\Integrations\Support_Integration;
Expand Down Expand Up @@ -89,16 +88,14 @@ public function config_page_scripts() {
wp_enqueue_script( 'dashboard' );
wp_enqueue_script( 'thickbox' );

$alert_dismissal_action = YoastSEO()->classes->get( Alert_Dismissal_Action::class );
$dismissed_alerts = $alert_dismissal_action->all_dismissed();
$woocommerce_conditional = new WooCommerce_Conditional();
$alert_dismissal_action = YoastSEO()->classes->get( Alert_Dismissal_Action::class );
$dismissed_alerts = $alert_dismissal_action->all_dismissed();

$script_data = [
'userLanguageCode' => WPSEO_Language_Utils::get_language( get_user_locale() ),
'dismissedAlerts' => $dismissed_alerts,
'isRtl' => is_rtl(),
'isPremium' => YoastSEO()->helpers->product->is_premium(),
'isWooCommerceActive' => $woocommerce_conditional->is_met(),
'currentPromotions' => YoastSEO()->classes->get( Promotion_Manager::class )->get_current_promotions(),
'webinarIntroFirstTimeConfigUrl' => $this->get_webinar_shortlink(),
'linkParams' => WPSEO_Shortlinker::get_query_params(),
Expand Down
8 changes: 2 additions & 6 deletions admin/metabox/class-metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

use Yoast\WP\SEO\Conditionals\Third_Party\Jetpack_Boost_Active_Conditional;
use Yoast\WP\SEO\Conditionals\Third_Party\Jetpack_Boost_Not_Premium_Conditional;
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional;
use Yoast\WP\SEO\Editors\Application\Site\Website_Information_Repository;
use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter;
use Yoast\WP\SEO\Presenters\Admin\Meta_Fields_Presenter;
Expand Down Expand Up @@ -882,10 +881,8 @@ public function enqueue() {
'log_level' => WPSEO_Utils::get_analysis_worker_log_level(),
];

$woocommerce_conditional = new WooCommerce_Conditional();
$woocommerce_active = $woocommerce_conditional->is_met();
$addon_manager = new WPSEO_Addon_Manager();
$woocommerce_seo_active = is_plugin_active( $addon_manager->get_plugin_file( WPSEO_Addon_Manager::WOOCOMMERCE_SLUG ) );
$addon_manager = new WPSEO_Addon_Manager();
$woocommerce_seo_active = is_plugin_active( $addon_manager->get_plugin_file( WPSEO_Addon_Manager::WOOCOMMERCE_SLUG ) );

$script_data = [
'metabox' => $this->get_metabox_script_data(),
Expand All @@ -903,7 +900,6 @@ public function enqueue() {
'isJetpackBoostActive' => ( $is_block_editor ) ? YoastSEO()->classes->get( Jetpack_Boost_Active_Conditional::class )->is_met() : false,
'isJetpackBoostNotPremium' => ( $is_block_editor ) ? YoastSEO()->classes->get( Jetpack_Boost_Not_Premium_Conditional::class )->is_met() : false,
'isWooCommerceSeoActive' => $woocommerce_seo_active,
'isWooCommerceActive' => $woocommerce_active,
];

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/components/fills/MetaboxFill.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import PremiumSEOAnalysisModal from "../modals/PremiumSEOAnalysisModal";
import KeywordUpsell from "../modals/KeywordUpsell";
import { BlackFridayProductEditorChecklistPromotion } from "../BlackFridayProductEditorChecklistPromotion";
import { BlackFridayPromotion } from "../BlackFridayPromotion";
import { isWooCommerceActive } from "../../helpers/isWooCommerceActive";
import { withMetaboxWarningsCheck } from "../higherorder/withMetaboxWarningsCheck";

const BlackFridayProductEditorChecklistPromotionWithMetaboxWarningsCheck = withMetaboxWarningsCheck( BlackFridayProductEditorChecklistPromotion );
Expand All @@ -40,8 +39,9 @@ const BlackFridayPromotionWithMetaboxWarningsCheck = withMetaboxWarningsCheck( B
export default function MetaboxFill( { settings } ) {
const isTerm = useSelect( ( select ) => select( "yoast-seo/editor" ).getIsTerm(), [] );
const isProduct = useSelect( ( select ) => select( "yoast-seo/editor" ).getIsProduct(), [] );
const isWooCommerceActive = useSelect( ( select ) => select( "yoast-seo/editor" ).getIsWooCommerceActive(), [] );

const shouldShowWooCommerceChecklistPromo = isProduct && isWooCommerceActive();
const shouldShowWooCommerceChecklistPromo = isProduct && isWooCommerceActive;

return (
<>
Expand Down
9 changes: 0 additions & 9 deletions packages/js/src/helpers/isWooCommerceActive.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/js/src/redux/selectors/isWooSEO.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const getIsWooSeoActive = () => Boolean( get( window, "wpseoScriptData.is
*
* @returns {boolean} True if WooCommerce is active.
*/
export const getIsWooCommerceActive = () => Boolean( get( window, "wpseoScriptData.isWooCommerceActive", false ) );
export const getIsWooCommerceActive = () => get( window, "wpseoScriptData.metabox.isWooCommerceActive", false );

/**
* Determines whether the WooCommerce SEO addon is not active in a product page.
Expand Down
2 changes: 1 addition & 1 deletion src/editors/framework/integrations/multilingual.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function is_enabled(): bool {
* @return array<string,bool> Returns the name and if the feature is enabled.
*/
public function to_array(): array {
return [ 'multilingualPluginActive' => $this->is_enabled() ];
return [ 'isMultilingualActive' => $this->is_enabled() ];
}

/**
Expand Down
55 changes: 55 additions & 0 deletions src/editors/framework/integrations/woocommerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
// @phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- This namespace should reflect the namespace of the original class.
namespace Yoast\WP\SEO\Editors\Framework\Integrations;

use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional;
use Yoast\WP\SEO\Editors\Domain\Integrations\Integration_Data_Provider_Interface;

/**
* Describes if the Woocommerce plugin is enabled.
*/
class WooCommerce implements Integration_Data_Provider_Interface {

/**
* The WooCommerce conditional.
*
* @var WooCommerce_Conditional $woocommerce_conditional
*/
private $woocommerce_conditional;

/**
* The constructor.
*
* @param WooCommerce_Conditional $woocommerce_conditional The WooCommerce conditional.
*/
public function __construct( WooCommerce_Conditional $woocommerce_conditional ) {
$this->woocommerce_conditional = $woocommerce_conditional;
}

/**
* If the plugin is activated.
*
* @return bool If the plugin is activated.
*/
public function is_enabled(): bool {
return $this->woocommerce_conditional->is_met();
}

/**
* Return this object represented by a key value array.
*
* @return array<string,bool> Returns the name and if the feature is enabled.
*/
public function to_array(): array {
return [ 'isWooCommerceActive' => $this->is_enabled() ];
}

/**
* Returns this object represented by a key value structure that is compliant with the script data array.
*
* @return array<string,bool> Returns the legacy key and if the feature is enabled.
*/
public function to_legacy_array(): array {
return [ 'isWooCommerceActive' => $this->is_enabled() ];
}
}
5 changes: 1 addition & 4 deletions src/integrations/third-party/elementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use WPSEO_Replace_Vars;
use WPSEO_Utils;
use Yoast\WP\SEO\Conditionals\Third_Party\Elementor_Edit_Conditional;
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional;
use Yoast\WP\SEO\Editors\Application\Site\Website_Information_Repository;
use Yoast\WP\SEO\Elementor\Infrastructure\Request_Post;
use Yoast\WP\SEO\Helpers\Capability_Helper;
Expand Down Expand Up @@ -408,16 +407,14 @@ public function enqueue() {
'enabled_features' => WPSEO_Utils::retrieve_enabled_features(),
];

$woocommerce_conditional = new WooCommerce_Conditional();
$permalink = $this->get_permalink();
$permalink = $this->get_permalink();

$script_data = [
'metabox' => $this->get_metabox_script_data( $permalink ),
'userLanguageCode' => WPSEO_Language_Utils::get_language( \get_user_locale() ),
'isPost' => true,
'isBlockEditor' => WP_Screen::get()->is_block_editor(),
'isElementorEditor' => true,
'isWooCommerceActive' => $woocommerce_conditional->is_met(),
'postStatus' => \get_post_status( $post_id ),
'postType' => \get_post_type( $post_id ),
'analysis' => [
Expand Down
109 changes: 109 additions & 0 deletions tests/Unit/Editors/Framework/Integrations/WooCommerce_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
// @phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- This namespace should reflect the namespace of the original class.
namespace Yoast\WP\SEO\Tests\Unit\Editors\Framework\Integrations;

use Mockery;
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional;
use Yoast\WP\SEO\Editors\Framework\Integrations\WooCommerce;
use Yoast\WP\SEO\Tests\Unit\TestCase;

/**
* Class WooCommerce_Test
*
* @group editors
*
* @covers \Yoast\WP\SEO\Editors\Framework\Integrations\WooCommerce
*/
final class WooCommerce_Test extends TestCase {

/**
* Holds the WooCommerce conditional mock.
*
* @var Mockery\MockInterface|WooCommerce_Conditional
*/
protected $woocommerce_conditional;

/**
* The WooCommerce feature.
*
* @var WooCommerce
*/
private $instance;

/**
* Set up the test.
*
* @return void
*/
protected function set_up() {
parent::set_up();
$this->woocommerce_conditional = Mockery::mock( WooCommerce_Conditional::class );
$this->instance = new WooCommerce( $this->woocommerce_conditional );
}

/**
* Tests the is_enabled method.
*
* @dataProvider data_provider_is_enabled
*
* @param bool $woocommerce_enabled If the woocommerce plugin is enabled.
* @param bool $expected The expected outcome.
*
* @return void
*/
public function test_is_enabled(
bool $woocommerce_enabled,
bool $expected
) {

$this->woocommerce_conditional
->expects( 'is_met' )
->times( 3 )
->andReturn( $woocommerce_enabled );

$this->assertSame( $expected, $this->instance->is_enabled() );
$this->assertSame( [ 'isWooCommerceActive' => $this->instance->is_enabled() ], $this->instance->to_legacy_array() );
}

/**
* Tests the to_array method.
*
* @dataProvider data_provider_is_enabled
*
* @param bool $woocommerce_enabled If the woocommerce plugin is enabled.
* @param bool $expected The expected outcome.
*
* @return void
*/
public function test_to_array(
bool $woocommerce_enabled,
bool $expected
) {

$this->woocommerce_conditional
->expects( 'is_met' )
->times( 3 )
->andReturn( $woocommerce_enabled );

$this->assertSame( $expected, $this->instance->is_enabled() );
$this->assertSame( [ 'isWooCommerceActive' => $this->instance->is_enabled() ], $this->instance->to_array() );
}

/**
* Data provider for test_is_enabled.
*
* @return array<array<string|bool>>
*/
public static function data_provider_is_enabled() {
return [
'Enabled' => [
'woocommerce_enabled' => true,
'expected' => true,
],
'Disabled' => [
'woocommerce_enabled' => false,
'expected' => false,
],
];
}
}

0 comments on commit 4e7901a

Please sign in to comment.