class-email.php
3 years ago
class-giveaway.php
9 months ago
class-notice.php
9 months ago
class-rating.php
3 years ago
class-giveaway.php
273 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Giveaway notice class. |
| 4 | * |
| 5 | * @since 2.0 |
| 6 | * @author Incsub (Joel James) |
| 7 | * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 8 | * @copyright Copyright (c) 2022, Incsub |
| 9 | * @package WPMUDEV\Notices\Notices |
| 10 | */ |
| 11 | |
| 12 | namespace WPMUDEV\Notices\Notices; |
| 13 | |
| 14 | // If this file is called directly, abort. |
| 15 | defined( 'WPINC' ) || die; |
| 16 | |
| 17 | use WPMUDEV\Notices\Handler; |
| 18 | |
| 19 | if ( ! class_exists( __NAMESPACE__ . '\\Giveaway' ) ) { |
| 20 | /** |
| 21 | * Class Giveaway |
| 22 | * |
| 23 | * @since 2.0 |
| 24 | * @package WPMUDEV\Notices |
| 25 | */ |
| 26 | class Giveaway extends Notice { |
| 27 | |
| 28 | /** |
| 29 | * Current notice type. |
| 30 | * |
| 31 | * @since 2.0 |
| 32 | * @var string $type |
| 33 | */ |
| 34 | protected $type = 'giveaway'; |
| 35 | |
| 36 | /** |
| 37 | * Show after 2 days |
| 38 | * |
| 39 | * @since 2.0 |
| 40 | * @var string $type |
| 41 | */ |
| 42 | protected $time = DAY_IN_SECONDS * 2; // After 2 days. |
| 43 | |
| 44 | /** |
| 45 | * Allowed plugin IDs for the notice. |
| 46 | * |
| 47 | * @since 2.0 |
| 48 | * @var string[] $allowed_plugins |
| 49 | */ |
| 50 | protected $allowed_plugins = array( |
| 51 | 'defender', |
| 52 | 'smartcrawl', |
| 53 | 'forminator', |
| 54 | 'hustle', |
| 55 | 'branda', |
| 56 | 'beehive', |
| 57 | ); |
| 58 | |
| 59 | /** |
| 60 | * Initializes and returns the notice instance. |
| 61 | * |
| 62 | * @since 2.0 |
| 63 | * |
| 64 | * @param array $options Plugin options. |
| 65 | */ |
| 66 | protected function __construct( array $options ) { |
| 67 | parent::__construct( $options ); |
| 68 | |
| 69 | // Dismiss all plugin notices once giveaway is dismissed. |
| 70 | add_action( 'wpmudev_notices_after_notice_action', array( $this, 'maybe_dismiss_all' ), 10, 3 ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Render a notice type content. |
| 75 | * |
| 76 | * @since 2.0 |
| 77 | * |
| 78 | * @param string $plugin Plugin ID. |
| 79 | * |
| 80 | * @return void |
| 81 | */ |
| 82 | public function render( $plugin ) { |
| 83 | $this->enqueue_assets( $plugin ); |
| 84 | |
| 85 | echo '<div id="wpmudev-plugin-notices" class="wpmudev-plugin-notices sui-wrap"></div>'; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Enqueue assets for a notice if required. |
| 90 | * |
| 91 | * @since 2.0 |
| 92 | * |
| 93 | * @param string $plugin Plugin ID. |
| 94 | * |
| 95 | * @return void |
| 96 | */ |
| 97 | protected function enqueue_assets( $plugin ) { |
| 98 | $script_handle = 'wpmudev-notices-giveaway'; |
| 99 | |
| 100 | wp_enqueue_style( |
| 101 | $script_handle, |
| 102 | $this->assets_url( 'css/giveaway-banner.min.css' ), |
| 103 | array(), |
| 104 | Handler::instance()->version |
| 105 | ); |
| 106 | |
| 107 | wp_enqueue_script( |
| 108 | $script_handle, |
| 109 | $this->assets_url( 'js/giveaway-banner.min.js' ), |
| 110 | array( 'wp-element', 'wp-i18n' ), |
| 111 | Handler::instance()->version, |
| 112 | true |
| 113 | ); |
| 114 | |
| 115 | // Script vars. |
| 116 | wp_localize_script( |
| 117 | $script_handle, |
| 118 | 'wpmudevNoticeGiveaway', |
| 119 | array( |
| 120 | 'pluginId' => $plugin, |
| 121 | 'apiUrl' => $this->api_url( 'giveaway/v1/plugin' ), |
| 122 | 'images' => array( |
| 123 | 'form' => $this->assets_url( 'images/giveaway/form/' . $plugin . '.png' ), |
| 124 | 'form2x' => $this->assets_url( 'images/giveaway/form/' . $plugin . '@2x.png' ), |
| 125 | 'success' => $this->assets_url( 'images/giveaway/success/common.png' ), |
| 126 | 'success2x' => $this->assets_url( 'images/giveaway/success/common@2x.png' ), |
| 127 | ), |
| 128 | 'nonce' => wp_create_nonce( 'wpmudev_notices_action' ), |
| 129 | ) |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Check if current notice is allowed for the plugin. |
| 135 | * |
| 136 | * @since 2.0 |
| 137 | * |
| 138 | * @param string $plugin Plugin ID. |
| 139 | * |
| 140 | * @return bool |
| 141 | */ |
| 142 | public function can_show( $plugin ) { |
| 143 | // Should be a valid plugin and not on dashboard. |
| 144 | $allowed_plugins = in_array( $plugin, $this->allowed_plugins, true ); |
| 145 | // Check if WPMUDEV Dashboard plugin is active. |
| 146 | $dash_installed = class_exists( '\WPMUDEV_Dashboard' ); |
| 147 | |
| 148 | return $allowed_plugins && ! $dash_installed && ! $this->is_dismissed(); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Check if any of the plugins has already dismissed the notice. |
| 153 | * |
| 154 | * @since 2.0.1 |
| 155 | * |
| 156 | * @return bool |
| 157 | */ |
| 158 | private function is_dismissed() { |
| 159 | $option = Handler::instance()->get_option(); |
| 160 | |
| 161 | if ( ! empty( $option['done'] ) ) { |
| 162 | foreach ( $option['done'] as $notices ) { |
| 163 | // Remove from the queue. |
| 164 | if ( isset( $notices[ $this->type ] ) ) { |
| 165 | // Make sure to dismiss all. |
| 166 | $this->dismiss_all(); |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Parse options for the notice. |
| 178 | * |
| 179 | * @since 2.0 |
| 180 | * |
| 181 | * @param array $options Plugin options. |
| 182 | * |
| 183 | * @return array |
| 184 | */ |
| 185 | protected function parse_options( array $options ) { |
| 186 | return wp_parse_args( |
| 187 | $options, |
| 188 | array( |
| 189 | 'installed_on' => time() + $this->time, |
| 190 | ) |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Remove a notice from the queue. |
| 196 | * |
| 197 | * If a giveaway notice is dismissed permanently, we need to hide |
| 198 | * all plugins' giveaway notices. |
| 199 | * |
| 200 | * @since 2.0.1 |
| 201 | * |
| 202 | * @param string $action Action. |
| 203 | * @param string $plugin Plugin ID. |
| 204 | * @param string $type Notice type. |
| 205 | * |
| 206 | * @return void |
| 207 | */ |
| 208 | public function maybe_dismiss_all( $action, $plugin, $type ) { |
| 209 | // Not a giveaway notice. |
| 210 | if ( $this->type === $type && 'dismiss_notice' === $action ) { |
| 211 | $this->dismiss_all(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Mark all plugins' giveaway notices as done. |
| 217 | * |
| 218 | * @since 2.0.1 |
| 219 | * |
| 220 | * @return void |
| 221 | */ |
| 222 | private function dismiss_all() { |
| 223 | $option = Handler::instance()->get_option(); |
| 224 | |
| 225 | if ( ! empty( $option['queue'] ) ) { |
| 226 | foreach ( $option['queue'] as $plugin_id => $notices ) { |
| 227 | // Remove from the queue. |
| 228 | if ( isset( $notices[ $this->type ] ) ) { |
| 229 | unset( $option['queue'][ $plugin_id ][ $this->type ] ); |
| 230 | // Add to done list. |
| 231 | if ( ! isset( $option['done'][ $plugin_id ] ) ) { |
| 232 | $option['done'][ $plugin_id ] = array(); |
| 233 | } |
| 234 | $option['done'][ $plugin_id ][ $this->type ] = time(); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // Update the queue. |
| 239 | Handler::instance()->update_option( $option ); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Extend a notice to future time. |
| 245 | * |
| 246 | * If notice not found in queue, it will be added. |
| 247 | * |
| 248 | * @since 2.0 |
| 249 | * |
| 250 | * @param string $plugin Plugin ID. |
| 251 | * |
| 252 | * @return void |
| 253 | */ |
| 254 | public function extend_notice( $plugin ) { |
| 255 | $option = Handler::instance()->get_option(); |
| 256 | |
| 257 | if ( |
| 258 | isset( $option['plugins'][ $plugin ] ) // Only if already registered. |
| 259 | && ! isset( $option['done'][ $plugin ][ $this->type ] ) // Should not be in done list. |
| 260 | ) { |
| 261 | // Extend to future. |
| 262 | $option['queue'][ $plugin ][ $this->type ] = $this->get_next_schedule( |
| 263 | false, |
| 264 | DAY_IN_SECONDS * 30 // Extend 30 days. |
| 265 | ); |
| 266 | |
| 267 | // Update queue. |
| 268 | Handler::instance()->update_option( $option ); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 |