| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm; |
| 4 |
|
| 5 |
if (!\defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
final class Config |
| 10 |
{ |
| 11 |
public const SLUG = 'bit-form'; |
| 12 |
|
| 13 |
public const TITLE = 'Bit Form'; |
| 14 |
|
| 15 |
public const VAR_PREFIX = BITFORMS_PREFIX; |
| 16 |
|
| 17 |
public const VERSION = BITFORMS_VERSION; |
| 18 |
|
| 19 |
public const DB_VERSION = BITFORMS_DB_VERSION; |
| 20 |
|
| 21 |
public const REQUIRED_PHP_VERSION = BITFORMS_REQUIRED_PHP_VERSION; |
| 22 |
|
| 23 |
public const REQUIRED_WP_VERSION = BITFORMS_REQUIRED_WP_VERSION; |
| 24 |
|
| 25 |
public const API_VERSION = BITFORMS_API_VERSION; |
| 26 |
|
| 27 |
public const APP_BASE = BITFORMS_PLUGIN_BASENAME; |
| 28 |
|
| 29 |
public const DEV_URL = 'http://localhost:3000'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Provides configuration for plugin. |
| 33 |
* |
| 34 |
* @param string $type Type of conf |
| 35 |
* @param string $default Default value |
| 36 |
* |
| 37 |
* @return array|string|null |
| 38 |
*/ |
| 39 |
public static function get($type, $default = null) |
| 40 |
{ |
| 41 |
switch ($type) { |
| 42 |
case 'MAIN_FILE': |
| 43 |
return realpath(__DIR__ . DIRECTORY_SEPARATOR . self::APP_BASE); |
| 44 |
|
| 45 |
case 'BASENAME': |
| 46 |
return plugin_basename(trim(self::get('MAIN_FILE'))); |
| 47 |
|
| 48 |
case 'SITE_URL': |
| 49 |
$parsedUrl = wp_parse_url(get_admin_url()); |
| 50 |
$siteUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host']; |
| 51 |
$siteUrl .= empty($parsedUrl['port']) ? null : ':' . $parsedUrl['port']; |
| 52 |
|
| 53 |
return $siteUrl; |
| 54 |
|
| 55 |
case 'ADMIN_URL': |
| 56 |
return str_replace(self::get('SITE_URL'), '', get_admin_url()); |
| 57 |
|
| 58 |
case 'API_URL': |
| 59 |
global $wp_rewrite; |
| 60 |
|
| 61 |
return [ |
| 62 |
'base' => get_rest_url() . self::SLUG . '/v1', |
| 63 |
'separator' => $wp_rewrite->permalink_structure ? '?' : '&', |
| 64 |
]; |
| 65 |
|
| 66 |
case 'ROOT_URI': |
| 67 |
return BITFORMS_ROOT_URI; |
| 68 |
|
| 69 |
case 'ASSET_URI': |
| 70 |
return BITFORMS_ASSET_URI; |
| 71 |
|
| 72 |
case 'ASSET_JS_URI': |
| 73 |
return BITFORMS_ASSET_JS_URI; |
| 74 |
|
| 75 |
case 'ASSET_CSS_URI': |
| 76 |
return self::get('ASSET_URI') . '/css'; |
| 77 |
|
| 78 |
case 'PLUGIN_PAGE_LINKS': |
| 79 |
return self::pluginPageLinks(); |
| 80 |
|
| 81 |
case 'SIDE_BAR_MENU': |
| 82 |
// return self::sideBarMenu(); |
| 83 |
|
| 84 |
case 'WP_DB_PREFIX': |
| 85 |
global $wpdb; |
| 86 |
|
| 87 |
return $wpdb->prefix; |
| 88 |
|
| 89 |
default: |
| 90 |
return $default; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Prefixed variable name with prefix. |
| 96 |
* |
| 97 |
* @param string $option Variable name |
| 98 |
* |
| 99 |
* @return array |
| 100 |
*/ |
| 101 |
public static function withPrefix($option) |
| 102 |
{ |
| 103 |
return self::VAR_PREFIX . $option; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Retrieves options from option table. |
| 108 |
* |
| 109 |
* @param string $option Option name |
| 110 |
* @param bool $default default value |
| 111 |
* @param bool $wp Whether option is default wp option |
| 112 |
* |
| 113 |
* @return mixed |
| 114 |
*/ |
| 115 |
public static function getOption($option, $default = false, $wp = false) |
| 116 |
{ |
| 117 |
if ($wp) { |
| 118 |
return get_option($option, $default); |
| 119 |
} |
| 120 |
|
| 121 |
return get_option(self::withPrefix($option), $default); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Saves option to option table. |
| 126 |
* |
| 127 |
* @param string $option Option name |
| 128 |
* @param bool $autoload Whether option will autoload |
| 129 |
* @param mixed $value |
| 130 |
* |
| 131 |
* @return bool |
| 132 |
*/ |
| 133 |
public static function addOption($option, $value, $autoload = false) |
| 134 |
{ |
| 135 |
return add_option(self::withPrefix($option), $value, '', $autoload ? 'yes' : 'no'); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Save or update option to option table. |
| 140 |
* |
| 141 |
* @param string $option Option name |
| 142 |
* @param mixed $value Option value |
| 143 |
* @param bool $autoload Whether option will autoload |
| 144 |
* |
| 145 |
* @return bool |
| 146 |
*/ |
| 147 |
public static function updateOption(string $option, mixed $value, bool $autoload = null): bool |
| 148 |
{ |
| 149 |
return update_option(self::withPrefix($option), $value, !\is_null($autoload) ? 'yes' : null); |
| 150 |
} |
| 151 |
|
| 152 |
public static function isDev() |
| 153 |
{ |
| 154 |
return \defined('BITAPPS_DEV') && BITAPPS_DEV; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Provides links for plugin pages. Those links will bi displayed in |
| 159 |
* all plugin pages under the plugin name. |
| 160 |
* |
| 161 |
* @return array |
| 162 |
*/ |
| 163 |
private static function pluginPageLinks() |
| 164 |
{ |
| 165 |
return [ |
| 166 |
'app-setting' => [ |
| 167 |
'title' => __('App Setting', self::SLUG), |
| 168 |
'url' => self::get('ADMIN_URL') . 'admin.php?page=bitform#/app-settings/recaptcha', |
| 169 |
], |
| 170 |
'doc-support' => [ |
| 171 |
'title' => __('Doc & Support', self::SLUG), |
| 172 |
'url' => self::get('ADMIN_URL') . 'admin.php?page=bitform#/doc-support', |
| 173 |
], |
| 174 |
]; |
| 175 |
} |
| 176 |
} |
| 177 |
|