Skip to content

Commit

Permalink
ADD: API-driven promo banners Crocoblock/issues-tracker#11553
Browse files Browse the repository at this point in the history
  • Loading branch information
MjHead committed Sep 5, 2024
1 parent 4618faf commit 5458b61
Show file tree
Hide file tree
Showing 14 changed files with 485 additions and 2 deletions.
5 changes: 4 additions & 1 deletion includes/admin/pages/stable-pages-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ public function add_static_pages() {
$utm->set_content( $utm->get_license_and_theme() );

$pages[] = array(
'title' => __( 'Go PRO', 'jet-form-builder' ),
'title' => apply_filters(
'jet-form-builder/admin/pages/go-pro-title',
__( 'Go PRO', 'jet-form-builder' )
),
'capability' => 'manage_options',
'slug' => $utm->add_query( JET_FORM_BUILDER_SITE . '/pricing/' ),
);
Expand Down
5 changes: 4 additions & 1 deletion modules/admin/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ public function modify_plugin_action_links( array $links ): array {

$url = $utm->add_query( JET_FORM_BUILDER_SITE . '/pricing/' );

$label = __( 'Go Pro', 'jet-form-builder' );
$label = apply_filters(
'jet-form-builder/admin/go-pro-link-title',
__( 'Go Pro', 'jet-form-builder' )
);

$links['go_pro'] = "<a href=\"{$url}\" target=\"_blank\" class=\"jet-fb-go-pro-link\">{$label}</a>";

Expand Down
1 change: 1 addition & 0 deletions modules/modules-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function rep_instances(): array {
new Validation\Module(),
new Wysiwyg\Module(),
new Switcher\Module(),
new Promo_Banner\Module(),
);
}

Expand Down
3 changes: 3 additions & 0 deletions modules/promo-banner/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../.babelrc"
}
5 changes: 5 additions & 0 deletions modules/promo-banner/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
assets/src export-ignore
package.json export-ignore
.babelrc export-ignore
webpack.config.js export-ignore
.gitattributes export-ignore
1 change: 1 addition & 0 deletions modules/promo-banner/assets/build/index.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array(), 'version' => '1b38af3fde1368c66296');
1 change: 1 addition & 0 deletions modules/promo-banner/assets/build/index.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions modules/promo-banner/assets/build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions modules/promo-banner/assets/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import './index.scss';

( function( $ ) {

"use strict";

const JFBPromoCreateBanner = function( bannerHTML ) {

const banner = document.createElement( 'div' );
const bannerDismiss = document.createElement( 'a' );

banner.classList.add( 'jfb-promo-banner' );

if ( window.jfbPromoBanner.classes ) {
banner.classList.add( window.jfbPromoBanner.classes );
}

bannerDismiss.classList.add( 'jfb-promo-banner__dismiss' );

bannerDismiss.setAttribute( 'href', '#' );
bannerDismiss.setAttribute( 'aria-label', 'Dismiss JetFormBuilder Promo Banner' );
bannerDismiss.setAttribute( 'role', 'button' );
bannerDismiss.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><rect x="0" fill="none" width="20" height="20"/><g><path d="M14.95 6.46L11.41 10l3.54 3.54-1.41 1.41L10 11.42l-3.53 3.53-1.42-1.42L8.58 10 5.05 6.47l1.42-1.42L10 8.58l3.54-3.53z"/></g></svg>';

banner.innerHTML = bannerHTML;
banner.appendChild( bannerDismiss );

const bodyContent = document.getElementById( 'wpbody-content' );
const wrap = document.querySelector( '.wrap' );

if ( wrap ) {
wrap.prepend( banner );
} else {
bodyContent.prepend( banner );
}

$( banner ).on( 'click', '.jfb-promo-banner__dismiss', ( event ) => {

event.preventDefault();
banner.remove();

$.ajax({
url: window.ajaxurl,
type: 'POST',
dataType: 'json',
data: {
action: window.jfbPromoBanner.action,
hash: window.jfbPromoBanner.hash,
nonce: window.jfbPromoBanner.nonce,
},
});

});

}

if ( window.jfbPromoBanner ) {

$( window ).load( () => {
JFBPromoCreateBanner( window.jfbPromoBanner.banner );
} );

}

} ( jQuery ) );
23 changes: 23 additions & 0 deletions modules/promo-banner/assets/src/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.jfb-promo-banner {
margin: 15px 0 5px;
position: relative;
&.has-screen-links {
margin: 35px 0 5px;
}
&__dismiss {
position: absolute;
right: 0;
top: 50%;
margin-top: -18px;
margin-right: 10px;
width: 36px;
height: 36px;
display: flex;
justify-content:center;
align-items:center;
svg {
width: 24px;
height: 24px;
}
}
}
187 changes: 187 additions & 0 deletions modules/promo-banner/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php
namespace JFB_Modules\Promo_Banner;

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}

