diff --git a/Tests/Fixtures/classes/ThirdParty/Plugins/GravityForm.php b/Tests/Fixtures/classes/ThirdParty/Plugins/GravityForm.php new file mode 100644 index 00000000..42795940 --- /dev/null +++ b/Tests/Fixtures/classes/ThirdParty/Plugins/GravityForm.php @@ -0,0 +1,8 @@ + [ + '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', + ] + ], + ], + ] +]; diff --git a/Tests/Unit/classes/ThirdParty/Plugins/gravityForms.php b/Tests/Unit/classes/ThirdParty/Plugins/gravityForms.php new file mode 100644 index 00000000..a6e7fe17 --- /dev/null +++ b/Tests/Unit/classes/ThirdParty/Plugins/gravityForms.php @@ -0,0 +1,47 @@ +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); + } + } +} diff --git a/classes/ThirdParty/Plugins/GravityForms.php b/classes/ThirdParty/Plugins/GravityForms.php new file mode 100644 index 00000000..608d9d9f --- /dev/null +++ b/classes/ThirdParty/Plugins/GravityForms.php @@ -0,0 +1,78 @@ + '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 ); + } +} diff --git a/classes/ThirdParty/ServiceProvider.php b/classes/ThirdParty/ServiceProvider.php new file mode 100644 index 00000000..bbd22496 --- /dev/null +++ b/classes/ThirdParty/ServiceProvider.php @@ -0,0 +1,59 @@ +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 ); + } +} diff --git a/config/providers.php b/config/providers.php index 9cdbbec9..8c3f4be0 100644 --- a/config/providers.php +++ b/config/providers.php @@ -7,4 +7,5 @@ 'Imagify\Picture\ServiceProvider', 'Imagify\Stats\ServiceProvider', 'Imagify\Webp\ServiceProvider', + 'Imagify\ThirdParty\ServiceProvider', ]; diff --git a/inc/classes/class-imagify-views.php b/inc/classes/class-imagify-views.php index 52eb092f..8b99ae50 100644 --- a/inc/classes/class-imagify-views.php +++ b/inc/classes/class-imagify-views.php @@ -76,6 +76,13 @@ class Imagify_Views { */ protected static $_instance; + /** + * Imagify admin bar menu. + * + * @var bool + */ + private $admin_menu_is_present = false; + /** ----------------------------------------------------------------------------------------- */ /** INSTANCE/INIT =========================================================================== */ @@ -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' ] ); } @@ -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(), + ] + ); + } } /**