| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base plugin class for WP Offload SES. |
| 4 |
* |
| 5 |
* @author Delicious Brains |
| 6 |
* @package WP Offload SES |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace DeliciousBrains\WP_Offload_SES; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class Plugin_Base |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
abstract class Plugin_Base { |
| 17 |
|
| 18 |
const DBRAINS_URL = 'https://deliciousbrains.com'; |
| 19 |
const SETTINGS_KEY = ''; |
| 20 |
const SETTINGS_CONSTANT = ''; |
| 21 |
|
| 22 |
/** |
| 23 |
* The plugin version. |
| 24 |
* |
| 25 |
* @var int |
| 26 |
*/ |
| 27 |
protected $plugin_version; |
| 28 |
|
| 29 |
/** |
| 30 |
* The plugin slug. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $plugin_slug; |
| 35 |
|
| 36 |
/** |
| 37 |
* The plugin basename. |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
protected $plugin_basename; |
| 42 |
|
| 43 |
/** |
| 44 |
* The path to the plugin file. |
| 45 |
* |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
protected $plugin_file_path; |
| 49 |
|
| 50 |
/** |
| 51 |
* The path to the plugin directory. |
| 52 |
* |
| 53 |
* @var string |
| 54 |
*/ |
| 55 |
protected $plugin_dir_path; |
| 56 |
|
| 57 |
/** |
| 58 |
* The current page. |
| 59 |
* |
| 60 |
* @var string |
| 61 |
*/ |
| 62 |
protected $plugin_pagenow; |
| 63 |
|
| 64 |
/** |
| 65 |
* The default tab to load. |
| 66 |
* |
| 67 |
* @var string |
| 68 |
*/ |
| 69 |
protected $default_tab = ''; |
| 70 |
|
| 71 |
/** |
| 72 |
* The slug for the plugin page. |
| 73 |
* |
| 74 |
* @var string |
| 75 |
*/ |
| 76 |
protected static $plugin_page = 'wp-offload-ses'; |
| 77 |
|
| 78 |
/** |
| 79 |
* Plugin settings class. |
| 80 |
* |
| 81 |
* @var object |
| 82 |
*/ |
| 83 |
public $settings; |
| 84 |
|
| 85 |
/** |
| 86 |
* Construct the Plugin_Base class. |
| 87 |
* |
| 88 |
* @param string $plugin_file_path The path to the plugin file. |
| 89 |
*/ |
| 90 |
public function __construct( $plugin_file_path ) { |
| 91 |
$this->plugin_file_path = $plugin_file_path; |
| 92 |
$this->plugin_dir_path = $this->get_plugin_dir_path(); |
| 93 |
$this->plugin_basename = plugin_basename( $plugin_file_path ); |
| 94 |
$this->plugin_pagenow = is_network_admin() ? 'settings.php' : 'options-general.php'; |
| 95 |
$this->settings = new Settings( static::SETTINGS_KEY, static::SETTINGS_CONSTANT ); |
| 96 |
|
| 97 |
if ( $this->plugin_slug && isset( $GLOBALS['wposes_meta'][ $this->plugin_slug ]['version'] ) ) { |
| 98 |
$this->plugin_version = $GLOBALS['wposes_meta'][ $this->plugin_slug ]['version']; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Accessor for plugin version |
| 104 |
* |
| 105 |
* @return mixed |
| 106 |
*/ |
| 107 |
public function get_plugin_version() { |
| 108 |
return $this->plugin_version; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Accessor for plugin slug |
| 113 |
* |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
public function get_plugin_slug() { |
| 117 |
return $this->plugin_slug; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Accessor for plugin basename |
| 122 |
* |
| 123 |
* @return string |
| 124 |
*/ |
| 125 |
public function get_plugin_basename() { |
| 126 |
return $this->plugin_basename; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Accessor for plugin file path |
| 131 |
* |
| 132 |
* @return string |
| 133 |
*/ |
| 134 |
public function get_plugin_file_path() { |
| 135 |
return $this->plugin_file_path; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Accessor for plugin dir path |
| 140 |
* |
| 141 |
* @return string |
| 142 |
*/ |
| 143 |
public function get_plugin_dir_path() { |
| 144 |
if ( function_exists( 'wposes_get_plugin_dir_path' ) ) { |
| 145 |
$this->plugin_dir_path = wposes_get_plugin_dir_path(); |
| 146 |
} else { |
| 147 |
$this->plugin_dir_path = wposes_lite_get_plugin_dir_path(); |
| 148 |
} |
| 149 |
|
| 150 |
return $this->plugin_dir_path; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Accessor for plugin pagenow |
| 155 |
* |
| 156 |
* @return string |
| 157 |
*/ |
| 158 |
public function get_plugin_pagenow() { |
| 159 |
return $this->plugin_pagenow; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Helper method to return the settings page URL for the plugin |
| 164 |
* |
| 165 |
* @param array $args Args to pass to URL. |
| 166 |
* @param string $url_method To prepend to admin_url(). |
| 167 |
* @param bool $escape Should we escape the URL. |
| 168 |
* |
| 169 |
* @return string |
| 170 |
*/ |
| 171 |
public function get_plugin_page_url( $args = array(), $url_method = 'network', $escape = true ) { |
| 172 |
$default_args = array( |
| 173 |
'page' => static::$plugin_page, |
| 174 |
); |
| 175 |
|
| 176 |
$args = array_merge( $default_args, $args ); |
| 177 |
|
| 178 |
switch ( $url_method ) { |
| 179 |
case 'self': |
| 180 |
$base_url = self_admin_url( $this->get_plugin_pagenow() ); |
| 181 |
break; |
| 182 |
default: |
| 183 |
$pagenow = is_multisite() ? 'settings.php' : 'options-general.php'; |
| 184 |
$base_url = network_admin_url( $pagenow ); |
| 185 |
} |
| 186 |
|
| 187 |
// Add a hash to the URL. |
| 188 |
$hash = false; |
| 189 |
|
| 190 |
if ( isset( $args['hash'] ) ) { |
| 191 |
$hash = $args['hash']; |
| 192 |
unset( $args['hash'] ); |
| 193 |
} else if ( $this->default_tab ) { |
| 194 |
$hash = $this->default_tab; |
| 195 |
} |
| 196 |
|
| 197 |
$url = add_query_arg( $args, $base_url ); |
| 198 |
|
| 199 |
if ( $hash ) { |
| 200 |
$url .= '#' . $hash; |
| 201 |
} |
| 202 |
|
| 203 |
if ( $escape ) { |
| 204 |
$url = esc_url_raw( $url ); |
| 205 |
} |
| 206 |
|
| 207 |
return $url; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Enqueue script. |
| 212 |
* |
| 213 |
* @param string $handle The handle of the script. |
| 214 |
* @param string $path The path to the script. |
| 215 |
* @param array $deps Script dependencies. |
| 216 |
* @param bool $footer If it should load in the footer. |
| 217 |
*/ |
| 218 |
public function enqueue_script( $handle, $path, $deps = array(), $footer = true ) { |
| 219 |
$version = $this->get_asset_version(); |
| 220 |
$suffix = $this->get_asset_suffix(); |
| 221 |
|
| 222 |
$src = $this->plugins_url( $path . $suffix . '.js' ); |
| 223 |
wp_enqueue_script( $handle, $src, $deps, $version, $footer ); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Enqueue style. |
| 228 |
* |
| 229 |
* @param string $handle Handle of the style. |
| 230 |
* @param string $path Path of the stylesheet. |
| 231 |
* @param array $deps Stylesheet dependencies. |
| 232 |
*/ |
| 233 |
public function enqueue_style( $handle, $path, $deps = array() ) { |
| 234 |
$version = $this->get_asset_version(); |
| 235 |
|
| 236 |
$src = $this->plugins_url( $path . '.css' ); |
| 237 |
wp_enqueue_style( $handle, $src, $deps, $version ); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Wrapper for plugins_url() to account for mu-plugins. |
| 242 |
* |
| 243 |
* @param string $path The path to the asset. |
| 244 |
* |
| 245 |
* @return string |
| 246 |
*/ |
| 247 |
public function plugins_url( $path ) { |
| 248 |
return plugins_url( $path, $this->plugin_dir_path . '/wp-offload-ses.php' ); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Get the version used for script enqueuing |
| 253 |
* |
| 254 |
* @return mixed |
| 255 |
*/ |
| 256 |
public function get_asset_version() { |
| 257 |
return defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? time() : $this->plugin_version; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Get the filename suffix used for script enqueuing |
| 262 |
* |
| 263 |
* @return mixed |
| 264 |
*/ |
| 265 |
public function get_asset_suffix() { |
| 266 |
return defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Render a view template file |
| 271 |
* |
| 272 |
* @param string $view View filename without the extension. |
| 273 |
* @param array $args Arguments to pass to the view. |
| 274 |
*/ |
| 275 |
public function render_view( $view, $args = array() ) { |
| 276 |
extract( $args ); // phpcs:ignore |
| 277 |
include $this->plugin_dir_path . '/view/' . $view . '.php'; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Generate site URL with correct UTM tags. |
| 282 |
* |
| 283 |
* @param string $path URL path. |
| 284 |
* @param array $args URL args. |
| 285 |
* @param string $hash URL hash. |
| 286 |
* |
| 287 |
* @return string |
| 288 |
*/ |
| 289 |
public function dbrains_url( $path, $args = array(), $hash = '' ) { |
| 290 |
$args = wp_parse_args( |
| 291 |
$args, |
| 292 |
array( |
| 293 |
'utm_medium' => 'insideplugin', |
| 294 |
'utm_source' => 'SES', |
| 295 |
) |
| 296 |
); |
| 297 |
$args = array_map( 'urlencode', $args ); |
| 298 |
$url = trailingslashit( self::DBRAINS_URL ) . ltrim( $path, '/' ); |
| 299 |
$url = add_query_arg( $args, $url ); |
| 300 |
|
| 301 |
if ( $hash ) { |
| 302 |
$url .= '#' . $hash; |
| 303 |
} |
| 304 |
|
| 305 |
return $url; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Get the My Account URL |
| 310 |
* |
| 311 |
* @param array $args Optional args to add. |
| 312 |
* @param string $hash Optional hash to add. |
| 313 |
* |
| 314 |
* @return string |
| 315 |
*/ |
| 316 |
public function get_my_account_url( $args = array(), $hash = '' ) { |
| 317 |
return $this->dbrains_url( '/my-account/', $args, $hash ); |
| 318 |
} |
| 319 |
|
| 320 |
} |
| 321 |
|