| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Admin; |
| 4 |
|
| 5 |
/** |
| 6 |
* The admin menu and page handler class |
| 7 |
*/ |
| 8 |
|
| 9 |
use BitCode\BitForm\Core\Form\FormHandler; |
| 10 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 |
use BitCode\BitForm\Core\Util\DateTimeHelper; |
| 12 |
use BitCode\BitForm\Core\Util\FileDownloadProvider; |
| 13 |
use BitCode\BitForm\Core\Util\IpTool; |
| 14 |
|
| 15 |
class Admin_Bar |
| 16 |
{ |
| 17 |
private static $fonts_url = [ |
| 18 |
'https://fonts.googleapis.com/css2?family=Outfit:wght@100;300;400;500;600;700&display=swap&famildy=Roboto:wght@300;400;500&display=swap', |
| 19 |
'https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap', |
| 20 |
]; |
| 21 |
|
| 22 |
public function register() |
| 23 |
{ |
| 24 |
add_action('in_admin_header', [$this, 'AdminNotices']); |
| 25 |
add_action('admin_menu', [$this, 'AdminMenu']); |
| 26 |
add_action('admin_enqueue_scripts', [$this, 'AdminAssets']); |
| 27 |
add_filter('style_loader_tag', [$this, 'linkTagFilter'], 0, 3); |
| 28 |
add_filter('script_loader_tag', [$this, 'scriptTagFilter'], 0, 3); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Register the admin menu |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function AdminMenu() |
| 37 |
{ |
| 38 |
global $submenu; |
| 39 |
|
| 40 |
$capability = apply_filters('bitforms_form_access_capability', 'manage_options'); |
| 41 |
add_menu_page( |
| 42 |
__('Bit Form - Most advanced form builder and database management system', 'bit-form'), |
| 43 |
'Bit Form', |
| 44 |
$capability, |
| 45 |
'bitform', |
| 46 |
[$this, 'RootPage'], |
| 47 |
'data:image/svg+xml;base64,' . base64_encode('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 163 163"><path fill="#fff" d="M106 0H57A57 57 0 000 57v49a57 57 0 0057 57h49a57 57 0 0057-57V57a57 57 0 00-57-57zM51 60v3l-1 59v12c0 5-3 8-8 6-2-1-3-4-3-6v-21-55a29 29 0 011-4 5 5 0 015-4h33a4 4 0 004-2c2-3 8-2 10-1a9 9 0 015 9 9 9 0 01-7 8c-3 1-6 0-9-3a3 3 0 00-1-1H51zm73 74c-6 5-12 6-20 6H72a8 8 0 01-4-1 5 5 0 01-2-6 5 5 0 015-3h35c11-1 20-10 21-20 0-11-8-20-18-23a6 6 0 00-1 0H78h-1v14l1 2h18a4 4 0 003-1c4-4 8-3 11-1 4 2 5 7 3 12-2 4-9 6-13 2a6 6 0 00-5-1H71c-3 0-5-2-5-6V82c0-4 1-5 5-6h17a21 21 0 0021-24c-1-9-8-16-16-18a25 25 0 00-6 0H46c-4 0-7-3-7-6s3-5 7-5h42a33 33 0 0132 23c2 11 0 20-6 29l-1 2 5 2c11 4 17 12 19 23 3 12-2 24-13 32z"/></svg>'), |
| 48 |
56 |
| 49 |
); |
| 50 |
if (current_user_can($capability)) { |
| 51 |
$submenu['bitform'][] = [__('All Forms', 'bit-form'), $capability, 'admin.php?page=bitform#/']; |
| 52 |
$submenu['bitform'][] = [__('App Settings', 'bit-form'), $capability, 'admin.php?page=bitform#/app-settings/recaptcha']; |
| 53 |
$submenu['bitform'][] = [__('SMTP', 'bit-form'), $capability, 'admin.php?page=bitform#/app-settings/smtp']; |
| 54 |
$submenu['bitform'][] = [__('Payments', 'bit-form'), $capability, 'admin.php?page=bitform#/app-settings/payments']; |
| 55 |
$submenu['bitform'][] = [__('Doc & Support', 'bit-form'), $capability, 'admin.php?page=bitform#/doc-support']; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public function getAllPages() |
| 60 |
{ |
| 61 |
$pages = get_pages(['post_status' => 'publish', 'sort_column' => 'post_date', 'sort_order' => 'desc']); |
| 62 |
$allPages = []; |
| 63 |
foreach ($pages as $pageKey => $pageDetails) { |
| 64 |
$allPages[$pageKey]['title'] = $pageDetails->post_title; |
| 65 |
$allPages[$pageKey]['url'] = get_page_link($pageDetails->ID); |
| 66 |
} |
| 67 |
return $allPages; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Load the asset libraries |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
public function AdminAssets($current_screen) |
| 76 |
{ |
| 77 |
if (!strpos($current_screen, 'bitform')) { |
| 78 |
return; |
| 79 |
} |
| 80 |
$parsed_url = parse_url(get_admin_url()); |
| 81 |
$site_url = $parsed_url['scheme'] . '://' . $parsed_url['host']; |
| 82 |
$site_url .= empty($parsed_url['port']) ? null : ':' . $parsed_url['port']; |
| 83 |
$base_path_admin = str_replace($site_url, '', get_admin_url()); |
| 84 |
// loading google fonts |
| 85 |
wp_enqueue_style('googleapis-BITFORM-PRECONNECT', 'https://fonts.googleapis.com'); |
| 86 |
wp_enqueue_style('gstatic-BITFORM-PRECONNECT-CROSSORIGIN', 'https://fonts.gstatic.com'); |
| 87 |
foreach (self::$fonts_url as $i => $font_url) { |
| 88 |
wp_enqueue_style('bf-font-' . $i, $font_url); |
| 89 |
} |
| 90 |
|
| 91 |
if (defined('BITAPPS_DEV') && BITAPPS_DEV) { |
| 92 |
wp_enqueue_script('vite-client-helper-BITFORM-MODULE', BIT_DEV_URL . '/config/devHotModule.js', [], null); |
| 93 |
wp_enqueue_script('vite-client-BITFORM-MODULE', BIT_DEV_URL . '/@vite/client', [], null); |
| 94 |
wp_enqueue_script('index-BITFORM-MODULE', BIT_DEV_URL . '/main.jsx', [], null); |
| 95 |
} |
| 96 |
|
| 97 |
if (!defined('BITAPPS_DEV') || (defined('BITAPPS_DEV') && !BITAPPS_DEV)) { |
| 98 |
$build_hash = file_get_contents(BITFORMS_PLUGIN_DIR_PATH . '/build-hash.txt'); |
| 99 |
wp_enqueue_script('index-BITFORM-MODULE', BITFORMS_ASSET_URI . "/main-{$build_hash}.js", [], null); |
| 100 |
wp_enqueue_style('bf-css', BITFORMS_ASSET_URI . "/main-{$build_hash}.css"); |
| 101 |
} |
| 102 |
|
| 103 |
// if (defined('BITAPPS_DEV') && BITAPPS_DEV) { |
| 104 |
// wp_enqueue_script( |
| 105 |
// 'bitforms-vendors', |
| 106 |
// BITFORMS_ASSET_JS_URI . '/vendors-main.js', |
| 107 |
// null, |
| 108 |
// BITFORMS_VERSION, |
| 109 |
// true |
| 110 |
// ); |
| 111 |
// wp_enqueue_script( |
| 112 |
// 'bitforms-runtime', |
| 113 |
// BITFORMS_ASSET_JS_URI . '/runtime.js', |
| 114 |
// null, |
| 115 |
// BITFORMS_VERSION, |
| 116 |
// true |
| 117 |
// ); |
| 118 |
// wp_enqueue_script( |
| 119 |
// 'bitforms-file', |
| 120 |
// BITFORMS_ASSET_JS_URI . '/bitforms-file.js', |
| 121 |
// ['bitforms-vendors', 'bitforms-runtime'], |
| 122 |
// BITFORMS_VERSION, |
| 123 |
// true |
| 124 |
// ); |
| 125 |
// } |
| 126 |
|
| 127 |
if (wp_script_is('wp-i18n')) { |
| 128 |
$deps = ['bitforms-vendors', 'bitforms-runtime', 'bitforms-file', 'wp-i18n']; |
| 129 |
} else { |
| 130 |
$deps = ['bitforms-vendors', 'bitforms-runtime', 'bitforms-file']; |
| 131 |
} |
| 132 |
if (!defined('BITAPPS_DEV')) { |
| 133 |
wp_enqueue_script( |
| 134 |
'bitforms-admin-script', |
| 135 |
BITFORMS_ASSET_JS_URI . '/index.js', |
| 136 |
$deps, |
| 137 |
BITFORMS_VERSION, |
| 138 |
true |
| 139 |
); |
| 140 |
} |
| 141 |
wp_enqueue_script('tinymce_js', includes_url('js/tinymce/') . 'wp-tinymce.php', null, false, true); |
| 142 |
|
| 143 |
if (!wp_script_is('media-upload')) { |
| 144 |
wp_enqueue_media(); |
| 145 |
} |
| 146 |
|
| 147 |
$plugin_path = BITFORMS_ASSET_URI; |
| 148 |
|
| 149 |
echo "<meta name='theme-color' content='#13233c' />"; |
| 150 |
|
| 151 |
if (defined('BITAPPS_DEV') && !BITAPPS_DEV) { |
| 152 |
wp_enqueue_style( |
| 153 |
'bitforms-styles', |
| 154 |
BITFORMS_ASSET_URI . '/css/bitforms.css', |
| 155 |
null, |
| 156 |
BITFORMS_VERSION, |
| 157 |
'screen' |
| 158 |
); |
| 159 |
|
| 160 |
wp_enqueue_style( |
| 161 |
'bitforms-components-styles', |
| 162 |
BITFORMS_ASSET_URI . '/css/components.css', |
| 163 |
null, |
| 164 |
BITFORMS_VERSION, |
| 165 |
'screen' |
| 166 |
); |
| 167 |
} |
| 168 |
$formHandler = FormHandler::getInstance(); |
| 169 |
$all_forms = $formHandler->admin->getAllForm(); |
| 170 |
$urlQuery = parse_url(FileDownloadProvider::getBaseDownloadURL(), PHP_URL_QUERY); |
| 171 |
$baseDLURL = FileDownloadProvider::getBaseDownloadURL(); |
| 172 |
$baseDLURL = empty($urlQuery) ? $baseDLURL . '?' : $baseDLURL . '&'; |
| 173 |
$ipTool = new IpTool(); |
| 174 |
$user_details = $ipTool->getUserDetail(); |
| 175 |
$integrationHandler = new IntegrationHandler(0, $user_details); |
| 176 |
$allFormIntegrations = $integrationHandler->getAllIntegration('app'); |
| 177 |
$allFormSettings = []; |
| 178 |
if (!is_wp_error($allFormIntegrations)) { |
| 179 |
$integCount = []; |
| 180 |
foreach ($allFormIntegrations as $integration) { |
| 181 |
$type = $integration->integration_type; |
| 182 |
if (!is_null($type)) { |
| 183 |
if (!isset($integCount[$type])) { |
| 184 |
$integCount[$type] = 1; |
| 185 |
} else { |
| 186 |
$integCount[$type] += 1; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
foreach ($allFormIntegrations as $integration) { |
| 191 |
$type = $integration->integration_type; |
| 192 |
if (!is_null($type)) { |
| 193 |
$integrationDetails = json_decode($integration->integration_details); |
| 194 |
$integrationDetails->id = $integration->id; |
| 195 |
if ($integCount[$type] > 1) { |
| 196 |
if (!isset($allFormSettings[$type])) { |
| 197 |
$allFormSettings[$type] = []; |
| 198 |
} |
| 199 |
array_push($allFormSettings[$type], $integrationDetails); |
| 200 |
} else { |
| 201 |
$allFormSettings[$type] = $integrationDetails; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
$users = get_users(['fields' => ['ID', 'user_nicename', 'user_email', 'display_name']]); |
| 208 |
$userMail = []; |
| 209 |
$userNames = []; |
| 210 |
foreach ($users as $key => $value) { |
| 211 |
$userMail[$key]['label'] = !empty($value->display_name) ? $value->display_name : ''; |
| 212 |
$userMail[$key]['value'] = !empty($value->user_email) ? $value->user_email : ''; |
| 213 |
$userMail[$key]['id'] = $value->ID; |
| 214 |
$userNames[$value->ID] = ['name' => $value->display_name, 'url' => get_edit_user_link($value->ID)]; |
| 215 |
} |
| 216 |
$bits = [ |
| 217 |
'configs' => ['bf_separator' => BITFORMS_BF_SEPARATOR], |
| 218 |
'nonce' => wp_create_nonce('bitforms_save'), |
| 219 |
'isPro' => false, |
| 220 |
'siteURL' => site_url(), |
| 221 |
'assetsURL' => BITFORMS_ASSET_URI, |
| 222 |
'baseURL' => $base_path_admin . 'admin.php?page=bitform#', |
| 223 |
'baseDLURL' => $baseDLURL, |
| 224 |
'styleURL' => BITFORMS_UPLOAD_BASE_URL . '/form-styles', |
| 225 |
'iconURL' => BITFORMS_UPLOAD_BASE_URL . '/icons', |
| 226 |
'ajaxURL' => admin_url('admin-ajax.php'), |
| 227 |
'allForms' => is_wp_error($all_forms) ? null : $all_forms, |
| 228 |
'allPages' => is_wp_error($this->getAllPages()) ? [] : $this->getAllPages(), |
| 229 |
'allFormSettings' => count($allFormSettings) > 0 ? $allFormSettings : (object)[], |
| 230 |
'userMail' => !empty($userMail) ? $userMail : [], |
| 231 |
'user' => (object) $userNames, |
| 232 |
'dateFormat' => get_option('date_format'), |
| 233 |
'timeFormat' => get_option('time_format'), |
| 234 |
'timeZone' => DateTimeHelper::wp_timezone_string(), |
| 235 |
'googleRedirectURL' => get_rest_url() . 'bitform/v1/google', |
| 236 |
'oneDriveRedirectURL' => get_rest_url() . 'bitform/v1/oneDrive', |
| 237 |
'zohoRedirectURL' => get_rest_url() . 'bitform/v1/zoho', |
| 238 |
]; |
| 239 |
|
| 240 |
$isMigratingToV2 = get_option('bitforms_migrating_to_v2', false); |
| 241 |
if ($isMigratingToV2) { |
| 242 |
$bits['isMigratingToV2'] = $isMigratingToV2; |
| 243 |
} |
| 244 |
|
| 245 |
if (defined('BITFORMS_VERSION')) { |
| 246 |
$bits['version'] = BITFORMS_VERSION; |
| 247 |
} |
| 248 |
|
| 249 |
$changelogVersion = get_option('bitforms_changelog_version', '0.0.0'); |
| 250 |
$bits['changelogVersion'] = $changelogVersion; |
| 251 |
|
| 252 |
global $wpdb; |
| 253 |
$hasV1FormTable = $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}bitforms_form_v1'"); |
| 254 |
if ($hasV1FormTable) { |
| 255 |
$bits['canRollbackToV1'] = true; |
| 256 |
} |
| 257 |
|
| 258 |
$bitforms = apply_filters( |
| 259 |
'bitforms_localized_script', |
| 260 |
$bits |
| 261 |
); |
| 262 |
if ('en_US' !== get_locale() && file_exists(BITFORMS_PLUGIN_DIR_PATH . '/languages/generatedString.php')) { |
| 263 |
include_once BITFORMS_PLUGIN_DIR_PATH . '/languages/generatedString.php'; |
| 264 |
$bitforms['translations'] = $i18n_strings; |
| 265 |
} |
| 266 |
wp_localize_script('index-BITFORM-MODULE', 'bits', $bitforms); |
| 267 |
// !BIT_DEV && wp_localize_script('bitforms-admin-script', 'bits', $bitforms); |
| 268 |
// echo !BIT_DEV ? '' : "<script>console.log(window.bits=JSON.parse('" . str_replace("'", '', json_encode($bitforms)) . "'))</script>"; |
| 269 |
// if (\function_exists('wp_set_script_translations')) |
| 270 |
// wp_set_script_translations('bitforms-admin-script', 'bitform', BITFORMS_PLUGIN_DIR_PATH . 'languages'); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Bitforms apps-root id provider |
| 275 |
* @return void |
| 276 |
*/ |
| 277 |
public function RootPage() |
| 278 |
{ |
| 279 |
require_once BITFORMS_PLUGIN_DIR_PATH . '/views/view-root.php'; |
| 280 |
} |
| 281 |
|
| 282 |
public function AdminNotices() |
| 283 |
{ |
| 284 |
global $plugin_page; |
| 285 |
$plugin_page = null === $plugin_page ? '' : $plugin_page; |
| 286 |
if (false === strpos($plugin_page, 'bitform')) { |
| 287 |
return; |
| 288 |
} |
| 289 |
remove_all_actions('admin_notices'); |
| 290 |
remove_all_actions('all_admin_notices'); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Modify style tags |
| 295 |
* |
| 296 |
* @param string $html link tag |
| 297 |
* |
| 298 |
* @return string new link tag |
| 299 |
*/ |
| 300 |
public function linkTagFilter($html, $handle, $href) |
| 301 |
{ |
| 302 |
$newTag = $html; |
| 303 |
if (preg_match('/BITFORM-PRECONNECT/', $handle)) { |
| 304 |
$newTag = preg_replace('/rel=("|\')stylesheet("|\')/', 'rel="preconnect"', $newTag); |
| 305 |
} |
| 306 |
if (preg_match('/BITFORM-PRELOAD/', $handle)) { |
| 307 |
$newTag = preg_replace('/rel=("|\')stylesheet("|\')/', 'rel="preload"', $newTag); |
| 308 |
} |
| 309 |
if (preg_match('/BITFORM-CROSSORIGIN/', $handle)) { |
| 310 |
$newTag = preg_replace('/<link /', '<link crossorigin ', $newTag); |
| 311 |
} |
| 312 |
if (preg_match('/BITFORM-SCRIPT/', $handle)) { |
| 313 |
$newTag = preg_replace('/<link /', '<link as="script" ', $newTag); |
| 314 |
} |
| 315 |
return $newTag; |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Modify script tags |
| 320 |
* |
| 321 |
* @param string $html script tag |
| 322 |
* |
| 323 |
* @return string new script tag |
| 324 |
*/ |
| 325 |
public function scriptTagFilter($html, $handle, $href) |
| 326 |
{ |
| 327 |
$newTag = $html; |
| 328 |
if (preg_match('/BITFORM-MODULE/', $handle)) { |
| 329 |
$newTag = preg_replace('/<script /', '<script type="module" ', $newTag); |
| 330 |
} |
| 331 |
return $newTag; |
| 332 |
} |
| 333 |
} |
| 334 |
|