| 1 |
<?php |
| 2 |
/** |
| 3 |
* Addons functionality. |
| 4 |
* |
| 5 |
* @link https://duckdev.com/products/loggedin-limit-active-logins/ |
| 6 |
* @license http://www.gnu.org/licenses/ GNU General Public License |
| 7 |
* @package Loggedin |
| 8 |
* @subpackage Freemius |
| 9 |
* @author Joel James <me@joelsays.com> |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace DuckDev\Loggedin; |
| 13 |
|
| 14 |
// If this file is called directly, abort. |
| 15 |
defined( 'WPINC' ) || die; |
| 16 |
|
| 17 |
use DuckDev\Freemius\Freemius; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class Addons |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
class Addons { |
| 25 |
|
| 26 |
/** |
| 27 |
* Freemius ID of the addon. |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* |
| 31 |
* @var int |
| 32 |
*/ |
| 33 |
const FREEMIUS_ID = 19328; |
| 34 |
|
| 35 |
/** |
| 36 |
* Freemius instance. |
| 37 |
* |
| 38 |
* @var Freemius[] |
| 39 |
*/ |
| 40 |
protected array $freemius = array(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Initialize the class and set its properties. |
| 44 |
* |
| 45 |
* We register all our admin hooks here. |
| 46 |
* |
| 47 |
* @since 1.0.0 |
| 48 |
* @access public |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function __construct() { |
| 53 |
// Initialise Freemius. |
| 54 |
add_action( 'admin_init', array( $this, 'init_freemius' ) ); |
| 55 |
// Process license activate/deactivate. |
| 56 |
add_action( 'admin_init', array( $this, 'process_license' ) ); |
| 57 |
// Process addon list refresh. |
| 58 |
add_action( 'admin_init', array( $this, 'process_addons_refresh' ) ); |
| 59 |
// Add template vars. |
| 60 |
add_filter( 'loggedin_admin_page_vars', array( $this, 'admin_page_vars' ) ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get the list of addons. |
| 65 |
* |
| 66 |
* Get the latest list from the Freemius API. |
| 67 |
* It will be cached for 1 day. |
| 68 |
* |
| 69 |
* @since 2.0.0 |
| 70 |
* |
| 71 |
* @param bool $force Should force refresh. |
| 72 |
* |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
public function get_addons( bool $force = false ): array { |
| 76 |
// Get addon list from the API. |
| 77 |
return $this->freemius[ self::FREEMIUS_ID ]->addon()->get_addons( $force ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Create new options field to the settings page. |
| 82 |
* |
| 83 |
* @since 2.0.0 |
| 84 |
* @uses get_option() To get the option value. |
| 85 |
* |
| 86 |
* @return array |
| 87 |
*/ |
| 88 |
public function get_license_items(): array { |
| 89 |
$items = array(); |
| 90 |
$addons = $this->get_registered_addons(); |
| 91 |
|
| 92 |
foreach ( $addons as $id => $args ) { |
| 93 |
if ( isset( $this->freemius[ $id ] ) ) { |
| 94 |
$activation = $this->freemius[ $id ]->license()->get_activation_data(); |
| 95 |
$items[ $id ] = array( |
| 96 |
'key' => $activation['activation_params']['license_key'] ?? '', |
| 97 |
'status' => $activation['status'] ?? '', |
| 98 |
'plugin' => $this->freemius[ $id ]->license()->get_plugin()->get_data(), |
| 99 |
); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return $items; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Add addon related vars to template vars. |
| 108 |
* |
| 109 |
* @since 2.0.0 |
| 110 |
* |
| 111 |
* @param array $vars Variables. |
| 112 |
* |
| 113 |
* @return array |
| 114 |
*/ |
| 115 |
public function admin_page_vars( array $vars ): array { |
| 116 |
$addons = $this->get_addons(); |
| 117 |
|
| 118 |
// If addons are empty, remove the tab. |
| 119 |
if ( empty( $addons ) ) { |
| 120 |
unset( $vars['addons'] ); |
| 121 |
} |
| 122 |
|
| 123 |
$vars['addons'] = $addons; |
| 124 |
$vars['license_items'] = $this->get_license_items(); |
| 125 |
$vars['registered_addons'] = $this->get_registered_addons(); |
| 126 |
|
| 127 |
return $vars; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Process license activation and deactivation form submit. |
| 132 |
* |
| 133 |
* @since 2.0.0 |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
public function process_license() { |
| 138 |
// Form data. |
| 139 |
$data = wp_unslash( $_POST['loggedin_licenses'] ?? '' ); // phpcs:ignore |
| 140 |
|
| 141 |
// We need all these data to continue. |
| 142 |
if ( ! isset( $data['nonce'], $data['action'], $data['key'], $data['id'] ) ) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
// Nonce verification first. |
| 147 |
if ( ! wp_verify_nonce( $data['nonce'], 'loggedin_licenses[nonce]' ) ) { |
| 148 |
add_settings_error( 'loggedin_licenses', 'nonce_check_failed', __( 'Nonce check failed.', 'loggedin' ) ); |
| 149 |
|
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
// Now check permissions. |
| 154 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 155 |
add_settings_error( 'loggedin_licenses', 'permission_check_failed', __( 'You do not have the permission to do this.', 'loggedin' ) ); |
| 156 |
|
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
// Prepare form data. |
| 161 |
$id = intval( $data['id'] ); |
| 162 |
$key = sanitize_text_field( $data['key'] ); |
| 163 |
$action = 'activate' === $data['action'] ? 'activate' : 'deactivate'; |
| 164 |
|
| 165 |
// Do not continue if addon not initialised. |
| 166 |
if ( ! isset( $this->freemius[ $id ] ) ) { |
| 167 |
add_settings_error( 'loggedin_licenses', 'addon_not_initialized', __( 'Addon not initialized.', 'loggedin' ) ); |
| 168 |
|
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
if ( 'activate' === $action ) { |
| 173 |
$response = $this->freemius[ $id ]->license()->activate( $key ); |
| 174 |
} else { |
| 175 |
$response = $this->freemius[ $id ]->license()->deactivate(); |
| 176 |
} |
| 177 |
|
| 178 |
if ( is_wp_error( $response ) ) { |
| 179 |
// Show error notice. |
| 180 |
add_settings_error( 'loggedin_licenses', $response->get_error_code(), $response->get_error_message() ); |
| 181 |
} else { |
| 182 |
// Show success notice.' |
| 183 |
add_settings_error( |
| 184 |
'loggedin_licenses', |
| 185 |
$action, |
| 186 |
'activate' === $action ? __( 'Your license key has been activated successfully.', 'loggedin' ) : __( 'Your license key has been deactivated.', 'loggedin' ), |
| 187 |
'updated' |
| 188 |
); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Process addon list refresh request. |
| 194 |
* |
| 195 |
* @since 2.0.0 |
| 196 |
* |
| 197 |
* @return void |
| 198 |
*/ |
| 199 |
public function process_addons_refresh() { |
| 200 |
// We need all these data to continue. |
| 201 |
if ( ! isset( $_REQUEST['_wpnonce'], $_REQUEST['loggedin-addons-refresh'] ) ) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
// Nonce verification first. |
| 206 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'loggedin-addons-refresh' ) ) { |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
// Now check permissions. |
| 211 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
// Force refresh addons. |
| 216 |
$this->get_addons( true ); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Initialise Freemius SDK. |
| 221 |
* |
| 222 |
* This will create new Freemius instance for the parent plugin |
| 223 |
* as well as for all registered addons. |
| 224 |
* |
| 225 |
* @since 2.0.0 |
| 226 |
* |
| 227 |
* @return void |
| 228 |
*/ |
| 229 |
public function init_freemius(): void { |
| 230 |
// Initialize Freemius for Loggedin. |
| 231 |
$this->freemius[ self::FREEMIUS_ID ] = Freemius::get_instance( |
| 232 |
self::FREEMIUS_ID, |
| 233 |
array( |
| 234 |
'slug' => 'loggedin', |
| 235 |
'is_premium' => false, |
| 236 |
'main_file' => LOGGEDIN_FILE, |
| 237 |
'public_key' => 'pk_4c8df806d035c90805a97eae5fba5', |
| 238 |
'has_addons' => true, |
| 239 |
) |
| 240 |
); |
| 241 |
|
| 242 |
// Initialise Freemius for each addon. |
| 243 |
foreach ( $this->get_registered_addons() as $id => $args ) { |
| 244 |
$this->freemius[ $id ] = Freemius::get_instance( $id, $args ); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Get list of registered addons. |
| 250 |
* |
| 251 |
* @since 2.0.0 |
| 252 |
* |
| 253 |
* @return array |
| 254 |
*/ |
| 255 |
protected function get_registered_addons(): array { |
| 256 |
/** |
| 257 |
* Get the registered addons. |
| 258 |
* |
| 259 |
* Addon plugins should use this filter to register it. |
| 260 |
* |
| 261 |
* @since 2.0.0 |
| 262 |
* |
| 263 |
* @param array $addons Addon list. |
| 264 |
*/ |
| 265 |
return apply_filters( 'loggedin_register_addon', array() ); |
| 266 |
} |
| 267 |
} |
| 268 |
|