test
3 weeks ago
useCompleteOnboarding.js
3 weeks ago
useDateRangePicker.js
1 month ago
useEmail.ts
1 month ago
useEngagementChartData.js
1 month ago
useLicenseSettings.js
1 month ago
useLink.js
1 month ago
useMediaDetail.js
1 month ago
useMediaLibrary.js
1 month ago
useMediaList.ts
1 month ago
usePerformanceSettings.js
1 month ago
useRegisterActivePage.js
1 month ago
useSettingOption.js
1 month ago
useSimpleSettingsPage.js
1 month ago
useTopPerforming.js
1 month ago
useTopVideosPaginated.js
1 month ago
useUpgradeCTA.js
1 month ago
useUserDetail.js
1 month ago
useUpgradeCTA.js
75 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | |
| 3 | /** |
| 4 | * Returns the CTA copy + click handler for the in-app pro-gate surfaces: |
| 5 | * |
| 6 | * 1. No Pro plugin → "Upgrade Now" → external pricing |
| 7 | * 2. Pro plugin active, no license OR key → "Activate License" → in-app license tab |
| 8 | * 3. Pro plugin active + licensed → caller should hide the CTA |
| 9 | * |
| 10 | * State flags (`isLicenseInvalid`, `hasLicenseKey`, `renewLink`) are also |
| 11 | * returned so callers that want to *differentiate* the invalid-key state |
| 12 | * (e.g. top-of-dashboard banner, WP admin notice) can override the defaults. |
| 13 | * |
| 14 | * Pass the dashboard's `history` so SPA callers soft-navigate; non-SPA callers |
| 15 | * (e.g. the block-editor modal) omit it and get a hard navigation to the same URL. |
| 16 | */ |
| 17 | const useUpgradeCTA = ( history ) => { |
| 18 | const data = window?.prestoPlayer || {}; |
| 19 | const isProPluginActive = !! data.isProPluginActive; |
| 20 | const isPremium = !! data.isPremium; |
| 21 | const hasLicenseKey = !! data.hasLicenseKey; |
| 22 | const isProUnlicensed = isProPluginActive && ! isPremium; |
| 23 | const isLicenseInvalid = isProUnlicensed && hasLicenseKey; |
| 24 | |
| 25 | const externalUpgradeLink = |
| 26 | data.upgradeLink || |
| 27 | data.upgrade_link || |
| 28 | 'https://prestoplayer.com/pricing/'; |
| 29 | |
| 30 | const renewLink = |
| 31 | data.renewLink || 'https://my.prestomade.com/my-account/'; |
| 32 | |
| 33 | const dashboardUrl = data.dashboardUrl || 'admin.php?page=presto-dashboard'; |
| 34 | const licenseSettingsHref = `${ dashboardUrl }&tab=Settings§ion=license`; |
| 35 | |
| 36 | const label = isProUnlicensed |
| 37 | ? __( 'Activate License', 'presto-player' ) |
| 38 | : __( 'Upgrade Now', 'presto-player' ); |
| 39 | |
| 40 | const onClick = ( e ) => { |
| 41 | if ( isProUnlicensed ) { |
| 42 | e?.preventDefault?.(); |
| 43 | if ( history?.push ) { |
| 44 | history.push( { tab: 'Settings', section: 'license' } ); |
| 45 | } else { |
| 46 | window.location.href = licenseSettingsHref; |
| 47 | } |
| 48 | return; |
| 49 | } |
| 50 | // Skip when an <a target="_blank" href> already handles the click, |
| 51 | // otherwise we'd double-open the pricing page. |
| 52 | const target = e?.currentTarget; |
| 53 | if ( target?.tagName === 'A' && target.target === '_blank' ) { |
| 54 | return; |
| 55 | } |
| 56 | window.open( externalUpgradeLink, '_blank', 'noopener noreferrer' ); |
| 57 | }; |
| 58 | |
| 59 | return { |
| 60 | label, |
| 61 | isProUnlicensed, |
| 62 | isLicenseInvalid, |
| 63 | isProPluginActive, |
| 64 | isPremium, |
| 65 | hasLicenseKey, |
| 66 | href: isProUnlicensed ? licenseSettingsHref : externalUpgradeLink, |
| 67 | licenseSettingsHref, |
| 68 | externalUpgradeLink, |
| 69 | renewLink, |
| 70 | onClick, |
| 71 | }; |
| 72 | }; |
| 73 | |
| 74 | export default useUpgradeCTA; |
| 75 |