module.php
192 lines
| 1 | <?php |
| 2 | namespace JFB_Modules\Promo_Banner; |
| 3 | |
| 4 | // If this file is called directly, abort. |
| 5 | if ( ! defined( 'WPINC' ) ) { |
| 6 | die; |
| 7 | } |
| 8 | |
| 9 | use JFB_Components\Module\Base_Module_Handle_It; |
| 10 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 11 | use JFB_Components\Module\Base_Module_It; |
| 12 | use JFB_Components\Module\Base_Module_Url_It; |
| 13 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 14 | use JFB_Components\Module\Base_Module_Dir_It; |
| 15 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 16 | |
| 17 | class Module implements Base_Module_It, Base_Module_Url_It, Base_Module_Handle_It, Base_Module_Dir_It { |
| 18 | |
| 19 | use Base_Module_Handle_Trait; |
| 20 | use Base_Module_Url_Trait; |
| 21 | use Base_Module_Dir_Trait; |
| 22 | |
| 23 | public $storage = null; |
| 24 | |
| 25 | public function rep_item_id() { |
| 26 | return 'promo-banner'; |
| 27 | } |
| 28 | |
| 29 | public function condition(): bool { |
| 30 | return ( is_admin() && current_user_can( 'manage_options' ) ) ? true : false; |
| 31 | } |
| 32 | |
| 33 | public function init_hooks() { |
| 34 | |
| 35 | add_action( 'admin_enqueue_scripts', array( $this, 'register_banner' ) ); |
| 36 | add_action( 'wp_ajax_' . $this->get_handle(), array( $this, 'process_banner_dismiss' ) ); |
| 37 | |
| 38 | add_filter( 'jet-form-builder/admin/pages/go-pro-title', array( $this, 'add_promo_disounts' ) ); |
| 39 | add_filter( 'jet-form-builder/admin/go-pro-link-title', array( $this, 'add_promo_disounts' ) ); |
| 40 | } |
| 41 | |
| 42 | public function remove_hooks() { |
| 43 | |
| 44 | remove_action( 'admin_enqueue_scripts', array( $this, 'register_banner' ) ); |
| 45 | remove_action( 'wp_ajax_' . $this->get_handle(), array( $this, 'process_banner_dismiss' ) ); |
| 46 | |
| 47 | remove_filter( 'jet-form-builder/admin/pages/go-pro-title', array( $this, 'add_promo_disounts' ) ); |
| 48 | remove_filter( 'jet-form-builder/admin/go-pro-link-title', array( $this, 'add_promo_disounts' ) ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get storage instance |
| 53 | * |
| 54 | * @return [type] [description] |
| 55 | */ |
| 56 | public function get_storage() { |
| 57 | |
| 58 | if ( null === $this->storage ) { |
| 59 | $this->storage = new Storage( $this->get_handle() ); |
| 60 | } |
| 61 | |
| 62 | return $this->storage; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Add promo discount text |
| 67 | * |
| 68 | * @param string $text Default text. |
| 69 | */ |
| 70 | public function add_promo_disounts( $text ) { |
| 71 | |
| 72 | $promo_value = $this->get_storage()->get_promo_value(); |
| 73 | |
| 74 | if ( $promo_value ) { |
| 75 | $text .= ' - ' . $promo_value; |
| 76 | } |
| 77 | |
| 78 | return $text; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Dismiss banner |
| 83 | * |
| 84 | * @return [type] [description] |
| 85 | */ |
| 86 | public function process_banner_dismiss() { |
| 87 | |
| 88 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 89 | if ( empty( $_REQUEST['nonce'] ) || ! wp_verify_nonce( wp_unslash( $_REQUEST['nonce'] ), $this->get_handle() ) ) { |
| 90 | wp_send_json_error( |
| 91 | esc_html__( 'The page is expired. Pleaser reload it and try again.', 'jet-form-builder' ) |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 96 | $hash = ! empty( $_REQUEST['hash'] ) ? sanitize_text_field( $_REQUEST['hash'] ) : false; |
| 97 | |
| 98 | if ( ! $hash ) { |
| 99 | wp_send_json_error( esc_html__( 'There is no banner hash do dimiss.', 'jet-form-builder' ) ); |
| 100 | } |
| 101 | |
| 102 | $storage = $this->get_storage(); |
| 103 | |
| 104 | if ( ! $storage->is_banner_dismissed( $hash ) ) { |
| 105 | $storage->dismiss_banner( $hash ); |
| 106 | } |
| 107 | |
| 108 | wp_send_json_success(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Check if is allowed page to show banner on |
| 113 | * |
| 114 | * @return boolean |
| 115 | */ |
| 116 | public function is_allowed_page() { |
| 117 | |
| 118 | if ( |
| 119 | ! empty( $_GET['post_type'] ) |
| 120 | && jet_form_builder()->post_type->slug() === $_GET['post_type'] |
| 121 | ) { |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Register banner to show on the page |
| 130 | * |
| 131 | * @return bool |
| 132 | */ |
| 133 | public function register_banner() { |
| 134 | |
| 135 | if ( ! $this->is_allowed_page() ) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | $storage = $this->get_storage(); |
| 140 | $banner = $storage->get_banner_html(); |
| 141 | |
| 142 | if ( ! $banner ) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | $hash = $storage->get_banner_hash( $banner ); |
| 147 | |
| 148 | if ( $storage->is_banner_dismissed( $hash ) ) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | $script_asset = require_once $this->get_dir( 'assets/build/index.asset.php' ); |
| 153 | |
| 154 | if ( true === $script_asset ) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | wp_enqueue_style( |
| 159 | $this->get_handle(), |
| 160 | $this->get_url( 'assets/build/index.css' ), |
| 161 | array(), |
| 162 | $script_asset['version'] |
| 163 | ); |
| 164 | |
| 165 | wp_enqueue_script( |
| 166 | $this->get_handle(), |
| 167 | $this->get_url( 'assets/build/index.js' ), |
| 168 | $script_asset['dependencies'], |
| 169 | $script_asset['version'], |
| 170 | true |
| 171 | ); |
| 172 | |
| 173 | $addition_classes = ''; |
| 174 | |
| 175 | if ( get_current_screen()->get_help_tabs() || get_current_screen()->show_screen_options() ) { |
| 176 | $addition_classes = 'has-screen-links'; |
| 177 | } |
| 178 | |
| 179 | wp_localize_script( |
| 180 | $this->get_handle(), |
| 181 | 'jfbPromoBanner', |
| 182 | array( |
| 183 | 'banner' => $banner, |
| 184 | 'hash' => $hash, |
| 185 | 'nonce' => wp_create_nonce( $this->get_handle() ), |
| 186 | 'action' => $this->get_handle(), |
| 187 | 'classes' => $addition_classes, |
| 188 | ) |
| 189 | ); |
| 190 | } |
| 191 | } |
| 192 |