Skip to content

Commit 0c14b23

Browse files
fix: add notice for BF via SDK
1 parent 93b5f91 commit 0c14b23

2 files changed

Lines changed: 142 additions & 11 deletions

File tree

inc/admin.php

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public function __construct() {
114114

115115
add_filter( 'themeisle-sdk/survey/' . OPTML_PRODUCT_SLUG, [ $this, 'get_survey_metadata' ], 10, 2 );
116116
add_action( 'admin_init', [ $this, 'mark_user_with_offload' ] );
117+
add_filter( 'themeisle_sdk_blackfriday_data', [ $this, 'add_black_friday_data' ] );
117118
}
118119

119120
/**
@@ -1591,7 +1592,7 @@ public function get_bf_notices( $user_plan ) {
15911592
$end->setTime( 23, 59, 59 );
15921593

15931594
$promo_code = 'BFCM2525';
1594-
1595+
15951596
if ( $has_free_plan ) {
15961597
$notices['sidebar'] = [
15971598
'title' => sprintf(
@@ -1603,7 +1604,7 @@ public function get_bf_notices( $user_plan ) {
16031604
'subtitle' => sprintf(
16041605
/* translators: %1$s is the promo code, %2$s is the discount amount ('25 off') */
16051606
__( 'Use code %1$s for an instant %2$s', 'optimole-wp' ),
1606-
'<span class="border-b border-0 border-white border-dashed text-promo-orange">' . $promo_code .'</span>',
1607+
'<span class="border-b border-0 border-white border-dashed text-promo-orange">' . $promo_code . '</span>',
16071608
'<span class="text-promo-orange uppercase">' . __( '25% off', 'optimole-wp' ) . '</span>'
16081609
),
16091610
'cta_link' => esc_url_raw( tsdk_utmify( tsdk_translate_link( self::get_upgrade_base_link() ), 'bfcm25', 'sidebarnotice' ) ),
@@ -1625,20 +1626,13 @@ public function get_bf_notices( $user_plan ) {
16251626

16261627
if ( $has_monthly_plan ) {
16271628
$message = sprintf(
1628-
// translators: %1$s is opening span tag, %2$s is closing span tag, %3$s is the discount amount ('15% off')
1629+
// translators: %1$s is opening span tag, %2$s is closing span tag, %3$s is the discount amount ('15% off')
16291630
__( '%1$sSwitch to a yearly plan%2$s and get an instant %3$s on your payment. Contact us to reedem your deal', 'optimole-wp' ),
16301631
'<span class=" text-promo-orange">',
16311632
'</span>',
16321633
'<span class="text-promo-orange uppercase">' . __( '15% off', 'optimole-wp' ) . '</span>'
16331634
);
1634-
$cta_link = esc_url_raw( tsdk_utmify( self::get_contact_base_link(), 'bfcm25', 'notice' ) );
1635-
$cta_link = add_query_arg(
1636-
[
1637-
'contact_subject' => rawurlencode( 'Upgrade to yearly plan - Black Friday offer' ),
1638-
'contact_website' => rawurlencode( home_url() ),
1639-
],
1640-
$cta_link
1641-
);
1635+
$cta_link = $this->get_sale_url_for_monthly_plan();
16421636
$cta_text = __( 'Contact us', 'optimole-wp' );
16431637
}
16441638

@@ -1656,6 +1650,77 @@ public function get_bf_notices( $user_plan ) {
16561650
return $notices;
16571651
}
16581652

1653+
/**
1654+
* Set the black friday data.
1655+
*
1656+
* @param array $configs The configuration array for the loaded products.
1657+
* @return array
1658+
*/
1659+
public function add_black_friday_data( $configs ) {
1660+
1661+
$service_data = $this->settings->get( 'service_data' );
1662+
$user_plan = isset( $service_data['plan'] ) ? $service_data['plan'] : 'free';
1663+
1664+
$is_free_plan = 'free' === $user_plan;
1665+
$is_monthly_plan = strpos( $user_plan, '-yearly' ) === false && ! $is_free_plan;
1666+
1667+
if ( ! $is_free_plan && ! $is_monthly_plan ) {
1668+
return $configs;
1669+
}
1670+
1671+
$config = $configs['default'];
1672+
1673+
$message = sprintf(
1674+
// translators: $1$s is the promo code, $2$s is the discount amount ('25% off')
1675+
__( 'Use coupon code %1$s for an instant %2$s on your first billing cycle on Optimole plan.', 'optimole-wp' ),
1676+
'<strong>BFCM2525</strong>',
1677+
'<strong>' . strtoupper( __( '25% off', 'optimole-wp' ) ) . '</strong>'
1678+
);
1679+
$title = __( 'Optimole Black Friday Private Sale is live!', 'optimole-wp' );
1680+
$sale_url = esc_url_raw( tsdk_utmify( self::get_upgrade_base_link(), 'bfcm25', 'global_notice' ) );
1681+
1682+
if ( $is_monthly_plan ) {
1683+
$message = sprintf(
1684+
// translators: %1$s is opening strong tag, %2$s is closing strong tag, %3$s is the discount amount ('15% off')
1685+
__( '%1$sSwitch to a yearly plan%2$s and get an instant %3$s on your payment. Contact us to reedem your deal', 'optimole-wp' ),
1686+
'<strong>',
1687+
'</strong>',
1688+
'<strong>' . strtoupper( __( '15% off', 'optimole-wp' ) ) . '</strong>'
1689+
);
1690+
$sale_url = $this->get_sale_url_for_monthly_plan();
1691+
}
1692+
1693+
$config['title'] = $title;
1694+
$config['message'] = $message;
1695+
$config['sale_url'] = $sale_url;
1696+
$config['dismiss'] = true;
1697+
1698+
$configs['optimole-wp'] = $config;
1699+
1700+
return $configs;
1701+
}
1702+
1703+
/**
1704+
* Get the sale url for yearly plan.
1705+
*
1706+
* @return string
1707+
*/
1708+
private function get_sale_url_for_monthly_plan() {
1709+
$sale_url = esc_url_raw( tsdk_utmify( self::get_contact_base_link(), 'bfcm25', 'global_notice' ) );
1710+
$sale_url = add_query_arg(
1711+
[
1712+
'contact_subject' => rawurlencode( 'Upgrade to yearly plan - Black Friday offer' ),
1713+
'contact_website' => rawurlencode( home_url() ),
1714+
'contact_description' => rawurlencode(
1715+
'Hi, I saw the Black Friday offer about getting 15% OFF when switching from a monthly to a yearly plan. I would like to redeem this deal. Could you please help to switch my plan?'
1716+
),
1717+
],
1718+
$sale_url
1719+
);
1720+
1721+
return $sale_url;
1722+
}
1723+
16591724
/**
16601725
* Get all dashboard strings.
16611726
*

tests/test-admin.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,70 @@ public function test_get_bf_notices_date_boundaries() {
411411
$result = $this->admin->get_bf_notices( 'free' );
412412
$this->assertNotEmpty( $result, 'Should show notices at exact end time' );
413413
}
414+
415+
/**
416+
* Test add_black_friday_data yearly plans.
417+
*/
418+
public function test_add_black_friday_data_yearly_plans() {
419+
$base_config = [ 'default' => [ 'title' => 'Base', 'message' => 'Message' ] ];
420+
// Mock settings to return a yearly plan
421+
update_option( 'optml_settings', [ 'service_data' => [ 'plan' => 'starter-yearly' ] ] );
422+
// Reinitialize admin to pick up the new settings
423+
$this->admin = new Optml_Admin();
424+
$result = $this->admin->add_black_friday_data( $base_config );
425+
$this->assertEquals( $base_config, $result );
426+
$this->assertArrayNotHasKey( 'optimole-wp', $result );
427+
}
428+
429+
/**
430+
* Test add_black_friday_data free plan.
431+
*/
432+
public function test_add_black_friday_data_free_plan() {
433+
$base_config = [ 'default' => [ 'title' => 'Base', 'message' => 'Message' ] ];
434+
// Mock settings to return a free plan
435+
update_option( 'optml_settings', [ 'service_data' => [ 'plan' => 'free' ] ] );
436+
// Reinitialize admin to pick up the new settings
437+
$this->admin = new Optml_Admin();
438+
$result = $this->admin->add_black_friday_data( $base_config );
439+
$this->assertArrayHasKey( 'optimole-wp', $result );
440+
$config = $result['optimole-wp'];
441+
$this->assertStringContainsString( 'Black Friday', $config['title'] );
442+
$this->assertStringContainsString( 'BFCM2525', $config['message'] );
443+
$this->assertStringContainsString( '25% OFF', $config['message'] );
444+
$this->assertStringContainsString( 'upgrade', $config['sale_url'] );
445+
$this->assertTrue( $config['dismiss'] );
446+
}
447+
448+
/**
449+
* Test add_black_friday_data monthly plan.
450+
*/
451+
public function test_add_black_friday_data_monthly_plan() {
452+
$base_config = [ 'default' => [ 'title' => 'Base', 'message' => 'Message' ] ];
453+
// Mock settings to return a monthly plan
454+
update_option( 'optml_settings', [ 'service_data' => [ 'plan' => 'starter' ] ] );
455+
// Reinitialize admin to pick up the new settings
456+
$this->admin = new Optml_Admin();
457+
$result = $this->admin->add_black_friday_data( $base_config );
458+
$this->assertArrayHasKey( 'optimole-wp', $result );
459+
$config = $result['optimole-wp'];
460+
$this->assertStringContainsString( 'Black Friday', $config['title'] );
461+
$this->assertStringContainsString( 'Switch to a yearly plan', $config['message'] );
462+
$this->assertStringContainsString( '15% OFF', $config['message'] );
463+
$this->assertStringContainsString( 'contact', $config['sale_url'] );
464+
$this->assertTrue( $config['dismiss'] );
465+
}
466+
467+
/**
468+
* Test get_sale_url_for_monthly_plan.
469+
*/
470+
public function test_get_sale_url_for_monthly_plan() {
471+
$reflection = new \ReflectionMethod( $this->admin, 'get_sale_url_for_monthly_plan' );
472+
$reflection->setAccessible( true );
473+
$url = $reflection->invoke( $this->admin );
474+
475+
$this->assertStringContainsString( 'contact_subject', $url );
476+
$this->assertStringContainsString( 'contact_website', $url );
477+
$this->assertStringContainsString( 'contact_description', $url );
478+
$this->assertStringContainsString( 'Upgrade%20to%20yearly%20plan', $url );
479+
}
414480
}

0 commit comments

Comments
 (0)