Skip to content

Commit

Permalink
Fixed imagify pricing modal layout issue (#924)
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal authored Dec 20, 2024
1 parent d30b24c commit 260a3cc
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 6 deletions.
8 changes: 8 additions & 0 deletions Tests/Fixtures/classes/ThirdParty/Plugins/GravityForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
if ( ! class_exists( 'GFCommon' ) ) {
class GFForms{
public static function is_gravity_page() {
return true;
}
}
}
27 changes: 27 additions & 0 deletions Tests/Fixtures/classes/ThirdParty/Plugins/gravityForms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

return [
'test_data' => [
'testShouldReturnStylesAndScriptWhenConflictModeIsOn' => [
'config' => [
'is_plugin_active' => true,
'filter' => 'gform_noconflict_styles',
],
'expected' => [
'styles' => [
'imagify-admin-bar',
'imagify-admin',
'imagify-notices',
'imagify-pricing-modal',
],
'scripts' => [
'imagify-admin-bar',
'imagify-admin',
'imagify-notices',
'imagify-pricing-modal',
'imagify-sweetalert',
]
],
],
]
];
47 changes: 47 additions & 0 deletions Tests/Unit/classes/ThirdParty/Plugins/gravityForms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);

namespace ThirdParty\Plugins ;

use Imagify\Tests\Unit\TestCase;
use Imagify\ThirdParty\Plugins\GravityForms;
use Mockery;
use GFForms;
use Brain\Monkey\Functions;
use Brain\Monkey;

/**
* Tests for \Imagify\ThirdParty\Plugins\GravityForms.
*
* @covers \Imagify\ThirdParty\Plugins\GravityForms::imagify_gf_noconflict_styles
* @covers \Imagify\ThirdParty\Plugins\GravityForms::imagify_gf_noconflict_scripts
*
* @group ThirdParty
*/
class Test_GravityForms extends TestCase{

/**
* @dataProvider configTestData
*/
public function testShouldReturnAsExpected( $config, $expected ) {
Functions\when( 'is_plugin_active' )->justReturn( $config[ 'is_plugin_active' ] );
Functions\when( 'get_option' )->justReturn( $config[ 'is_plugin_active' ] );

$gf_forms = Mockery::mock('overload:' . GFForms::class);
$gf_forms->expects()->is_gravity_page()->andReturn( $config[ 'is_plugin_active' ] );

$gravity_forms = new GravityForms();

$styles = apply_filters( 'gform_noconflict_styles', [] );
$styles = $gravity_forms->imagify_gf_noconflict_styles( $styles );
foreach ( $expected['styles'] as $style ) {
$this->assertContains( $style, $styles );
}

$scripts = apply_filters( 'gform_noconflict_scripts', [] );
$scripts = $gravity_forms->imagify_gf_noconflict_scripts( $scripts );
foreach ( $expected['scripts'] as $script ) {
$this->assertContains( $script, $scripts);
}
}
}
78 changes: 78 additions & 0 deletions classes/ThirdParty/Plugins/GravityForms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);

namespace Imagify\ThirdParty\Plugins;

use GFForms;
use Imagify\EventManagement\SubscriberInterface;

/**
* Subscriber for compatibility with GravityForms
*/
class GravityForms implements SubscriberInterface {
/**
* Returns an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events(): array {
if ( ! class_exists( 'GFCommon' ) ) {
return [];
}

return [
'gform_noconflict_styles' => 'imagify_gf_noconflict_styles',
'gform_noconflict_scripts' => 'imagify_gf_noconflict_scripts',
];
}

/**
* Register imagify styles to gravity forms conflict styles
*
* @param array $styles Array fo registered styles.
*
* @return array
*/
public function imagify_gf_noconflict_styles( $styles ): array {
if ( ! $this->is_gravity_forms_no_conflict_mode_enabled() ) {
return $styles;
}

$styles[] = 'imagify-admin-bar';
$styles[] = 'imagify-admin';
$styles[] = 'imagify-notices';
$styles[] = 'imagify-pricing-modal';

return $styles;
}

/**
* Register Imagify scripts to gravity forms conflict scripts
*
* @param array $scripts Array fo registered scripts.
*
* @return array
*/
public function imagify_gf_noconflict_scripts( $scripts ): array {
if ( ! $this->is_gravity_forms_no_conflict_mode_enabled() ) {
return $scripts;
}

$scripts[] = 'imagify-admin-bar';
$scripts[] = 'imagify-sweetalert';
$scripts[] = 'imagify-admin';
$scripts[] = 'imagify-notices';
$scripts[] = 'imagify-pricing-modal';

return $scripts;
}

/**
* Check if gravity form is active and no_conflict mode is enabled.
*
* @return bool
*/
private function is_gravity_forms_no_conflict_mode_enabled(): bool {
return (bool) get_option( 'gform_enable_noconflict', false );
}
}
59 changes: 59 additions & 0 deletions classes/ThirdParty/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);

