abilities
2 days ago
admin
2 days ago
compatibility
2 days ago
extensions
2 days ago
foopluginbase
2 days ago
public
2 days ago
thumbs
2 days ago
asset-manifest.php
2 days ago
class-attachment-filters.php
2 days ago
class-command-palette.php
2 days ago
class-foogallery-animated-gif-support.php
2 days ago
class-foogallery-attachment-custom-class.php
2 days ago
class-foogallery-attachment-filename.php
2 days ago
class-foogallery-attachment-type.php
2 days ago
class-foogallery-attachment.php
2 days ago
class-foogallery-cache.php
2 days ago
class-foogallery-common-fields.php
2 days ago
class-foogallery-crop-position.php
2 days ago
class-foogallery-datasource-media_library.php
2 days ago
class-foogallery-debug.php
2 days ago
class-foogallery-delayed-runtime-loader.php
2 days ago
class-foogallery-extensions-compatibility.php
2 days ago
class-foogallery-force-https.php
2 days ago
class-foogallery-lazyload.php
2 days ago
class-foogallery-license-constant-handler.php
2 days ago
class-foogallery-lightbox.php
2 days ago
class-foogallery-no-javascript-fallback.php
2 days ago
class-foogallery-paging.php
2 days ago
class-foogallery-password-protect.php
2 days ago
class-foogallery-sitemaps.php
2 days ago
class-foogallery-widget.php
2 days ago
class-foogallery.php
2 days ago
class-gallery-advanced-settings.php
2 days ago
class-il8n.php
2 days ago
class-override-thumbnail.php
2 days ago
class-posttypes.php
2 days ago
class-previews.php
2 days ago
class-retina.php
2 days ago
class-thumbnail-dimensions.php
2 days ago
class-thumbnails.php
2 days ago
class-version-check.php
2 days ago
constants.php
2 days ago
functions.php
2 days ago
gallery-management-functions.php
2 days ago
includes.php
2 days ago
index.php
2 days ago
render-functions.php
2 days ago
class-foogallery-license-constant-handler.php
151 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles automatic Freemius license activation from a predefined constant. |
| 4 | * |
| 5 | * @package FooGallery |
| 6 | * @author FooPlugins |
| 7 | * @license GPL-2.0+ |
| 8 | * @link https://github.com/fooplugins/foogallery |
| 9 | */ |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Automatically applies a Freemius license key defined via a constant. |
| 17 | */ |
| 18 | class FooGallery_License_Constant_Handler { |
| 19 | |
| 20 | const LICENSE_CONSTANT = 'FOOGALLERY_LICENSE_KEY'; |
| 21 | |
| 22 | const OPTION_STATE = 'foogallery_license_activation_state'; |
| 23 | |
| 24 | /** |
| 25 | * Mark that activation just ran so we can attempt license application on the next admin load. |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public static function flag_activation() { |
| 30 | if ( ! defined( self::LICENSE_CONSTANT ) ) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | update_option( self::OPTION_STATE, 'pending' ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Boot the handler hooks. |
| 39 | * |
| 40 | * @return void |
| 41 | */ |
| 42 | public static function init() { |
| 43 | if ( ! defined( self::LICENSE_CONSTANT ) ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | $handler = new self(); |
| 48 | |
| 49 | add_action( 'admin_init', array( $handler, 'license_key_auto_activation' ) ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Attempt to apply the configured license key. |
| 54 | * |
| 55 | * @return void |
| 56 | */ |
| 57 | public function license_key_auto_activation() { |
| 58 | $state = get_option( self::OPTION_STATE, '' ); |
| 59 | |
| 60 | if ( 'pending' !== $state && 1 !== $state ) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if ( is_network_admin() || ! current_user_can( 'activate_plugins' ) ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | if ( ! defined( self::LICENSE_CONSTANT ) ) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | $license_key = $this->sanitize_license_constant( constant( self::LICENSE_CONSTANT ) ); |
| 73 | |
| 74 | if ( '' === $license_key ) { |
| 75 | $this->mark_state( 'invalid_constant' ); |
| 76 | |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | if ( ! function_exists( 'foogallery_fs' ) ) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | $fs = foogallery_fs(); |
| 85 | |
| 86 | if ( $fs->has_active_valid_license() ) { |
| 87 | $this->mark_state( 'done' ); |
| 88 | |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | try { |
| 93 | $next_page = $fs->activate_migrated_license( $license_key, null, null, array(), null ); |
| 94 | } catch ( Exception $exception ) { |
| 95 | $this->mark_state( 'unexpected_error' ); |
| 96 | |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | if ( $fs->can_use_premium_code() ) { |
| 101 | $this->mark_state( 'done' ); |
| 102 | } else { |
| 103 | $this->mark_state( 'failed' ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Validate and normalize the license constant value. |
| 109 | * |
| 110 | * @param mixed $license_constant Raw constant value. |
| 111 | * |
| 112 | * @return string |
| 113 | */ |
| 114 | private function sanitize_license_constant( $license_constant ) { |
| 115 | if ( ! is_string( $license_constant ) ) { |
| 116 | return ''; |
| 117 | } |
| 118 | |
| 119 | $license_key = trim( $license_constant ); |
| 120 | |
| 121 | if ( '' === $license_key ) { |
| 122 | return ''; |
| 123 | } |
| 124 | |
| 125 | if ( 32 !== strlen( $license_key ) ) { |
| 126 | return ''; |
| 127 | } |
| 128 | |
| 129 | if ( 0 !== strncmp( $license_key, 'sk_', 3 ) ) { |
| 130 | return ''; |
| 131 | } |
| 132 | |
| 133 | if ( false !== strpbrk( $license_key, " \t\n\r\0\x0B" ) ) { |
| 134 | return ''; |
| 135 | } |
| 136 | |
| 137 | return $license_key; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Update the activation attempt state. |
| 142 | * |
| 143 | * @param string $state New state value. |
| 144 | * |
| 145 | * @return void |
| 146 | */ |
| 147 | private function mark_state( $state ) { |
| 148 | update_option( self::OPTION_STATE, $state ); |
| 149 | } |
| 150 | } |
| 151 |