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

Fixed imagify pricing modal layout issue #924

Merged
merged 18 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
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
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);
}
}
}
76 changes: 76 additions & 0 deletions classes/ThirdParty/Plugins/GravityForms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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' ) ) {
Khadreal marked this conversation as resolved.
Show resolved Hide resolved
return [
'gform_noconflict_styles' => 'imagify_gf_noconflict_styles',
'gform_noconflict_scripts' => 'imagify_gf_noconflict_scripts',
];
}

return [];
}

/**
* 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() ) {
Khadreal marked this conversation as resolved.
Show resolved Hide resolved
$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() ) {
Khadreal marked this conversation as resolved.
Show resolved Hide resolved
$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 is_plugin_active( 'gravityforms/gravityforms.php' )
Khadreal marked this conversation as resolved.
Show resolved Hide resolved
&& get_option( 'gform_enable_noconflict', false )
&& GFForms::is_gravity_page(); // @phpstan-ignore-line

Check failure on line 74 in classes/ThirdParty/Plugins/GravityForms.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

No error to ignore is reported on line 74.
}
}
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',
];
64 changes: 58 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 @@
*/
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 @@
// 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,60 @@
}
}

/**
* Check if menu is present
*
* @return bool
*/
private function admin_menu_is_present(): bool {

Check warning on line 650 in inc/classes/class-imagify-views.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/classes/class-imagify-views.php#L650

Avoid unused private methods such as 'admin_menu_is_present'.
Khadreal marked this conversation as resolved.
Show resolved Hide resolved
global $wp_admin_bar;

$imagify_menu_id = 'imagify';

return $wp_admin_bar && $wp_admin_bar->get_node( $imagify_menu_id );
}

/**
* 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
Loading