From ed29ba4004900cf118ef58e1d3b9e31357a7fd66 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Thu, 8 Sep 2016 00:52:22 +1000 Subject: [PATCH] Add licenses tab to settings --- assets/css/admin.css | 11 + includes/admin/class-sp-admin-settings.php | 7 + .../settings/class-sp-settings-licenses.php | 212 ++++++++++++++++++ 3 files changed, 230 insertions(+) create mode 100644 includes/admin/settings/class-sp-settings-licenses.php diff --git a/assets/css/admin.css b/assets/css/admin.css index 457d0f59..b5747a5b 100644 --- a/assets/css/admin.css +++ b/assets/css/admin.css @@ -856,6 +856,17 @@ table.sp-modules-table .sp-module-unavailable span .dashicons { -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=80)"; } +/* Licenses */ + +.sp-licenses-wrapper { + max-width: 750px; +} + +.sp-licenses-table th, +.sp-licenses-table input { + vertical-align: middle; +} + #debug-report { display: none; margin: 10px 0; diff --git a/includes/admin/class-sp-admin-settings.php b/includes/admin/class-sp-admin-settings.php index 00dc42a5..79f87d8d 100644 --- a/includes/admin/class-sp-admin-settings.php +++ b/includes/admin/class-sp-admin-settings.php @@ -42,6 +42,13 @@ class SP_Admin_Settings { $settings = apply_filters( 'sportspress_get_settings_pages', $settings ); $settings[] = include( 'settings/class-sp-settings-text.php' ); + + if ( + ( ! is_multisite() && current_user_can( 'manage_options' ) ) || + ( is_multisite() && current_user_can( 'manage_network_options' ) ) + ) { + $settings[] = include( 'settings/class-sp-settings-licenses.php' ); + } if ( current_user_can( 'manage_options' ) ) $settings[] = include( 'settings/class-sp-settings-status.php' ); diff --git a/includes/admin/settings/class-sp-settings-licenses.php b/includes/admin/settings/class-sp-settings-licenses.php new file mode 100644 index 00000000..1cf51aab --- /dev/null +++ b/includes/admin/settings/class-sp-settings-licenses.php @@ -0,0 +1,212 @@ +id = 'licenses'; + $this->label = __( 'Licenses', 'sportspress' ); + + $this->licenses = apply_filters( 'sportspress_licenses', array( + 'pro' => array( + 'name' => 'SportsPress Pro', + 'url' => 'https://account.themeboy.com', + ), + )); + + if ( sizeof ( $this->licenses ) <= 1 ) + return; + + add_filter( 'sportspress_settings_tabs_array', array( $this, 'add_settings_page' ), 20 ); + add_action( 'sportspress_settings_' . $this->id, array( $this, 'output' ) ); + add_action( 'sportspress_settings_save_' . $this->id, array( $this, 'save' ) ); + } + + /** + * Output licenses + * + * @access public + * @return void + */ + public function output() { + ?> + +
+ licenses as $id => $license ) { + $key = get_site_option( 'sportspress_' . $id . '_license_key' ); + $key = trim( $key ); + $status = get_site_option( 'sportspress_' . $id . '_license_status', false ); + ?> +
+

+ + + + + + + +
+ + + + + + + + +

+ + +

+ +

+ + +

+ +
+
+ +
+ $value ) { + if ( 'sp_license_activate_' === substr( $name, 0, 20 ) ) { + $this->activate( substr( $name, 20 ) ); + } elseif ( 'sp_license_deactivate_' === substr( $name, 0, 22 ) ) { + $this->deactivate( substr( $name, 22 ) ); + } + } + } + + /** + * Activate license + */ + public function activate( $id ) { + + // return if a license key isn't set + if ( ! isset( $_POST[ 'sp_license_key_' . $id ] ) ) + return; + + // retrieve the license key + $license = trim( $_POST[ 'sp_license_key_' . $id ] ); + + // get the name of the product + $name = $this->licenses[ $id ]['name']; + + // data to send in our API request + $api_params = array( + 'edd_action'=> 'activate_license', + 'license' => $license, + 'item_name' => urlencode( $name ), // the name of our product in EDD + 'url' => home_url() + ); + + // Call the custom API. + $response = wp_remote_post( $this->licenses[ $id ]['url'], array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) ); + + // Make sure the response came back okay + if ( is_wp_error( $response ) ) { + SP_Admin_Settings::add_error( __( 'Sorry, there has been an error.', 'sportspress' ) ); + return false; + } + + // Decode the license data + $license_data = json_decode( wp_remote_retrieve_body( $response ) ); + + // Update license status + update_site_option( 'sportspress_' . $id . '_license_status', $license_data->license ); + + // Update License or display error + if ( 'valid' == $license_data->license ) { + update_site_option( 'sportspress_' . $id . '_license_key', $license ); + SP_Admin_Settings::add_override( __( 'License activated.', 'sportspress' ) ); + } else { + SP_Admin_Settings::add_error( __( 'License invalid.', 'sportspress' ) ); + } + } + + /** + * Deactivate license + */ + public function deactivate( $id ) { + + // return if a license key isn't set + if ( ! isset( $_POST[ 'sp_license_key_' . $id ] ) ) + return; + + // retrieve the license key + $license = trim( $_POST[ 'sp_license_key_' . $id ] ); + + // get the name of the product + $name = $this->licenses[ $id ]['name']; + + // data to send in our API request + $api_params = array( + 'edd_action'=> 'deactivate_license', + 'license' => $license, + 'item_name' => urlencode( $name ), // the name of our product in EDD + 'url' => home_url() + ); + + // Call the custom API. + $response = wp_remote_post( $this->licenses[ $id ]['url'], array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) ); + + // make sure the response came back okay + if ( is_wp_error( $response ) ) { + SP_Admin_Settings::add_error( __( 'Sorry, there has been an error.', 'sportspress' ) ); + return false; + } + + // decode the license data + $license_data = json_decode( wp_remote_retrieve_body( $response ) ); + + // $license_data->license will be either "deactivated" or "failed" + if ( $license_data->license == 'deactivated' ) { + delete_site_option( 'sportspress_' . $id . '_license_status' ); + SP_Admin_Settings::add_override( __( 'License deactivated.', 'sportspress' ) ); + } else { + SP_Admin_Settings::add_error( __( 'Sorry, there has been an error.', 'sportspress' ) ); + } + } +} + +endif; + +return new SP_Settings_Licenses();