About_us.php
3 weeks ago
Abstract_Migration.php
2 months ago
Announcements.php
2 months ago
Compatibilities.php
2 years ago
Dashboard_widget.php
3 weeks ago
Featured_plugins.php
1 month ago
Float_widget.php
1 year ago
Licenser.php
2 months ago
Logger.php
10 months ago
Migrator.php
2 months ago
Notification.php
3 years ago
Promotions.php
3 weeks ago
Recommendation.php
3 years ago
Review.php
1 year ago
Rollback.php
1 year ago
Script_loader.php
1 year ago
Translate.php
5 years ago
Translations.php
1 year ago
Uninstall_feedback.php
2 days ago
Welcome.php
2 years ago
Welcome.php
195 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The welcome model class for ThemeIsle SDK |
| 4 | * |
| 5 | * Here's how to hook it in your plugin or theme: |
| 6 | * ```php |
| 7 | * add_filter( '<product_slug>_welcome_metadata', function() { |
| 8 | * return [ |
| 9 | * 'is_enabled' => <condition_if_pro_available>, |
| 10 | * 'pro_name' => 'Product PRO name', |
| 11 | * 'logo' => '<path_to_logo>', |
| 12 | * 'cta_link' => tsdk_utmify( 'https://link_to_upgrade.with/?discount=<discountCode>') |
| 13 | * ]; |
| 14 | * } ); |
| 15 | * ``` |
| 16 | * |
| 17 | * @package ThemeIsleSDK |
| 18 | * @subpackage Modules |
| 19 | * @copyright Copyright (c) 2023, Bogdan Preda |
| 20 | * @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 21 | * @since 1.0.0 |
| 22 | */ |
| 23 | |
| 24 | namespace ThemeisleSDK\Modules; |
| 25 | |
| 26 | // Exit if accessed directly. |
| 27 | use ThemeisleSDK\Common\Abstract_Module; |
| 28 | use ThemeisleSDK\Loader; |
| 29 | |
| 30 | if ( ! defined( 'ABSPATH' ) ) { |
| 31 | exit; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Promotions module for ThemeIsle SDK. |
| 36 | */ |
| 37 | class Welcome extends Abstract_Module { |
| 38 | |
| 39 | /** |
| 40 | * Debug mode. |
| 41 | * |
| 42 | * @var bool |
| 43 | */ |
| 44 | private $debug = false; |
| 45 | |
| 46 | /** |
| 47 | * Welcome metadata. |
| 48 | * |
| 49 | * @var array |
| 50 | */ |
| 51 | private $welcome_discounts = array(); |
| 52 | |
| 53 | /** |
| 54 | * Check that we can load this module. |
| 55 | * |
| 56 | * @param \ThemeisleSDK\Product $product The product. |
| 57 | * |
| 58 | * @return bool |
| 59 | */ |
| 60 | public function can_load( $product ) { |
| 61 | $this->debug = apply_filters( 'themeisle_sdk_welcome_debug', $this->debug ); |
| 62 | $welcome_metadata = apply_filters( $product->get_key() . '_welcome_metadata', array() ); |
| 63 | |
| 64 | $is_welcome_enabled = $this->is_welcome_meta_valid( $welcome_metadata ); |
| 65 | |
| 66 | if ( $is_welcome_enabled ) { |
| 67 | $this->welcome_discounts[ $product->get_key() ] = $welcome_metadata; |
| 68 | } |
| 69 | |
| 70 | return $this->debug || $is_welcome_enabled; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Check that the metadata is valid and the welcome is enabled. |
| 75 | * |
| 76 | * @param array $welcome_metadata The metadata to validate. |
| 77 | * |
| 78 | * @return bool |
| 79 | */ |
| 80 | private function is_welcome_meta_valid( $welcome_metadata ) { |
| 81 | return ! empty( $welcome_metadata ) && isset( $welcome_metadata['is_enabled'] ) && $welcome_metadata['is_enabled']; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Load the module. |
| 86 | * |
| 87 | * @param \ThemeisleSDK\Product $product The product. |
| 88 | * |
| 89 | * @return $this |
| 90 | */ |
| 91 | public function load( $product ) { |
| 92 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | $this->product = $product; |
| 97 | if ( ! $this->is_time_to_show_welcome() && $this->debug === false ) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | add_filter( 'themeisle_sdk_registered_notifications', [ $this, 'add_notification' ], 99, 1 ); |
| 102 | |
| 103 | return $this; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Check if it's time to show the welcome. |
| 108 | * |
| 109 | * @return bool |
| 110 | */ |
| 111 | private function is_time_to_show_welcome() { |
| 112 | // if 7 days from install have not passed, don't show the welcome. |
| 113 | if ( $this->product->get_install_time() + 7 * DAY_IN_SECONDS > time() ) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | // if 12 days from install have passed, don't show the welcome ( after 7 days for 5 days ). |
| 118 | if ( $this->product->get_install_time() + 12 * DAY_IN_SECONDS < time() ) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Add the welcome notification. |
| 127 | * Will block all other notifications if a welcome notification is present. |
| 128 | * |
| 129 | * @return array |
| 130 | */ |
| 131 | public function add_notification( $all_notifications ) { |
| 132 | if ( empty( $this->welcome_discounts ) ) { |
| 133 | return $all_notifications; |
| 134 | } |
| 135 | |
| 136 | if ( ! isset( $this->welcome_discounts[ $this->product->get_key() ] ) ) { |
| 137 | return $all_notifications; |
| 138 | } |
| 139 | |
| 140 | // filter out the notifications that are not welcome upsells |
| 141 | // if we arrived here we will have at least one welcome upsell |
| 142 | $all_notifications = array_filter( |
| 143 | $all_notifications, |
| 144 | function ( $notification ) { |
| 145 | return strpos( $notification['id'], '_welcome_upsell_flag' ) !== false; |
| 146 | } |
| 147 | ); |
| 148 | |
| 149 | $offer = $this->welcome_discounts[ $this->product->get_key() ]; |
| 150 | |
| 151 | $response = []; |
| 152 | $logo = isset( $offer['logo'] ) ? $offer['logo'] : ''; |
| 153 | $pro_name = isset( $offer['pro_name'] ) ? $offer['pro_name'] : $this->product->get_friendly_name() . ' PRO'; |
| 154 | |
| 155 | $link = $offer['cta_link']; |
| 156 | |
| 157 | $message = apply_filters( $this->product->get_key() . '_welcome_upsell_message', Loader::$labels['welcome']['message'] ); |
| 158 | |
| 159 | $button_submit = apply_filters( $this->product->get_key() . '_feedback_review_button_do', Loader::$labels['welcome']['ctay'] ); |
| 160 | $button_cancel = apply_filters( $this->product->get_key() . '_feedback_review_button_cancel', Loader::$labels['welcome']['ctan'] ); |
| 161 | $message = str_replace( |
| 162 | [ '{product}', '{pro_product}', '{cta_link}' ], |
| 163 | [ |
| 164 | $this->product->get_friendly_name(), |
| 165 | $pro_name, |
| 166 | $link, |
| 167 | ], |
| 168 | $message |
| 169 | ); |
| 170 | |
| 171 | $all_notifications[] = [ |
| 172 | 'id' => $this->product->get_key() . '_welcome_upsell_flag', |
| 173 | 'message' => $message, |
| 174 | 'img_src' => $logo, |
| 175 | 'ctas' => [ |
| 176 | 'confirm' => [ |
| 177 | 'link' => $link, |
| 178 | 'text' => $button_submit, |
| 179 | ], |
| 180 | 'cancel' => [ |
| 181 | 'link' => '#', |
| 182 | 'text' => $button_cancel, |
| 183 | ], |
| 184 | ], |
| 185 | 'type' => 'info', |
| 186 | ]; |
| 187 | |
| 188 | $key = array_rand( $all_notifications ); |
| 189 | $response[] = $all_notifications[ $key ]; |
| 190 | |
| 191 | return $response; |
| 192 | } |
| 193 | |
| 194 | } |
| 195 |