use JFB_Components\Module\Base_Module_Handle_It;
use JFB_Components\Module\Base_Module_Handle_Trait;
use JFB_Components\Module\Base_Module_It;
use JFB_Components\Module\Base_Module_Url_It;
use JFB_Components\Module\Base_Module_Url_Trait;
use JFB_Components\Module\Base_Module_Dir_It;
use JFB_Components\Module\Base_Module_Dir_Trait;

class Module implements Base_Module_It, Base_Module_Url_It, Base_Module_Handle_It, Base_Module_Dir_It {

use Base_Module_Handle_Trait;
use Base_Module_Url_Trait;
use Base_Module_Dir_Trait;

public $storage = null;

public function rep_item_id() {
return 'promo-banner';
}

public function condition() : bool {
return ( is_admin() && current_user_can( 'manage_options' ) ) ? true : false;
}

public function init_hooks() {

add_action( 'admin_enqueue_scripts', array( $this, 'register_banner' ) );
add_action( 'wp_ajax_' . $this->get_handle(), array( $this, 'process_banner_dismiss' ) );

add_filter( 'jet-form-builder/admin/pages/go-pro-title', array( $this, 'add_promo_disounts' ) );
add_filter( 'jet-form-builder/admin/go-pro-link-title', array( $this, 'add_promo_disounts' ) );
}

public function remove_hooks() {

remove_action( 'admin_enqueue_scripts', array( $this, 'register_banner' ) );
remove_action( 'wp_ajax_' . $this->get_handle(), array( $this, 'process_banner_dismiss' ) );

remove_filter( 'jet-form-builder/admin/pages/go-pro-title', array( $this, 'add_promo_disounts' ) );
remove_filter( 'jet-form-builder/admin/go-pro-link-title', array( $this, 'add_promo_disounts' ) );
}

/**
* Get storage instance
*
* @return [type] [description]
*/
public function get_storage() {

if ( null === $this->storage ) {
$this->storage = new Storage( $this->get_handle() );
}

return $this->storage;
}

/**
* Add promo discount text
*
* @param string $text Default text.
*/
public function add_promo_disounts( $text ) {

$promo_value = $this->get_storage()->get_promo_value();

if ( $promo_value ) {
$text .= ' - ' . $promo_value;
}

return $text;
}

/**
* Dismiss banner
*
* @return [type] [description]
*/
public function process_banner_dismiss() {

if ( empty( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], $this->get_handle() ) ) {
wp_send_json_error(
esc_html__( 'The page is expired. Pleaser reload it and try again.', 'jet-form-builder' )
);
}

$hash = ! empty( $_REQUEST['hash'] ) ? sanitize_text_field( $_REQUEST['hash'] ) : false;

if ( ! $hash ) {
wp_send_json_error( esc_html__( 'There is no banner hash do dimiss.', 'jet-form-builder' ) );
}

$storage = $this->get_storage();

if ( ! $storage->is_banner_dismissed( $hash ) ) {
$storage->dismiss_banner( $hash );
}

wp_send_json_success();

}

/**
* Check if is allowed page to show banner on
*
* @return boolean
*/
public function is_allowed_page() {

if (
! empty( $_GET['post_type'] )
&& jet_form_builder()->post_type->slug() === $_GET['post_type']
) {
return true;
}

return false;
}

/**
* Register banner to show on the page
*
* @return bool
*/
public function register_banner() {

if ( ! $this->is_allowed_page() ) {
return;
}

$storage = $this->get_storage();
$banner = $storage->get_banner_html();

if ( ! $banner ) {
return;
}

$hash = $storage->get_banner_hash( $banner );

if ( $storage->is_banner_dismissed( $hash ) ) {
return;
}

$script_asset = require_once $this->get_dir( 'assets/build/index.asset.php' );

if ( true === $script_asset ) {
return;
}

wp_enqueue_style(
$this->get_handle(),
$this->get_url( 'assets/build/index.css' ),
array(),
$script_asset['version']
);

wp_enqueue_script(
$this->get_handle(),
$this->get_url( 'assets/build/index.js' ),
$script_asset['dependencies'],
$script_asset['version'],
true
);

$addition_classes = '';

if ( get_current_screen()->get_help_tabs() || get_current_screen()->show_screen_options() ) {
$addition_classes = 'has-screen-links';
}

wp_localize_script( $this->get_handle(), 'jfbPromoBanner', array(
'banner' => $banner,
'hash' => $hash,
'nonce' => wp_create_nonce( $this->get_handle() ),
'action' => $this->get_handle(),
'classes' => $addition_classes,
) );

}
}
11 changes: 11 additions & 0 deletions modules/promo-banner/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "jet-form-builder-promo-banner",
"description": "Promo banner handler for JetFormBuilder Plugin",
"version": "1.0.0",
"license": "MIT",
"private": true,
"scripts": {
"build": "npx webpack build --mode production",
"dev": "npx webpack build --mode development -w"
}
}
Loading

0 comments on commit 5458b61

Please sign in to comment.