class-email.php
3 years ago
class-giveaway.php
11 months ago
class-notice.php
11 months ago
class-rating.php
3 years ago
class-notice.php
234 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin notice base 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 | if ( ! class_exists( __NAMESPACE__ . '\\Notice' ) ) { |
| 18 | /** |
| 19 | * Class Notice |
| 20 | * |
| 21 | * @since 2.0 |
| 22 | * @package WPMUDEV\Notices |
| 23 | */ |
| 24 | abstract class Notice { |
| 25 | |
| 26 | /** |
| 27 | * Current notice type (Should override in sub class). |
| 28 | * |
| 29 | * @var string $type |
| 30 | * |
| 31 | * @since 2.0 |
| 32 | */ |
| 33 | protected $type; |
| 34 | |
| 35 | /** |
| 36 | * Time to start showing notice. |
| 37 | * |
| 38 | * This will be added to the current time. |
| 39 | * |
| 40 | * @var string $type |
| 41 | * |
| 42 | * @since 2.0 |
| 43 | */ |
| 44 | protected $time = 0; // Right now. |
| 45 | |
| 46 | /** |
| 47 | * Current plugin's notice options. |
| 48 | * |
| 49 | * @var string $plugin_id |
| 50 | * |
| 51 | * @since 2.0 |
| 52 | */ |
| 53 | protected $options = array(); |
| 54 | |
| 55 | /** |
| 56 | * Initializes and returns the notice instance. |
| 57 | * |
| 58 | * @param array $options Plugin options. |
| 59 | * |
| 60 | * @since 2.0 |
| 61 | */ |
| 62 | protected function __construct( array $options ) { |
| 63 | // Set options. |
| 64 | $this->options = $this->parse_options( $options ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Initializes and returns the singleton instance. |
| 69 | * |
| 70 | * @param array $options Plugin options. |
| 71 | * |
| 72 | * @since 2.0 |
| 73 | * |
| 74 | * @return static |
| 75 | */ |
| 76 | public static function instance( array $options = array() ) { |
| 77 | static $instance = null; |
| 78 | |
| 79 | if ( null === $instance ) { |
| 80 | $called_class = get_called_class(); |
| 81 | $instance = new $called_class( $options ); |
| 82 | } |
| 83 | |
| 84 | return $instance; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Render a notice type content. |
| 89 | * |
| 90 | * @param string $plugin Plugin ID. |
| 91 | * |
| 92 | * @since 2.0 |
| 93 | * |
| 94 | * @return void |
| 95 | */ |
| 96 | abstract public function render( $plugin ); |
| 97 | |
| 98 | /** |
| 99 | * Check if current notice is allowed for the plugin. |
| 100 | * |
| 101 | * @param string $plugin Plugin ID. |
| 102 | * |
| 103 | * @since 2.0 |
| 104 | * |
| 105 | * @return bool |
| 106 | */ |
| 107 | public function can_show( $plugin ) { |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Enqueue assets for a notice if required. |
| 113 | * |
| 114 | * @param string $plugin Plugin ID. |
| 115 | * |
| 116 | * @since 2.0 |
| 117 | * |
| 118 | * @return void |
| 119 | */ |
| 120 | protected function enqueue_assets( $plugin ) { |
| 121 | // Override to enqueue. |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Parse options for the notice. |
| 126 | * |
| 127 | * @param array $options Plugin options. |
| 128 | * |
| 129 | * @since 2.0 |
| 130 | * |
| 131 | * @return array |
| 132 | */ |
| 133 | protected function parse_options( array $options ) { |
| 134 | return $options; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get a notice option value. |
| 139 | * |
| 140 | * @param string $key Option name. |
| 141 | * @param mixed $default Default value. |
| 142 | * |
| 143 | * @since 2.0 |
| 144 | * |
| 145 | * @return array |
| 146 | */ |
| 147 | protected function get_option( $key, $default = '' ) { |
| 148 | if ( isset( $this->options[ $key ] ) ) { |
| 149 | return $this->options[ $key ]; |
| 150 | } |
| 151 | |
| 152 | return $default; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get next scheduled time to show notice. |
| 157 | * |
| 158 | * @param int $time Current time. |
| 159 | * @param int $extend How many days to extend. |
| 160 | * |
| 161 | * @since 2.0 |
| 162 | * |
| 163 | * @return int |
| 164 | */ |
| 165 | public function get_next_schedule( $time = false, $extend = false ) { |
| 166 | // Use current time. |
| 167 | if ( ! is_int( $time ) ) { |
| 168 | $time = time(); |
| 169 | } |
| 170 | |
| 171 | // Use extension time. |
| 172 | if ( ! is_int( $extend ) ) { |
| 173 | $extend = $this->time; |
| 174 | } |
| 175 | |
| 176 | return $time + $extend; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Get full url to an asset. |
| 181 | * |
| 182 | * @param string $path Path to append. |
| 183 | * |
| 184 | * @since 2.0 |
| 185 | * |
| 186 | * @return string |
| 187 | */ |
| 188 | protected function assets_url( $path ) { |
| 189 | return plugin_dir_url( WPMUDEV_NOTICES_FILE ) . 'assets/' . $path; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get the full url to the API endpoint. |
| 194 | * |
| 195 | * @param string $endpoint API endpoint. |
| 196 | * |
| 197 | * @since 2.0 |
| 198 | * |
| 199 | * @return string |
| 200 | */ |
| 201 | protected function api_url( $endpoint ) { |
| 202 | $base = 'https://wpmudev.com/'; |
| 203 | |
| 204 | // Support custom API base. |
| 205 | if ( defined( 'WPMUDEV_CUSTOM_API_SERVER' ) && ! empty( WPMUDEV_CUSTOM_API_SERVER ) ) { |
| 206 | $base = trailingslashit( WPMUDEV_CUSTOM_API_SERVER ); |
| 207 | } |
| 208 | |
| 209 | // Append endpoint. |
| 210 | return $base . 'api/' . $endpoint; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Get current screen id. |
| 215 | * |
| 216 | * @since 2.0 |
| 217 | * |
| 218 | * @return string |
| 219 | */ |
| 220 | protected function screen_id() { |
| 221 | // Screen not defined yet. |
| 222 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 223 | return ''; |
| 224 | } |
| 225 | |
| 226 | // Get current screen. |
| 227 | $screen = get_current_screen(); |
| 228 | |
| 229 | // Return current screen id. |
| 230 | return empty( $screen->id ) ? '' : $screen->id; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 |