FapiMemberDivi.php
131 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FapiMember\Divi; |
| 4 | |
| 5 | use FapiMember\Container\Container; |
| 6 | use FapiMember\Model\Enums\UserPermission; |
| 7 | use FapiMember\Repository\LevelRepository; |
| 8 | use FapiMember\Repository\UserRepository; |
| 9 | use FapiMember\Utils\DisplayHelper; |
| 10 | |
| 11 | class FapiMemberDivi |
| 12 | { |
| 13 | private LevelRepository $levelRepository; |
| 14 | private UserRepository $userRepository; |
| 15 | |
| 16 | private array $actionOptions = [ |
| 17 | 'show' => 'Zobrazit vždy', |
| 18 | 'show_if' => 'Zobrazit když je členem', |
| 19 | 'hide_if' => 'Zobrazit když není členem', |
| 20 | ]; |
| 21 | public array $allowedModuleSlugs = [ |
| 22 | 'et_pb_section', |
| 23 | 'et_pb_row', |
| 24 | 'et_pb_column', |
| 25 | ]; |
| 26 | |
| 27 | public function __construct() |
| 28 | { |
| 29 | $this->levelRepository = Container::get(LevelRepository::class); |
| 30 | $this->userRepository = Container::get(UserRepository::class); |
| 31 | } |
| 32 | |
| 33 | public function getSectionsAsOptions(): array |
| 34 | { |
| 35 | $sections = $this->levelRepository->getAllSections(); |
| 36 | $sectionOptions = []; |
| 37 | |
| 38 | foreach ($sections as $section) { |
| 39 | $sectionOptions[] = [ |
| 40 | 'name' => $section->getName(), |
| 41 | 'id' => $section->getId(), |
| 42 | ]; |
| 43 | |
| 44 | foreach ($section->getLevels() as $level) { |
| 45 | $sectionOptions[] = [ |
| 46 | 'name' => '[' . $section->getName() . '] → ' . $level->getName(), |
| 47 | 'id' => $level->getId(), |
| 48 | ]; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return $sectionOptions; |
| 53 | } |
| 54 | |
| 55 | public function addFields(array $unprocessedFields): array |
| 56 | { |
| 57 | $newFields['fm-action-field'] = [ |
| 58 | 'label' => esc_html__( 'Zobrazení prvku', 'fmd-fm-divi' ), |
| 59 | 'type' => 'select', |
| 60 | 'option_category' => 'basic_option', |
| 61 | 'options' => $this->actionOptions, |
| 62 | 'description' => esc_html__( 'Podmínka, pod kterou se element zobrazí', 'fmd-fm-divi' ), |
| 63 | 'toggle_slug' => 'fapi-member', |
| 64 | 'tab_slug' => 'custom_css', |
| 65 | ]; |
| 66 | |
| 67 | $newFields['fm-level-field'] = [ |
| 68 | 'label' => esc_html__( 'Členské sekce/úrovně', 'fmd-fm-divi' ), |
| 69 | 'type' => 'fmd_multi_select', |
| 70 | 'option_category' => 'basic_option', |
| 71 | 'options' => $this->getSectionsAsOptions(), |
| 72 | 'description' => esc_html__( 'Výběr členských sekcí/úrovní, na které je podmínka aplikována', 'fmd-fm-divi' ), |
| 73 | 'toggle_slug' => 'fapi-member', |
| 74 | 'tab_slug' => 'custom_css', |
| 75 | ]; |
| 76 | |
| 77 | return array_merge($unprocessedFields, $newFields); |
| 78 | } |
| 79 | |
| 80 | public function addToggle(array $modules): array |
| 81 | { |
| 82 | foreach($this->allowedModuleSlugs as $slug) { |
| 83 | if(isset($modules[$slug]) && is_object($modules[$slug])) { |
| 84 | $modules[$slug]->settings_modal_toggles['custom_css']['toggles']['fapi-member'] = __( 'FAPI Member', 'fapi-member' ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return $modules; |
| 89 | } |
| 90 | |
| 91 | public function hideElements($output, $props, $attrs, $slug) |
| 92 | { |
| 93 | |
| 94 | if (et_fb_is_enabled()) { |
| 95 | return $output; |
| 96 | } |
| 97 | |
| 98 | if(!isset($props['fm-action-field'])){ |
| 99 | return $output; |
| 100 | } |
| 101 | |
| 102 | $action = $props['fm-action-field']; |
| 103 | |
| 104 | if ($action !== 'show_if' && $action !== 'hide_if') { |
| 105 | return $output; |
| 106 | } |
| 107 | |
| 108 | $user = $this->userRepository->getCurrentUser(); |
| 109 | |
| 110 | if ($user === null) { |
| 111 | return $action === 'hide_if' ? $output : ''; |
| 112 | } elseif (current_user_can(UserPermission::REQUIRED_CAPABILITY)) { |
| 113 | return $output; |
| 114 | } |
| 115 | |
| 116 | $levelIds = json_decode($props['fm-level-field'], true); |
| 117 | $show = DisplayHelper::shouldContentBeRendered( |
| 118 | $action === 'show_if', |
| 119 | $levelIds, |
| 120 | $user->getId() |
| 121 | ); |
| 122 | |
| 123 | if (!$show) { |
| 124 | return ''; |
| 125 | } |
| 126 | |
| 127 | return $output; |
| 128 | } |
| 129 | |
| 130 | } |
| 131 |