| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Services; |
| 4 |
|
| 5 |
use FluentCommunity\App\Models\BaseSpace; |
| 6 |
use FluentCommunity\Framework\Support\Arr; |
| 7 |
|
| 8 |
class LockscreenService |
| 9 |
{ |
| 10 |
public static function getLockscreenSettings(BaseSpace $space, $viewOnly = false) |
| 11 |
{ |
| 12 |
$defaultSettings = [ |
| 13 |
[ |
| 14 |
'hidden' => false, |
| 15 |
'type' => 'image', |
| 16 |
'label' => 'Banner', |
| 17 |
'name' => 'banner', |
| 18 |
'heading' => 'Banner Heading', |
| 19 |
'heading_color' => '#FFFFFF', |
| 20 |
'description' => 'Banner Description', |
| 21 |
'text_color' => '#FFFFFF', |
| 22 |
'button_text' => 'Buy Now', |
| 23 |
'button_link' => '', |
| 24 |
'button_color' => '#2B2E33', |
| 25 |
'button_text_color' => '#FFFFFF', |
| 26 |
'background_image' => '', |
| 27 |
'overlay_color' => '#798398', |
| 28 |
'new_tab' => 'no' |
| 29 |
], |
| 30 |
[ |
| 31 |
'hidden' => false, |
| 32 |
'type' => 'block', |
| 33 |
'label' => 'Description', |
| 34 |
'name' => 'description', |
| 35 |
'content' => '<!-- wp:paragraph --><p>Description</p><!-- /wp:paragraph -->', |
| 36 |
] |
| 37 |
]; |
| 38 |
|
| 39 |
if ($space->isCourseSpace()) { |
| 40 |
$defaultSettings[] = [ |
| 41 |
'hidden' => true, |
| 42 |
'type' => 'lesson', |
| 43 |
'label' => 'Lessons', |
| 44 |
'name' => 'lesson' |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
$defaultSettings[] = [ |
| 49 |
'hidden' => false, |
| 50 |
'type' => 'image', |
| 51 |
'label' => 'Call to action', |
| 52 |
'name' => 'action', |
| 53 |
'heading' => 'Call to Action Heading', |
| 54 |
'heading_color' => '#FFFFFF', |
| 55 |
'description' => 'Call to Action Description', |
| 56 |
'text_color' => '#FFFFFF', |
| 57 |
'button_text' => 'Buy Now', |
| 58 |
'button_link' => '', |
| 59 |
'button_color' => '#2B2E33', |
| 60 |
'button_text_color' => '#FFFFFF', |
| 61 |
'background_image' => '', |
| 62 |
'overlay_color' => '#798398', |
| 63 |
'new_tab' => 'no' |
| 64 |
]; |
| 65 |
|
| 66 |
$settings = (array) $space->getCustomMeta('lockscreen_settings', $defaultSettings); |
| 67 |
|
| 68 |
foreach ($settings as &$setting) { |
| 69 |
if ($viewOnly && Arr::get($setting, 'type') === 'block' && !empty($setting['content'])) { |
| 70 |
$user = Helper::getCurrentUser(); |
| 71 |
$content = apply_filters('the_content', $setting['content']); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 72 |
$setting['content'] = (new SmartCodeParser())->parse($content, $user); |
| 73 |
} |
| 74 |
|
| 75 |
if (Arr::get($setting, 'type') == 'image' && empty($setting['new_tab'])) { |
| 76 |
$setting['new_tab'] = 'no'; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return apply_filters('fluent_community/lockscreen_fields', $settings, $space); |
| 81 |
} |
| 82 |
|
| 83 |
public static function formatLockscreenFields($settingFields, $space) |
| 84 |
{ |
| 85 |
$currentSettings = self::getLockscreenSettings($space); |
| 86 |
|
| 87 |
$textFields = ['type', 'name', 'label', 'heading', 'description', 'button_text', 'heading_color', 'text_color', 'button_color', 'button_text_color', 'overlay_color', 'new_tab', 'background_color']; |
| 88 |
$urlFields = ['button_link']; |
| 89 |
|
| 90 |
$formattedFields = []; |
| 91 |
|
| 92 |
foreach ($settingFields as $value) { |
| 93 |
$textValues = array_map('sanitize_text_field', Arr::only($value, $textFields)); |
| 94 |
$urlValues = array_map('sanitize_url', Arr::only($value, $urlFields)); |
| 95 |
|
| 96 |
$formattedField = array_merge($textValues, $urlValues); |
| 97 |
|
| 98 |
$formattedField['hidden'] = Arr::isTrue($value, 'hidden'); |
| 99 |
|
| 100 |
if (Arr::get($value, 'type') == 'block') { |
| 101 |
$formattedField['content'] = CustomSanitizer::santizeEditorBody(Arr::get($value, 'content')); |
| 102 |
} |
| 103 |
|
| 104 |
if (isset($value['background_image'])) { |
| 105 |
$bgImage = sanitize_url($value['background_image']); |
| 106 |
$fieldName = Arr::get($value, 'name'); |
| 107 |
$currentImage = self::getCurrentImage($currentSettings, $fieldName); |
| 108 |
$bgImageUrl = self::handleMediaUrl($bgImage, $currentImage, $fieldName, $space->id); |
| 109 |
$formattedField['background_image'] = $bgImageUrl; |
| 110 |
} |
| 111 |
|
| 112 |
$formattedFields[] = apply_filters('fluent_community/lockscreen_formatted_field', $formattedField, $value, $space); |
| 113 |
} |
| 114 |
|
| 115 |
return $formattedFields; |
| 116 |
} |
| 117 |
|
| 118 |
public static function getLockscreenConfig(BaseSpace $space, $membership = null, $viewOnly = false) |
| 119 |
{ |
| 120 |
if ($space->privacy != 'private') { |
| 121 |
return null; |
| 122 |
} |
| 123 |
|
| 124 |
if($membership) { |
| 125 |
if($membership->pivot->status) { |
| 126 |
if($membership->pivot->status == 'pending') { |
| 127 |
return [ |
| 128 |
'is_pending' => true |
| 129 |
]; |
| 130 |
} |
| 131 |
return null; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
$showCustom = Arr::get($space->settings, 'custom_lock_screen', 'no') === 'yes'; |
| 136 |
$showPaywalls = Arr::get($space->settings, 'show_paywalls', 'no') === 'yes' && defined('FLUENTCART_VERSION'); |
| 137 |
$canSendRequest = Arr::get($space->settings, 'can_request_join', 'no') === 'yes'; |
| 138 |
|
| 139 |
$isRedirect = Arr::get($space->settings, 'custom_lock_screen', 'no') == 'redirect' && Arr::get($space->settings, 'onboard_redirect_url', false); |
| 140 |
$redirectUrl = ''; |
| 141 |
if($isRedirect) { |
| 142 |
$redirectUrl = Arr::get($space->settings, 'onboard_redirect_url', false); |
| 143 |
} |
| 144 |
|
| 145 |
return [ |
| 146 |
'showCustom' => $showCustom, |
| 147 |
'showPaywalls' => $showPaywalls, |
| 148 |
'showWelcomeBanner' => Arr::get($space->settings, 'show_welcome_banner', 'no') === 'yes', |
| 149 |
'canSendRequest' => $canSendRequest, |
| 150 |
'lockScreen' => $showCustom ? self::getLockscreenSettings($space, $viewOnly) : null, |
| 151 |
'redirect_url' => $redirectUrl |
| 152 |
]; |
| 153 |
} |
| 154 |
|
| 155 |
protected static function getCurrentImage($currentSettings, $fieldName) |
| 156 |
{ |
| 157 |
foreach ($currentSettings as $setting) { |
| 158 |
if (Arr::get($setting, 'name') === $fieldName) { |
| 159 |
return Arr::get($setting, 'background_image'); |
| 160 |
} |
| 161 |
} |
| 162 |
return null; |
| 163 |
} |
| 164 |
|
| 165 |
protected static function handleMediaUrl($url, $currentImage, $fieldName, $spaceId) |
| 166 |
{ |
| 167 |
$deleteMediaUrls = []; |
| 168 |
|
| 169 |
if ($url) { |
| 170 |
$media = Helper::getMediaFromUrl($url); |
| 171 |
if (!$media || $media->is_active) { |
| 172 |
return $currentImage; |
| 173 |
} |
| 174 |
|
| 175 |
$url = $media->public_url; |
| 176 |
|
| 177 |
$media->update([ |
| 178 |
'is_active' => true, |
| 179 |
'user_id' => get_current_user_id(), |
| 180 |
'sub_object_id' => $spaceId, |
| 181 |
'object_source' => 'lockscreen_' . $fieldName |
| 182 |
]); |
| 183 |
} |
| 184 |
|
| 185 |
if ($currentImage) { |
| 186 |
$deleteMediaUrls[] = $currentImage; |
| 187 |
} |
| 188 |
|
| 189 |
do_action('fluent_community/remove_medias_by_url', $deleteMediaUrls, [ |
| 190 |
'sub_object_id' => $spaceId, |
| 191 |
]); |
| 192 |
|
| 193 |
return $url; |
| 194 |
} |
| 195 |
} |
| 196 |
|