PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 4.0.0
WP 2FA – Two-factor authentication for WordPress v4.0.0
4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 3.0.1 3.1.0 3.1.1 3.1.1.2 trunk 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.7.0
wp-2fa / includes / classes / Admin / class-license-page.php
wp-2fa / includes / classes / Admin Last commit date
AdminSettings 5 days ago Controllers 5 days ago Fly-Out 5 days ago Helpers 5 days ago Methods 5 days ago Settings 5 days ago SettingsPages 5 days ago Views 5 days ago class-about-us.php 5 days ago class-docs-and-support.php 5 days ago class-free-support-notice.php 5 days ago class-help-contact-us.php 5 days ago class-license-page.php 5 days ago class-new-interface-notice.php 5 days ago class-plugin-updated-notice.php 5 days ago class-premium-features.php 5 days ago class-profile-section-renderer.php 5 days ago class-settings-page.php 5 days ago class-setup-wizard.php 5 days ago class-top-bar-banner.php 5 days ago class-user-listing.php 5 days ago class-user-notices.php 5 days ago class-user-profile.php 5 days ago class-user-registered.php 5 days ago class-wizard-integration.php 5 days ago index.php 5 days ago
class-license-page.php
230 lines
1 <?php
2 /**
3 * License page rendering class.
4 *
5 * @package wp2fa
6 * @subpackage admin
7 * @copyright 2026 Melapress
8 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
9 * @link https://wordpress.org/plugins/wp-2fa/
10 * @since 4.0.0
11 */
12
13 declare( strict_types=1 );
14
15 namespace WP2FA\Admin;
16
17 use WP2FA\WP2FA;
18 use WP2FA\Licensing\Licensing_Factory;
19
20 if ( ! class_exists( '\WP2FA\Admin\License_Page' ) ) {
21
22 /**
23 * Handles the License admin page.
24 *
25 * Provides a submenu item that allows users to activate or manage
26 * their license key when using Freemius as the licensing provider.
27 *
28 * @since 4.0.0
29 */
30 class License_Page {
31
32 const PAGE_SLUG = 'wp-2fa-license';
33
34 /**
35 * Create admin submenu entry for the License page.
36 *
37 * @return void
38 *
39 * @since 4.0.0
40 */
41 public static function add_extra_menu_item() {
42
43 // Only show when Freemius is the active provider.
44 if ( 'freemius' !== Licensing_Factory::get_provider_type() ) {
45 return;
46 }
47
48 // Hide the License page when license is already active and valid.
49 if ( Licensing_Factory::is_registered() && Licensing_Factory::has_active_valid_license() ) {
50 return;
51 }
52
53 \add_submenu_page(
54 Settings_Page::TOP_MENU_SLUG,
55 \esc_html__( 'License', 'wp-2fa' ),
56 \esc_html__( 'License', 'wp-2fa' ),
57 'manage_options',
58 self::PAGE_SLUG,
59 array( __CLASS__, 'render' ),
60 5
61 );
62
63 // Redirect to Freemius account page if user is registered and has valid license.
64 \add_action( 'current_screen', array( __CLASS__, 'maybe_redirect' ) );
65 }
66
67 /**
68 * Register AJAX handlers. Must be called independently of menu registration
69 * since AJAX requests don't trigger admin_menu.
70 *
71 * @return void
72 *
73 * @since 4.0.0
74 */
75 public static function register_ajax_handlers() {
76 // Freemius handles its own license activation AJAX.
77 // We just need to ensure the dialog is rendered on our page.
78 \add_action( 'current_screen', array( __CLASS__, 'maybe_enqueue_freemius_dialog' ) );
79 }
80
81 /**
82 * Enqueue Freemius license activation dialog on our License page.
83 *
84 * @return void
85 *
86 * @since 4.0.0
87 */
88 public static function maybe_enqueue_freemius_dialog() {
89 $screen = \get_current_screen();
90 if ( null === $screen || ! self::is_license_screen( $screen ) ) {
91 return;
92 }
93
94 // Get the Freemius instance to render its license activation dialog.
95 $fs = Licensing_Factory::provider_call( 'get_provider_instance' );
96 if ( $fs && false !== $fs && \is_object( $fs ) ) {
97 // Render the Freemius license activation dialog in footer.
98 \add_action( 'admin_footer', array( $fs, '_add_license_activation_dialog_box' ) );
99 }
100 }
101
102 /**
103 * Redirect to Freemius account page when user already has an active license.
104 *
105 * @return void
106 *
107 * @since 4.0.0
108 */
109 public static function maybe_redirect() {
110 if ( ! \current_user_can( 'manage_options' ) ) {
111 return;
112 }
113
114 $screen = \get_current_screen();
115 if ( null === $screen || ! self::is_license_screen( $screen ) ) {
116 return;
117 }
118
119 // If user is registered and has a valid license, redirect to account page.
120 if ( Licensing_Factory::is_registered() && Licensing_Factory::has_active_valid_license() ) {
121 $account_url = Licensing_Factory::get_account_url();
122 if ( ! empty( $account_url ) ) {
123 \wp_safe_redirect( $account_url );
124 exit;
125 }
126 }
127 }
128
129 /**
130 * Check if the current screen is the license page (single site or network admin).
131 *
132 * @param \WP_Screen $screen The current screen object.
133 *
134 * @return bool
135 *
136 * @since 4.0.0
137 */
138 private static function is_license_screen( $screen ) {
139 $base_id = 'wp-2fa_page_' . self::PAGE_SLUG;
140
141 return $screen->id === $base_id || $screen->id === $base_id . '-network';
142 }
143
144 /**
145 * Render the License page.
146 *
147 * @return void
148 *
149 * @since 4.0.0
150 */
151 public static function render() {
152 if ( ! \current_user_can( 'manage_options' ) ) {
153 \wp_die( \esc_html__( 'You do not have sufficient permissions to access this page.', 'wp-2fa' ) );
154 }
155
156 $main_user = ! empty( WP2FA::get_wp2fa_setting( '2fa_settings_last_updated_by' ) ) ? (int) WP2FA::get_wp2fa_setting( '2fa_settings_last_updated_by' ) : \get_current_user_id();
157 if ( ! empty( WP2FA::get_wp2fa_general_setting( 'limit_access' ) ) && $main_user !== \get_current_user_id() ) {
158 echo \esc_html__( 'These settings have been disabled by your site administrator, please contact them for further assistance.', 'wp-2fa' );
159 return;
160 }
161
162 $account_url = Licensing_Factory::get_account_url();
163 $pricing_url = Licensing_Factory::get_pricing_url();
164
165 echo '<div class="wrap">';
166 echo '<h1>' . \esc_html__( 'License', 'wp-2fa' ) . '</h1>';
167
168 if ( Licensing_Factory::has_active_valid_license() ) {
169 echo '<div class="notice notice-success inline"><p>';
170 echo \esc_html__( 'Your license is active and valid.', 'wp-2fa' );
171 echo '</p></div>';
172 if ( $account_url ) {
173 echo '<p><a href="' . \esc_url( $account_url ) . '" class="button button-primary">';
174 echo \esc_html__( 'Manage License', 'wp-2fa' );
175 echo '</a></p>';
176 }
177 } else {
178 self::render_license_activation_form();
179 }
180
181 echo '</div>';
182 }
183
184 /**
185 * Render the license activation form.
186 *
187 * Uses Freemius's own license activation dialog which handles
188 * the full activation flow including API communication.
189 *
190 * @return void
191 *
192 * @since 4.0.0
193 */
194 private static function render_license_activation_form() {
195 $pricing_url = Licensing_Factory::get_pricing_url();
196
197 // Get the Freemius unique affix for the dialog trigger class.
198 $fs = Licensing_Factory::provider_call( 'get_provider_instance' );
199 $unique_affix = ( $fs && false !== $fs && \is_object( $fs ) ) ? $fs->get_unique_affix() : 'wp-2fa';
200
201 ?>
202 <div class="wp2fa-license-activation-wrap">
203 <div class="notice notice-warning inline">
204 <p><?php \esc_html_e( 'No active license found. Activate your license key below to unlock all premium features.', 'wp-2fa' ); ?></p>
205 </div>
206
207 <div class="wp2fa-license-form" style="margin-top: 20px;">
208 <p>
209 <a href="#" class="button button-primary activate-license-trigger <?php echo \esc_attr( $unique_affix ); ?>">
210 <?php \esc_html_e( 'Activate License', 'wp-2fa' ); ?>
211 </a>
212 </p>
213 </div>
214
215 <p class="description" style="margin-top: 20px;">
216 <?php
217 printf(
218 /* translators: %s: link to pricing page */
219 \esc_html__( 'Don\'t have a license key? %s', 'wp-2fa' ),
220 '<a href="' . \esc_url( $pricing_url ) . '" target="_blank">' . \esc_html__( 'Purchase one here', 'wp-2fa' ) . '</a>'
221 );
222 ?>
223 </p>
224 </div>
225 <?php
226 }
227
228 }
229 }
230