| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Squiz.NamingConventions.ValidVariableName |
| 4 |
|
| 5 |
namespace BitApps\Integrations; |
| 6 |
|
| 7 |
use BitApps\Integrations\Core\Util\DateTimeHelper; |
| 8 |
use BitApps\Integrations\Core\Util\Hooks; |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Provides App configurations. |
| 16 |
*/ |
| 17 |
class Config |
| 18 |
{ |
| 19 |
public const SLUG = 'bit-integrations'; |
| 20 |
|
| 21 |
public const TITLE = 'Bit Integrations'; |
| 22 |
|
| 23 |
public const VAR_PREFIX = 'bit_integrations_'; |
| 24 |
|
| 25 |
public const VERSION = '2.9.2'; |
| 26 |
|
| 27 |
public const DB_VERSION = '1.1'; |
| 28 |
|
| 29 |
public const REQUIRED_PHP_VERSION = '7.4'; |
| 30 |
|
| 31 |
public const REQUIRED_WP_VERSION = '5.1'; |
| 32 |
|
| 33 |
public const API_VERSION = '1.0'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Provides configuration for plugin. |
| 37 |
* |
| 38 |
* @param string $type Type of conf |
| 39 |
* @param string $default Default value |
| 40 |
* |
| 41 |
* @return array|string|null |
| 42 |
*/ |
| 43 |
public static function get($type, $default = null) |
| 44 |
{ |
| 45 |
switch ($type) { |
| 46 |
case 'MAIN_FILE': |
| 47 |
return BIT_INTEGRATIONS_PLUGIN_FILE; |
| 48 |
|
| 49 |
case 'BASENAME': |
| 50 |
return plugin_basename(trim(self::get('MAIN_FILE'))); |
| 51 |
|
| 52 |
case 'BASEDIR': |
| 53 |
return plugin_dir_path(self::get('MAIN_FILE')); |
| 54 |
|
| 55 |
case 'BACKEND_DIR': |
| 56 |
return plugin_dir_path(self::get('MAIN_FILE')) . 'backend'; |
| 57 |
|
| 58 |
case 'SITE_URL': |
| 59 |
$parsedUrl = wp_parse_url(get_admin_url()); |
| 60 |
$siteUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host']; |
| 61 |
$siteUrl .= empty($parsedUrl['port']) ? null : ':' . $parsedUrl['port']; |
| 62 |
|
| 63 |
return $siteUrl; |
| 64 |
|
| 65 |
case 'ADMIN_URL': |
| 66 |
return str_replace(self::get('SITE_URL'), '', get_admin_url()); |
| 67 |
|
| 68 |
case 'API_URL': |
| 69 |
return get_rest_url(null, '/' . self::SLUG . '/v1'); |
| 70 |
|
| 71 |
case 'ROOT_URI': |
| 72 |
return set_url_scheme(plugins_url('', self::get('MAIN_FILE')), wp_parse_url(home_url())['scheme']); |
| 73 |
|
| 74 |
case 'ASSET_URI': |
| 75 |
return self::get('ROOT_URI') . '/assets'; |
| 76 |
|
| 77 |
case 'PLUGIN_PAGE_LINKS': |
| 78 |
return self::pluginPageLinks(); |
| 79 |
|
| 80 |
case 'WP_DB_PREFIX': |
| 81 |
global $wpdb; |
| 82 |
|
| 83 |
return $wpdb->prefix; |
| 84 |
|
| 85 |
default: |
| 86 |
return $default; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Prefixed variable name with prefix. |
| 92 |
* |
| 93 |
* @param string $option Variable name |
| 94 |
* |
| 95 |
* @return string |
| 96 |
*/ |
| 97 |
public static function withPrefix($option) |
| 98 |
{ |
| 99 |
return self::VAR_PREFIX . $option; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Retrieves options from option table. |
| 104 |
* |
| 105 |
* @param string $option Option name |
| 106 |
* @param bool $default default value |
| 107 |
* @param bool $wp Whether option is default wp option |
| 108 |
* |
| 109 |
* @return mixed |
| 110 |
*/ |
| 111 |
public static function getOption($option, $default = false, $wp = false) |
| 112 |
{ |
| 113 |
if ($wp) { |
| 114 |
return get_option($option, $default); |
| 115 |
} |
| 116 |
|
| 117 |
return get_option(self::withPrefix($option), $default); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Saves option to option table. |
| 122 |
* |
| 123 |
* @param string $option Option name |
| 124 |
* @param bool $autoload Whether option will autoload |
| 125 |
* @param mixed $value |
| 126 |
* |
| 127 |
* @return bool |
| 128 |
*/ |
| 129 |
public static function addOption($option, $value, $autoload = false) |
| 130 |
{ |
| 131 |
return add_option(self::withPrefix($option), $value, '', $autoload ? 'yes' : 'no'); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Delete option from option table. |
| 136 |
* |
| 137 |
* @param string $option Option name |
| 138 |
*/ |
| 139 |
public static function deleteOption($option) |
| 140 |
{ |
| 141 |
return delete_option(self::withPrefix($option)); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Save or update option to option table. |
| 146 |
* |
| 147 |
* @param string $option Option name |
| 148 |
* @param mixed $value Option value |
| 149 |
* @param bool $autoload Whether option will autoload |
| 150 |
* |
| 151 |
* @return bool |
| 152 |
*/ |
| 153 |
public static function updateOption($option, $value, $autoload = null) |
| 154 |
{ |
| 155 |
return update_option(self::withPrefix($option), $value, !\is_null($autoload) ? 'yes' : null); |
| 156 |
} |
| 157 |
|
| 158 |
public static function isDev() |
| 159 |
{ |
| 160 |
return is_readable(Config::get('BASEDIR') . '.port'); |
| 161 |
} |
| 162 |
|
| 163 |
public static function getDevUrl() |
| 164 |
{ |
| 165 |
if (self::isDev()) { |
| 166 |
$port = self::getDevPort(); |
| 167 |
|
| 168 |
return "http://localhost:{$port}"; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
public static function getDevPort() |
| 173 |
{ |
| 174 |
return self::isDev() ? file_get_contents(Config::get('BASEDIR') . '/.port') : null; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Build and return the frontend config array for localization |
| 179 |
* |
| 180 |
* @return array |
| 181 |
*/ |
| 182 |
public static function getFrontendConfig() |
| 183 |
{ |
| 184 |
$frontendConfig = apply_filters( |
| 185 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- hook is prefixed via Config::VAR_PREFIX. |
| 186 |
Config::withPrefix('localized_script'), |
| 187 |
[ |
| 188 |
'nonce' => wp_create_nonce(Config::withPrefix('nonce')), |
| 189 |
'assetsURL' => Config::get('ASSET_URI'), |
| 190 |
'baseURL' => get_admin_url(null, 'admin.php?page=bit-integrations#'), |
| 191 |
'siteURL' => site_url(), |
| 192 |
'ajaxURL' => admin_url('admin-ajax.php'), |
| 193 |
'api' => Config::get('API_URL'), |
| 194 |
'dateFormat' => get_option('date_format'), |
| 195 |
'timeFormat' => get_option('time_format'), |
| 196 |
'timeZone' => DateTimeHelper::wp_timezone_string(), |
| 197 |
'userMail' => self::getUserMails(), |
| 198 |
'currentUser' => wp_get_current_user(), |
| 199 |
'version' => defined('BTCBI_PRO_VERSION') ? \constant('BTCBI_PRO_VERSION') : Config::VERSION |
| 200 |
] |
| 201 |
); |
| 202 |
|
| 203 |
/** |
| 204 |
* @deprecated 2.7.8 Use `bit_integrations_localized_script` filter instead. |
| 205 |
* @since 2.7.8 |
| 206 |
*/ |
| 207 |
$frontendConfig = Hooks::apply('btcbi_localized_script', $frontendConfig); |
| 208 |
|
| 209 |
$changelogVersion = Config::getOption('changelog_version', '0.0.0'); |
| 210 |
|
| 211 |
if (version_compare($changelogVersion, '0.0.0', '==')) { |
| 212 |
/** |
| 213 |
* @deprecated 2.7.8 Use `bit_integrations_changelog_version` option instead. |
| 214 |
* @since 2.7.8 |
| 215 |
*/ |
| 216 |
$frontendConfig['changelogVersion'] = Config::getOption('btcbi_changelog_version', $changelogVersion, true); |
| 217 |
} else { |
| 218 |
$frontendConfig['changelogVersion'] = $changelogVersion; |
| 219 |
} |
| 220 |
|
| 221 |
if ((get_locale() !== 'en_US' || get_user_locale() !== 'en_US') && file_exists(Config::get('BASEDIR') . '/languages/generatedString.php')) { |
| 222 |
include_once Config::get('BASEDIR') . '/languages/generatedString.php'; |
| 223 |
if (isset($bit_integrations_i18n_strings)) { |
| 224 |
$frontendConfig['translations'] = $bit_integrations_i18n_strings; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
return $frontendConfig; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Get list of users with their email addresses. |
| 233 |
* |
| 234 |
* @param string $capability Capability required to retrieve users. Defaults to 'manage_options'. |
| 235 |
* |
| 236 |
* @return array Array of users with id, label (display name), and value (email). |
| 237 |
*/ |
| 238 |
public static function getUserMails($capability = 'manage_options') |
| 239 |
{ |
| 240 |
$userMails = []; |
| 241 |
if (current_user_can($capability)) { |
| 242 |
$users = get_users(['fields' => ['ID', 'user_nicename', 'user_email', 'display_name']]); |
| 243 |
foreach ($users as $key => $user) { |
| 244 |
$userMails[$key]['label'] = !empty($user->display_name) ? $user->display_name : ''; |
| 245 |
$userMails[$key]['value'] = !empty($user->user_email) ? $user->user_email : ''; |
| 246 |
$userMails[$key]['id'] = $user->ID; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
return $userMails; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Provides links for plugin pages. Those links will bi displayed in |
| 255 |
* all plugin pages under the plugin name. |
| 256 |
* |
| 257 |
* @return array |
| 258 |
*/ |
| 259 |
private static function pluginPageLinks() |
| 260 |
{ |
| 261 |
return [ |
| 262 |
'settings' => [ |
| 263 |
'title' => __('Settings', 'bit-integrations'), |
| 264 |
'url' => self::get('ADMIN_URL') . 'admin.php?page=' . self::SLUG . '#settings', |
| 265 |
], |
| 266 |
'help' => [ |
| 267 |
'title' => __('Help', 'bit-integrations'), |
| 268 |
'url' => self::get('ADMIN_URL') . 'admin.php?page=' . self::SLUG . '#help', |
| 269 |
], |
| 270 |
]; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Provides menus for wordpress admin sidebar. |
| 275 |
* should return an array of menus with the following structure: |
| 276 |
* [ |
| 277 |
* 'type' => menu | submenu, |
| 278 |
* 'name' => 'Name of menu will shown in sidebar', |
| 279 |
* 'capability' => 'capability required to access menu', |
| 280 |
* 'slug' => 'slug of menu after ?page=',. |
| 281 |
* |
| 282 |
* 'title' => 'page title will be shown in browser title if type is menu', |
| 283 |
* 'callback' => 'function to call when menu is clicked', |
| 284 |
* 'icon' => 'icon to display in menu if menu type is menu', |
| 285 |
* 'position' => 'position of menu in sidebar if menu type is menu', |
| 286 |
* |
| 287 |
* 'parent' => 'parent slug if submenu' |
| 288 |
* ] |
| 289 |
* |
| 290 |
* @return array |
| 291 |
*/ |
| 292 |
// private static function sideBarMenu() |
| 293 |
// { |
| 294 |
// $adminViews = new Layout(); |
| 295 |
|
| 296 |
// return [ |
| 297 |
// 'Home' => [ |
| 298 |
// 'type' => 'menu', |
| 299 |
// 'title' => __('Bit Integrations', 'bit-integrations'), |
| 300 |
// 'name' => __('Bit Integrations', 'bit-integrations'), |
| 301 |
// 'capability' => 'manage_options', |
| 302 |
// 'slug' => self::SLUG, |
| 303 |
// 'callback' => [$adminViews, 'body'], |
| 304 |
// 'icon' => 'dashicons-admin-home', |
| 305 |
// 'position' => '20', |
| 306 |
// ], |
| 307 |
// 'Dashboard' => [ |
| 308 |
// 'parent' => self::SLUG, |
| 309 |
// 'type' => 'submenu', |
| 310 |
// 'name' => 'Dashboard', |
| 311 |
// 'capability' => 'manage_options', |
| 312 |
// 'slug' => self::SLUG . '#/', |
| 313 |
// ], |
| 314 |
// 'All Flows' => [ |
| 315 |
// 'parent' => self::SLUG, |
| 316 |
// 'type' => 'submenu', |
| 317 |
// 'name' => 'Flows', |
| 318 |
// 'capability' => 'manage_options', |
| 319 |
// 'slug' => self::SLUG . '#/flows', |
| 320 |
// ], |
| 321 |
// 'Connections' => [ |
| 322 |
// 'parent' => self::SLUG, |
| 323 |
// 'type' => 'submenu', |
| 324 |
// 'name' => 'Connections', |
| 325 |
// 'capability' => 'manage_options', |
| 326 |
// 'slug' => self::SLUG . '#/connections', |
| 327 |
// ], |
| 328 |
// 'Webhooks' => [ |
| 329 |
// 'parent' => self::SLUG, |
| 330 |
// 'type' => 'submenu', |
| 331 |
// 'name' => 'Webhooks', |
| 332 |
// 'capability' => 'manage_options', |
| 333 |
// 'slug' => self::SLUG . '#/webhooks', |
| 334 |
// ], |
| 335 |
// ]; |
| 336 |
// } |
| 337 |
} |
| 338 |
|