-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD: API-driven promo banners Crocoblock/issues-tracker#11553
- Loading branch information
Showing
14 changed files
with
485 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../.babelrc" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php return array('dependencies' => array(), 'version' => '1b38af3fde1368c66296'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) ); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.