class-settings-addons.php
4 months ago
class-settings-base.php
5 months ago
class-settings-builder.php
5 months ago
class-settings-capabilities.php
5 months ago
class-settings-folders.php
4 months ago
class-settings-galleries.php
5 months ago
class-settings-general.php
5 months ago
class-settings-licenses.php
4 months ago
class-settings-lightboxes.php
4 months ago
class-settings-remote-library.php
5 months ago
class-settings-licenses.php
304 lines
| 1 | <?php |
| 2 | // exit if accessed directly |
| 3 | if ( ! defined( 'ABSPATH' ) ) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Responsive_Lightbox_Settings_Licenses class. |
| 8 | * |
| 9 | * Settings page class for Licenses tab migration to new Settings API. |
| 10 | * |
| 11 | * @class Responsive_Lightbox_Settings_Licenses |
| 12 | */ |
| 13 | class Responsive_Lightbox_Settings_Licenses extends Responsive_Lightbox_Settings_Base { |
| 14 | |
| 15 | /** |
| 16 | * Tab key identifier. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | const TAB_KEY = 'licenses'; |
| 21 | |
| 22 | /** |
| 23 | * Register this tab as migrated to new API. |
| 24 | * |
| 25 | * Override - only register if extensions exist. |
| 26 | * |
| 27 | * @param array $tabs Migrated tabs. |
| 28 | * @return array |
| 29 | */ |
| 30 | public function register_migrated_tab( $tabs ) { |
| 31 | $extensions = apply_filters( 'rl_settings_licenses', [] ); |
| 32 | if ( $extensions ) { |
| 33 | $tabs[] = self::TAB_KEY; |
| 34 | } |
| 35 | return $tabs; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Validate settings for licenses tab. |
| 40 | * |
| 41 | * Override to use licenses-specific validation. |
| 42 | * |
| 43 | * @param array $input Input data from form submission. |
| 44 | * @return array Validated data. |
| 45 | */ |
| 46 | public function validate( $input ) { |
| 47 | // use licenses-specific validation |
| 48 | return $this->validate_licenses( $input ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Provide settings data for licenses. |
| 53 | * |
| 54 | * @param array $data Settings data. |
| 55 | * @return array |
| 56 | */ |
| 57 | public function settings_data( $data ) { |
| 58 | // get extensions |
| 59 | $extensions = apply_filters( 'rl_settings_licenses', [] ); |
| 60 | |
| 61 | if ( $extensions ) { |
| 62 | $data[self::TAB_KEY] = [ |
| 63 | 'option_name' => 'responsive_lightbox_licenses', |
| 64 | 'option_group' => 'responsive_lightbox_licenses', |
| 65 | 'validate' => [ $this, 'validate' ], |
| 66 | 'sections' => [ |
| 67 | 'responsive_lightbox_licenses' => [ |
| 68 | 'title' => __( 'Licenses', 'responsive-lightbox' ), |
| 69 | 'callback' => [ $this, 'licenses_section_cb' ] |
| 70 | ] |
| 71 | ], |
| 72 | 'fields' => [] |
| 73 | ]; |
| 74 | |
| 75 | // add fields for each extension |
| 76 | foreach ( $extensions as $id => $extension ) { |
| 77 | $data[self::TAB_KEY]['fields'][$id] = [ |
| 78 | 'title' => $extension['name'], |
| 79 | 'section' => 'responsive_lightbox_licenses', |
| 80 | 'type' => 'custom', |
| 81 | 'callback' => [ $this, 'license_field_cb' ], |
| 82 | 'callback_args' => $extension |
| 83 | ]; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return $data; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Licenses section callback. |
| 92 | * |
| 93 | * @return void |
| 94 | */ |
| 95 | public function licenses_section_cb() { |
| 96 | ?><p class="description"><?php esc_html_e( 'A list of licenses for your Responsive Lightbox & Gallery extensions.', 'responsive-lightbox' ); ?></p><?php |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * License field callback. |
| 101 | * |
| 102 | * @param array $args |
| 103 | * @return void |
| 104 | */ |
| 105 | public function license_field_cb( $args ) { |
| 106 | // get extension data from callback_args (new API) or directly from args (legacy) |
| 107 | $extension = ! empty( $args['callback_args'] ) ? $args['callback_args'] : $args; |
| 108 | $extension_id = ! empty( $extension['id'] ) ? $extension['id'] : ''; |
| 109 | $extension_name = ! empty( $extension['name'] ) ? $extension['name'] : ''; |
| 110 | |
| 111 | $licenses = get_option( 'responsive_lightbox_licenses' ); |
| 112 | $licenses = is_array( $licenses ) ? $licenses : []; |
| 113 | |
| 114 | if ( ! empty( $licenses ) && ! empty( $extension_id ) ) { |
| 115 | $license = isset( $licenses[$extension_id]['license'] ) ? $licenses[$extension_id]['license'] : ''; |
| 116 | $status = ! empty( $licenses[$extension_id]['status'] ); |
| 117 | } else { |
| 118 | $license = ''; |
| 119 | $status = false; |
| 120 | } ?> |
| 121 | <fieldset class="rl_license rl_license-<?php echo esc_attr( $extension_id ); ?>"> |
| 122 | <input type="text" class="regular-text" name="responsive_lightbox_licenses[<?php echo esc_attr( $extension_id ); ?>][license]" value="<?php echo esc_attr( $license ); ?>"><span class="dashicons <?php echo ( $status ? 'dashicons-yes' : 'dashicons-no' ); ?>"></span> |
| 123 | <p class="description"><?php echo esc_html( sprintf( __( 'Enter your license key to activate %s extension and enable automatic upgrade notices.', 'responsive-lightbox' ), $extension_name ) ); ?></p> |
| 124 | </fieldset> |
| 125 | <?php |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Validate licenses function. |
| 130 | * |
| 131 | * Handles license activation/deactivation with external license server API. |
| 132 | * Called by validate() method. |
| 133 | * |
| 134 | * @param array $input |
| 135 | * @return array |
| 136 | */ |
| 137 | private function validate_licenses( $input ) { |
| 138 | // check cap |
| 139 | if ( ! current_user_can( apply_filters( 'rl_lightbox_settings_capability', Responsive_Lightbox()->options['capabilities']['active'] ? 'edit_lightbox_settings' : 'manage_options' ) ) ) |
| 140 | return $input; |
| 141 | |
| 142 | // check option page |
| 143 | $option_page = isset( $_POST['option_page'] ) ? sanitize_key( $_POST['option_page'] ) : ''; |
| 144 | |
| 145 | // check page |
| 146 | if ( ! $option_page ) |
| 147 | return $input; |
| 148 | |
| 149 | $rl_licenses = []; |
| 150 | |
| 151 | if ( isset( $_POST['responsive_lightbox_licenses'] ) && is_array( $_POST['responsive_lightbox_licenses'] ) && ! empty( $_POST['responsive_lightbox_licenses'] ) ) { |
| 152 | foreach ( $_POST['responsive_lightbox_licenses'] as $extension => $data ) { |
| 153 | $ext = sanitize_key( $extension ); |
| 154 | |
| 155 | if ( is_array( $data ) && ! empty( $data['license'] ) ) |
| 156 | $rl_licenses[$ext]['license'] = preg_replace( '/[^a-z0-9]/i', '', $data['license'] ); |
| 157 | else |
| 158 | $rl_licenses[$ext]['license'] = ''; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // check data |
| 163 | if ( ! $rl_licenses ) |
| 164 | return $input; |
| 165 | |
| 166 | // get extension licenses |
| 167 | $extensions = apply_filters( 'rl_settings_licenses', [] ); |
| 168 | |
| 169 | if ( empty( $extensions ) ) |
| 170 | return $input; |
| 171 | |
| 172 | // save settings |
| 173 | if ( isset( $_POST['save_responsive_lightbox_licenses'] ) || isset( $_POST['save_rl_licenses'] ) ) { |
| 174 | $licenses = get_option( 'responsive_lightbox_licenses' ); |
| 175 | $licenses = is_array( $licenses ) ? $licenses : []; |
| 176 | $statuses = [ 'updated' => 0, 'error' => 0 ]; |
| 177 | |
| 178 | foreach ( $extensions as $extension ) { |
| 179 | if ( ! isset( $rl_licenses[$extension['id']] ) ) |
| 180 | continue; |
| 181 | |
| 182 | $license = $rl_licenses[$extension['id']]['license']; |
| 183 | $status = ! empty( $licenses ) && ! empty( $licenses[$extension['id']]['status'] ); |
| 184 | |
| 185 | // update license |
| 186 | $input[$extension['id']]['license'] = $license; |
| 187 | |
| 188 | // request data |
| 189 | $request_args = [ |
| 190 | 'action' => 'activate_license', |
| 191 | 'license' => $license, |
| 192 | 'item_name' => $extension['item_name'] |
| 193 | ]; |
| 194 | |
| 195 | // request |
| 196 | $response = $this->license_request( $request_args ); |
| 197 | |
| 198 | // validate request |
| 199 | if ( is_wp_error( $response ) ) { |
| 200 | $input[$extension['id']]['status'] = false; |
| 201 | $statuses['error']++; |
| 202 | } else { |
| 203 | // decode the license data |
| 204 | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 205 | |
| 206 | // assign the data |
| 207 | if ( $license_data->license === 'valid' ) { |
| 208 | $input[$extension['id']]['status'] = true; |
| 209 | |
| 210 | if ( $status === false ) |
| 211 | $statuses['updated']++; |
| 212 | } else { |
| 213 | $input[$extension['id']]['status'] = false; |
| 214 | $statuses['error']++; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // success notice |
| 220 | if ( $statuses['updated'] > 0 ) |
| 221 | add_settings_error( 'rl_licenses_settings', 'license_activated', esc_html( sprintf( _n( '%s license successfully activated.', '%s licenses successfully activated.', (int) $statuses['updated'], 'responsive-lightbox' ), (int) $statuses['updated'] ) ), 'updated' ); |
| 222 | |
| 223 | // failed notice |
| 224 | if ( $statuses['error'] > 0 ) |
| 225 | add_settings_error( 'rl_licenses_settings', 'license_activation_failed', esc_html( sprintf( _n( '%s license activation failed.', '%s licenses activation failed.', (int) $statuses['error'], 'responsive-lightbox' ), (int) $statuses['error'] ) ), 'error' ); |
| 226 | } elseif ( isset( $_POST['reset_responsive_lightbox_licenses'] ) || isset( $_POST['reset_rl_licenses'] ) ) { |
| 227 | $licenses = get_option( 'responsive_lightbox_licenses' ); |
| 228 | $licenses = is_array( $licenses ) ? $licenses : []; |
| 229 | $statuses = [ |
| 230 | 'updated' => 0, |
| 231 | 'error' => 0 |
| 232 | ]; |
| 233 | |
| 234 | foreach ( $extensions as $extension ) { |
| 235 | $license = ! empty( $licenses ) && isset( $licenses[$extension['id']]['license'] ) ? $licenses[$extension['id']]['license'] : ''; |
| 236 | $status = ! empty( $licenses ) && ! empty( $licenses[$extension['id']]['status'] ); |
| 237 | |
| 238 | if ( $status === true || ( $status === false && ! empty( $license ) ) ) { |
| 239 | // request data |
| 240 | $request_args = [ |
| 241 | 'action' => 'deactivate_license', |
| 242 | 'license' => trim( $license ), |
| 243 | 'item_name' => $extension['item_name'] |
| 244 | ]; |
| 245 | |
| 246 | // request |
| 247 | $response = $this->license_request( $request_args ); |
| 248 | |
| 249 | // validate request |
| 250 | if ( is_wp_error( $response ) ) |
| 251 | $statuses['error']++; |
| 252 | else { |
| 253 | // decode the license data |
| 254 | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 255 | |
| 256 | // assign the data |
| 257 | if ( $license_data->license == 'deactivated' ) { |
| 258 | $input[$extension['id']]['license'] = ''; |
| 259 | $input[$extension['id']]['status'] = false; |
| 260 | |
| 261 | $statuses['updated']++; |
| 262 | } else |
| 263 | $statuses['error']++; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // success notice |
| 269 | if ( $statuses['updated'] > 0 ) |
| 270 | add_settings_error( 'rl_licenses_settings', 'license_deactivated', esc_html( sprintf( _n( '%s license successfully deactivated.', '%s licenses successfully deactivated.', (int) $statuses['updated'], 'responsive-lightbox' ), (int) $statuses['updated'] ) ), 'updated' ); |
| 271 | |
| 272 | // failed notice |
| 273 | if ( $statuses['error'] > 0 ) |
| 274 | add_settings_error( 'rl_licenses_settings', 'license_deactivation_failed', esc_html( sprintf( _n( '%s license deactivation failed.', '%s licenses deactivation failed.', (int) $statuses['error'], 'responsive-lightbox' ), (int) $statuses['error'] ) ), 'error' ); |
| 275 | } |
| 276 | |
| 277 | return $input; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * License request function. |
| 282 | * |
| 283 | * @param array $args |
| 284 | * @return mixed |
| 285 | */ |
| 286 | private function license_request( $args ) { |
| 287 | // data to send in our API request |
| 288 | $api_params = [ |
| 289 | 'edd_action' => $args['action'], |
| 290 | 'license' => sanitize_key( $args['license'] ), |
| 291 | 'item_name' => urlencode( $args['item_name'] ), |
| 292 | // 'item_id' => $args['item_id'], |
| 293 | 'url' => home_url(), |
| 294 | 'timeout' => 60, |
| 295 | 'sslverify' => false |
| 296 | ]; |
| 297 | |
| 298 | // call the custom API |
| 299 | $response = wp_remote_get( add_query_arg( $api_params, 'https://www.dfactory.co' ) ); |
| 300 | |
| 301 | return $response; |
| 302 | } |
| 303 | } |
| 304 |