Api
1 year ago
Container
1 year ago
Divi
1 year ago
Elementor
1 year ago
Mioweb
1 year ago
Model
1 year ago
Repository
1 year ago
Service
1 year ago
Templates
1 year ago
Utils
1 year ago
Bootstrap.php
1 year ago
FapiMemberPlugin.php
1 year ago
Bootstrap.php
427 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace FapiMember; |
| 4 | |
| 5 | use FapiMember\Api\V1\RequestHandler; |
| 6 | use FapiMember\Api\V2\ApiController; |
| 7 | use FapiMember\Container\Container; |
| 8 | use FapiMember\Divi\FapiMemberDivi; |
| 9 | use FapiMember\Mioweb\FapiMemberMioweb; |
| 10 | use FapiMember\Model\Enums\Keys\OptionKey; |
| 11 | use FapiMember\Model\Enums\Types\RequestMethodType; |
| 12 | use FapiMember\Model\Enums\UserPermission; |
| 13 | use FapiMember\Service\ApiService; |
| 14 | use FapiMember\Service\ElementService; |
| 15 | use FapiMember\Service\RedirectService; |
| 16 | use FapiMember\Utils\DisplayHelper; |
| 17 | use FapiMember\Utils\PostTypeHelper; |
| 18 | use FapiMember\Utils\Random; |
| 19 | use FapiMember\Utils\ShortcodeSubstitutor; |
| 20 | |
| 21 | final class Bootstrap |
| 22 | { |
| 23 | private FapiMemberPlugin $fapiMemberPlugin; |
| 24 | private FapiMemberDivi $fapiMemberDivi; |
| 25 | private FapiMemberMioweb $fapiMemberMioweb; |
| 26 | private ApiService $apiService; |
| 27 | private ElementService $elementService; |
| 28 | private RedirectService $redirectService; |
| 29 | private RequestHandler $requestHandler; |
| 30 | private ShortcodeSubstitutor $shortcodeSubstitutor; |
| 31 | private ApiController $apiController; |
| 32 | |
| 33 | public function __construct(FapiMemberPlugin $fapiMemberPlugin) |
| 34 | { |
| 35 | $this->fapiMemberPlugin = $fapiMemberPlugin; |
| 36 | Container::set(FapiMemberPlugin::class, $fapiMemberPlugin); |
| 37 | |
| 38 | $this->apiService = Container::get(ApiService::class); |
| 39 | $this->elementService = Container::get(ElementService::class); |
| 40 | $this->redirectService = Container::get(RedirectService::class); |
| 41 | $this->requestHandler = Container::get(RequestHandler::class); |
| 42 | $this->shortcodeSubstitutor = Container::get(ShortcodeSubstitutor::class); |
| 43 | $this->apiController = Container::get(ApiController::class); |
| 44 | $this->fapiMemberDivi = Container::get(FapiMemberDivi::class); |
| 45 | $this->fapiMemberMioweb = Container::get(FapiMemberMioweb::class); |
| 46 | } |
| 47 | |
| 48 | public function initialize(): void |
| 49 | { |
| 50 | $this->addHooks(); |
| 51 | $this->generateTokenIfNeeded(); |
| 52 | $this->migrateCredentialsIfNeeded(); |
| 53 | |
| 54 | update_option(OptionKey::FAPI_MEMBER_VERSION, FAPI_MEMBER_PLUGIN_VERSION); |
| 55 | } |
| 56 | |
| 57 | private function generateTokenIfNeeded(): void |
| 58 | { |
| 59 | $token = get_option(OptionKey::TOKEN, ''); |
| 60 | |
| 61 | if (!$token) { |
| 62 | update_option(OptionKey::TOKEN, Random::generate(20, 'A-Za-z')); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public function migrateCredentialsIfNeeded(): void |
| 67 | { |
| 68 | $oldVersion = get_option(OptionKey::FAPI_MEMBER_VERSION, ''); |
| 69 | |
| 70 | if (!empty($oldVersion)){ |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | $fapiCredentials = get_option(OptionKey::API_CREDENTIALS, null); |
| 75 | |
| 76 | if ((!empty($fapiCredentials))) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | $apiUser = get_option(OptionKey::API_USER, null); |
| 81 | $apiKey = get_option(OptionKey::API_KEY, null ); |
| 82 | |
| 83 | if (empty($apiKey) || empty($apiUser)) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | update_option( |
| 88 | OptionKey::API_CREDENTIALS, |
| 89 | json_encode( |
| 90 | array( |
| 91 | array( |
| 92 | 'username' => $apiUser, |
| 93 | 'token' => $apiKey, |
| 94 | ), |
| 95 | ) |
| 96 | ) |
| 97 | ); |
| 98 | |
| 99 | $credentialsOk = $this->apiService->checkCredentials(); |
| 100 | update_option( OptionKey::API_CHECKED, $credentialsOk ); |
| 101 | |
| 102 | if (!$credentialsOk) { |
| 103 | update_option(OptionKey::API_CREDENTIALS, '0'); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private function addHooks(): void |
| 108 | { |
| 109 | $this->addInitHooks(); |
| 110 | $this->addAdminHooks(); |
| 111 | |
| 112 | add_action('wp_enqueue_scripts', array($this, 'addPublicScripts')); |
| 113 | |
| 114 | add_action('rest_api_init', array($this, 'addRestEndpoints')); |
| 115 | |
| 116 | // adds meta boxed to setting page/post side bar |
| 117 | add_action('add_meta_boxes', array($this->elementService, 'addMetaBoxes')); |
| 118 | add_action('save_post', array($this->elementService, 'savePostMetadata')); |
| 119 | |
| 120 | // check if page in fapi level |
| 121 | add_action('template_redirect', array($this->redirectService, 'checkPageForRedirects')); |
| 122 | |
| 123 | // user profile |
| 124 | add_action('edit_user_profile', array($this->elementService, 'addUserMenuPage')); |
| 125 | |
| 126 | add_action('wp_enqueue_scripts', array($this, 'addPublicScripts')); |
| 127 | |
| 128 | $this->addMiowebHooks(); |
| 129 | $this->addDiviHooks(); |
| 130 | |
| 131 | add_image_size('level-selection', 300, 164, true ); |
| 132 | add_filter('login_redirect', array($this->fapiMemberPlugin, 'loginRedirect'), 5, 3 ); |
| 133 | add_filter('show_admin_bar', array($this->elementService, 'hideAdminBar')); |
| 134 | |
| 135 | // filters block to render by section and levels provided |
| 136 | add_filter( |
| 137 | 'render_block', |
| 138 | function ( $blockContent, $block ) { |
| 139 | if (!isset( $block['attrs']['hasSectionOrLevel'])) { |
| 140 | return $blockContent; |
| 141 | } |
| 142 | |
| 143 | if (!isset( $block['attrs']['fapiSectionAndLevels'])) { |
| 144 | return $blockContent; |
| 145 | } |
| 146 | |
| 147 | if (DisplayHelper::shouldContentBeRendered( |
| 148 | (string) $block['attrs']['hasSectionOrLevel'], |
| 149 | $block['attrs']['fapiSectionAndLevels'] |
| 150 | )) { |
| 151 | return $blockContent; |
| 152 | } |
| 153 | |
| 154 | return ''; |
| 155 | }, |
| 156 | 15, |
| 157 | 2 |
| 158 | ); |
| 159 | |
| 160 | // WPS hide login plugin |
| 161 | add_filter('whl_logged_in_redirect', array($this->redirectService, 'loggedInRedirect'), 1); |
| 162 | add_filter('whl_logged_in_redirect', array($this->redirectService, 'loggedInRedirect'), 1); |
| 163 | } |
| 164 | |
| 165 | private function addInitHooks(): void |
| 166 | { |
| 167 | add_action('init', array($this, 'registerLevelsTaxonomy')); |
| 168 | add_action('init', array($this, 'registerRoles')); |
| 169 | add_action('init', array($this, 'addShortcodes')); |
| 170 | add_action('init', array($this->fapiMemberPlugin, 'checkTimedLevelUnlock')); |
| 171 | } |
| 172 | |
| 173 | private function addAdminHooks(): void |
| 174 | { |
| 175 | add_action('admin_init', array($this, 'registerSettings')); |
| 176 | add_action('admin_menu', array($this->elementService, 'addAdminMenu')); |
| 177 | add_action('admin_enqueue_scripts', array($this, 'addScripts')); |
| 178 | add_action('admin_enqueue_scripts', [$this, 'addApiNonce']); |
| 179 | } |
| 180 | |
| 181 | private function addMiowebHooks(): void |
| 182 | { |
| 183 | add_action('wp_ajax_open_element_setting', [$this->fapiMemberMioweb, 'addSetting']); |
| 184 | add_action('wp_ajax_open_row_setting', [$this->fapiMemberMioweb, 'addSetting']); |
| 185 | add_action('mw_page_init', [$this->fapiMemberMioweb, 'hideContentIfNeeded']); |
| 186 | } |
| 187 | |
| 188 | public function addDiviHooks(): void |
| 189 | { |
| 190 | add_action('divi_extensions_init', [$this, 'initializeDiviExtension']); |
| 191 | |
| 192 | add_filter( 'et_builder_get_parent_modules', [$this->fapiMemberDivi, 'addToggle']); |
| 193 | |
| 194 | foreach ($this->fapiMemberDivi->allowedModuleSlugs as $slug) { |
| 195 | add_filter("et_pb_all_fields_unprocessed_" . $slug, [$this->fapiMemberDivi, 'addFields']); |
| 196 | } |
| 197 | |
| 198 | add_filter('et_pb_module_content', [$this->fapiMemberDivi, 'hideElements'], 10, 4 ); |
| 199 | } |
| 200 | |
| 201 | public function initializeDiviExtension(): void |
| 202 | { |
| 203 | require_once plugin_dir_path( __FILE__ ) . 'Divi/includes/FmDivi.php'; |
| 204 | } |
| 205 | |
| 206 | function addApiNonce(): void |
| 207 | { |
| 208 | if (current_user_can(UserPermission::REQUIRED_CAPABILITY)) { |
| 209 | $nonce = wp_create_nonce('wp_rest'); |
| 210 | echo "<script>window.apiInternalAccessNonce = '{$nonce}'</script>"; |
| 211 | } |
| 212 | |
| 213 | } |
| 214 | |
| 215 | public function addRestEndpoints(): void |
| 216 | { |
| 217 | $this->addRestEndpointV1('sections', 'handleApiSections', RequestMethodType::GET); |
| 218 | $this->addRestEndpointV1('sections-simple', 'handleApiSectionsSimple', RequestMethodType::GET); |
| 219 | $this->addRestEndpointV1('callback', 'handleApiCallback', RequestMethodType::POST); |
| 220 | $this->addRestEndpointV1('check-connection', 'handleApiCheckConnectionCallback', RequestMethodType::POST); |
| 221 | $this->addRestEndpointV1( |
| 222 | 'list-forms/(?P<user>[^/]+(?:\+[^/]+)?)', |
| 223 | 'handleApiListFormsCallback', |
| 224 | RequestMethodType::GET, |
| 225 | ); |
| 226 | $this->addRestEndpointV1('list-users', 'handleApiUsernamesCallback', RequestMethodType::GET); |
| 227 | |
| 228 | $this->addRestEndpointV2('sections'); |
| 229 | $this->addRestEndpointV2('pages'); |
| 230 | $this->addRestEndpointV2('emails'); |
| 231 | $this->addRestEndpointV2('memberships'); |
| 232 | $this->addRestEndpointV2('users'); |
| 233 | $this->addRestEndpointV2('apiConnections'); |
| 234 | } |
| 235 | |
| 236 | public function addRestEndpointV2(string $route): void |
| 237 | { |
| 238 | register_rest_route( |
| 239 | 'fapi/v2', |
| 240 | '/' . $route, |
| 241 | [ |
| 242 | 'methods' => [RequestMethodType::GET, RequestMethodType::POST], |
| 243 | 'callback' => array($this->apiController, 'handleRequest'), |
| 244 | 'permission_callback' => function () { |
| 245 | return true; |
| 246 | }, |
| 247 | ], |
| 248 | ); |
| 249 | } |
| 250 | |
| 251 | public function addRestEndpointV1(string $route, string $functionName, string $method): void |
| 252 | { |
| 253 | register_rest_route( |
| 254 | 'fapi/v1', |
| 255 | '/' . $route, |
| 256 | [ |
| 257 | 'methods' => $method, |
| 258 | 'callback' => array($this->requestHandler, $functionName), |
| 259 | 'permission_callback' => function () { |
| 260 | return true; |
| 261 | }, |
| 262 | ], |
| 263 | ); |
| 264 | } |
| 265 | |
| 266 | public function addShortcodes(): void |
| 267 | { |
| 268 | add_shortcode('fapi-member-login', array($this->shortcodeSubstitutor, 'shortcodeLoginForm')); |
| 269 | add_shortcode('fapi-member-user', array($this->shortcodeSubstitutor, 'shortcodeUser')); |
| 270 | add_shortcode('fapi-member-user-section-expiration', array($this->shortcodeSubstitutor, 'shortcodeSectionExpirationDate')); |
| 271 | add_shortcode('fapi-member-level-unlock-date', array($this->shortcodeSubstitutor, 'shortcodeLevelUnlockDate')); |
| 272 | add_shortcode('fapi-member-unlock-level', array($this->shortcodeSubstitutor, 'shortcodeUnlockLevel')); |
| 273 | } |
| 274 | |
| 275 | public function addScripts(): void |
| 276 | { |
| 277 | $this->registerStyles(); |
| 278 | $this->registerScripts(); |
| 279 | |
| 280 | global $pagenow; |
| 281 | |
| 282 | if ($pagenow === 'admin.php' || $pagenow === 'options-general.php') { |
| 283 | wp_enqueue_style('fapi-member-admin-font'); |
| 284 | wp_enqueue_style('fapi-member-swal-css'); |
| 285 | wp_enqueue_script('fapi-member-swal'); |
| 286 | wp_enqueue_script('fapi-member-swal-promise-polyfill'); |
| 287 | wp_enqueue_script('fapi-member-clipboard'); |
| 288 | wp_enqueue_script('fapi-member-main'); |
| 289 | } |
| 290 | if ($pagenow === 'user-edit.php') { |
| 291 | wp_enqueue_style('fapi-member-user-profile'); |
| 292 | wp_enqueue_script('fapi-member-main'); |
| 293 | } |
| 294 | |
| 295 | wp_enqueue_script( |
| 296 | 'fm-react-app', |
| 297 | trailingslashit(plugins_url('/', __FILE__ )) . '../app/dist/bundle.js', |
| 298 | ['jquery', 'wp-element'], |
| 299 | filemtime(plugin_dir_path(__FILE__) . '../app/dist/bundle.js'), |
| 300 | true, |
| 301 | ); |
| 302 | |
| 303 | wp_localize_script('fm-react-app', 'environmentData', array( |
| 304 | 'timeZoneOffset' => get_option('gmt_offset'), |
| 305 | )); |
| 306 | } |
| 307 | |
| 308 | public function registerStyles(): void |
| 309 | { |
| 310 | wp_register_style( |
| 311 | 'fapi-member-user-profile', |
| 312 | plugins_url('fapi-member/media/fapi-user-profile.css') |
| 313 | ); |
| 314 | wp_register_style( |
| 315 | 'fapi-member-admin-font', |
| 316 | plugins_url('fapi-member/media/font/stylesheet.css') |
| 317 | ); |
| 318 | wp_register_style( |
| 319 | 'fapi-member-swal-css', |
| 320 | plugins_url('fapi-member/media/dist/sweetalert2.min.css') |
| 321 | ); |
| 322 | wp_register_style( |
| 323 | 'fapi-member-public-style', |
| 324 | plugins_url('fapi-member/media/fapi-member-public.css') |
| 325 | ); |
| 326 | } |
| 327 | |
| 328 | public function registerScripts(): void |
| 329 | { |
| 330 | wp_register_script( |
| 331 | 'fapi-member-swal', |
| 332 | plugins_url('fapi-member/media/dist/sweetalert2.js') |
| 333 | ); |
| 334 | |
| 335 | wp_register_script( |
| 336 | 'fapi-member-swal-promise-polyfill', |
| 337 | plugins_url('fapi-member/media/dist/polyfill.min.js') |
| 338 | ); |
| 339 | |
| 340 | wp_register_script( |
| 341 | 'fapi-member-clipboard', |
| 342 | plugins_url('fapi-member/media/dist/clipboard.min.js') |
| 343 | ); |
| 344 | |
| 345 | if (FapiMemberPlugin::isDevelopment()) { |
| 346 | wp_register_script( |
| 347 | 'fapi-member-main', |
| 348 | plugins_url('fapi-member/media/dist/fapi.dev.js') |
| 349 | ); |
| 350 | } else { |
| 351 | wp_register_script( |
| 352 | 'fapi-member-main', |
| 353 | plugins_url('fapi-member/media/dist/fapi.dist.js') |
| 354 | ); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | public function addPublicScripts(): void |
| 359 | { |
| 360 | $this->registerPublicStyles(); |
| 361 | |
| 362 | wp_enqueue_style('fapi-member-public-style'); |
| 363 | |
| 364 | if ( defined('FAPI_SHOWING_LEVEL_SELECTION')) { |
| 365 | wp_register_style( |
| 366 | 'fapi-member-public-levelselection-font', |
| 367 | 'https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap' |
| 368 | ); |
| 369 | wp_enqueue_style('fapi-member-public-levelselection-font'); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | public function registerPublicStyles(): void |
| 374 | { |
| 375 | wp_register_style( |
| 376 | 'fapi-member-public-style', |
| 377 | plugins_url('fapi-member/media/fapi-member-public.css') |
| 378 | ); |
| 379 | } |
| 380 | |
| 381 | public function registerRoles(): void |
| 382 | { |
| 383 | if ( get_role('member') === null ) { |
| 384 | add_role('member', __('Člen', 'fapi-member'), get_role('subscriber')->capabilities); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | public function registerSettings(): void |
| 389 | { |
| 390 | register_setting( |
| 391 | 'options', |
| 392 | 'fapiMemberApiEmail', |
| 393 | array( |
| 394 | 'type' => 'string', |
| 395 | 'description' => __('Fapi Member - API e-mail', 'fapi-member'), |
| 396 | 'show_in_rest' => false, |
| 397 | 'default' => null, |
| 398 | ) |
| 399 | ); |
| 400 | register_setting( |
| 401 | 'options', |
| 402 | 'fapiMemberApiKey', |
| 403 | array( |
| 404 | 'type' => 'string', |
| 405 | 'description' => __('Fapi Member - API key', 'fapi-member'), |
| 406 | 'show_in_rest' => false, |
| 407 | 'default' => null, |
| 408 | ) |
| 409 | ); |
| 410 | } |
| 411 | |
| 412 | public function registerLevelsTaxonomy(): void |
| 413 | { |
| 414 | register_taxonomy( |
| 415 | 'fapi_levels', |
| 416 | PostTypeHelper::getSupportedPostTypes(), |
| 417 | array( |
| 418 | 'public' => false, |
| 419 | 'hierarchical' => true, |
| 420 | 'show_ui' => false, |
| 421 | 'show_in_rest' => false, |
| 422 | ) |
| 423 | ); |
| 424 | } |
| 425 | |
| 426 | } |
| 427 |