namespace Imagify\ThirdParty;

use Imagify\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use Imagify\ThirdParty\Plugins\GravityForms;

/**
* Service provider for Third Party(Plugins, Themes, Hosting).
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* Services provided by this provider
*
* @var array
*/
protected $provides = [
'gravity_from_subscriber',
];

/**
* Subscribers provided by this provider
*
* @var array
*/
public $subscribers = [
'gravity_from_subscriber',
];

/**
* Check if the service provider provides a specific service.
*
* @param string $id The id of the service.
*
* @return bool
*/
public function provides( string $id ): bool {
return in_array( $id, $this->provides, true );
}

/**
* Returns the subscribers array
*
* @return array
*/
public function get_subscribers() {
return $this->subscribers;
}

/**
* Registers the provided classes
*
* @return void
*/
public function register(): void {
$this->getContainer()->addShared( 'gravity_from_subscriber', GravityForms::class );
}
}
1 change: 1 addition & 0 deletions config/providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
'Imagify\Picture\ServiceProvider',
'Imagify\Stats\ServiceProvider',
'Imagify\Webp\ServiceProvider',
'Imagify\ThirdParty\ServiceProvider',
];
51 changes: 45 additions & 6 deletions inc/classes/class-imagify-views.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ class Imagify_Views {
*/
protected static $_instance;

/**
* Imagify admin bar menu.
*
* @var bool
*/
private $admin_menu_is_present = false;


/** ----------------------------------------------------------------------------------------- */
/** INSTANCE/INIT =========================================================================== */
Expand Down Expand Up @@ -127,6 +134,7 @@ public function init() {
// JS templates in footer.
add_action( 'admin_print_footer_scripts', [ $this, 'print_js_templates' ] );
add_action( 'admin_footer', [ $this, 'print_modal_payment' ] );
add_action( 'wp_before_admin_bar_render', [ $this, 'maybe_print_modal_payment' ] );
}


Expand Down Expand Up @@ -634,16 +642,47 @@ public function print_js_templates() {
}
}

/**
* Get imagify user info
*
* @return bool
*/
private function get_user_info(): bool {
$user = new User();
$unconsumed_quota = $user->get_percent_unconsumed_quota();

return ( ! $user->is_infinite() && $unconsumed_quota <= 20 )
|| ( $user->is_free() && $unconsumed_quota > 20 );
}

/**
* Start print the payment modal process.
*/
public function maybe_print_modal_payment() {
if ( $this->get_user_info() ) {
global $wp_admin_bar;
$this->admin_menu_is_present = $wp_admin_bar && $wp_admin_bar->get_node( 'imagify' );

return;
}

$this->admin_menu_is_present = false;
}

/**
* Print the payment modal.
*
* @return void
*/
public function print_modal_payment() {
$this->print_template(
'modal-payment',
[
'attachments_number' => $this->get_attachments_number_modal(),
]
);
if ( is_admin_bar_showing() && $this->admin_menu_is_present ) {
$this->print_template(
'modal-payment',
[
'attachments_number' => $this->get_attachments_number_modal(),
]
);
}
}

/**
Expand Down

0 comments on commit 260a3cc

Please sign in to comment.