args = $args; $this->product_id = sanitize_key( $args['name'] ); if ( isset( $args['bundled'] ) ) { $this->add_bundled_product( $args['bundled'] ); } // Instantiate the updater. if ( null === $this->updater ) { $this->updater = new Fusion_Updater( $this ); } } /** * Adds a product to the array of bundled products. * * @access private * @since 1.0.0 * @param array $bundled An array o bundled products. */ private function add_bundled_product( $bundled ) { $bundled = (array) $bundled; foreach ( $bundled as $product_slug => $product_name ) { $product = sanitize_key( $product_name ); if ( ! isset( self::$bundled[ $product ] ) ) { self::$bundled[ $product ] = $this->args['name']; } } } /** * Gets bundled products array. * * @access public * @since 1.0.0 * @return array */ public function get_bundled() { return self::$bundled; } /** * Returns the current token. * * @access public * @since 1.0.0 * @param string $product_id The product-ID. * @return string The current token. */ public function get_token( $product_id = '' ) { if ( '' === $product_id ) { $product_id = $this->product_id; } $option = get_option( $this->option_name ); if ( ! empty( $option ) && is_array( $option ) && isset( $option[ $product_id ] ) && isset( $option[ $product_id ]['token'] ) ) { return $option[ $product_id ]['token']; } return ''; } /** * Gets the arguments. * * @access public * @since 1.0.0 * @return array */ public function get_args() { return $this->args; } /** * Envato API class. * * @access public * @since 1.0.0 * @return Fusion_Envato_API */ public function envato_api() { if ( null === $this->envato_api ) { $this->envato_api = new Fusion_Envato_API( $this ); } return $this->envato_api; } /** * Checks if the product is part of the themes or plugins * purchased by the user belonging to the token. * * @access public * @since 1.0.0 */ public function check_registration() { // Sanity check. No need to do anything if we're not saving the form. if ( isset( $_POST['fusion_registration'] ) && isset( $_POST['fusion_registration'][ $this->product_id ] ) && isset( $_POST['_wpnonce'] ) ) { // Security check. check_admin_referer( $this->option_name . '_' . $this->product_id ); // Get the saved products. $saved_products = get_option( $this->option_name, array() ); // Get the fusion_registered option. $registered = get_option( 'fusion_registered' ); // The new token. $token = sanitize_text_field( wp_unslash( $_POST['fusion_registration'][ $this->product_id ]['token'] ) ); $token = trim( $token ); // If token field is empty, copy is not registered. $registered[ $this->product_id ] = false; if ( ! empty( $token ) && 32 === strlen( $token ) ) { // Check if new token is valid. $registered[ $this->product_id ] = $this->product_exists( $token ); } // Update saved product option. $saved_products[ $this->product_id ] = array( 'token' => $token, ); update_option( $this->option_name, $saved_products ); // Check the token scopes and update the option accordingly. $registered['scopes'][ $this->product_id ] = $this->envato_api()->get_token_scopes( $saved_products[ $this->product_id ]['token'] ); // Update the 'fusion_registered' option. update_option( 'fusion_registered', $registered ); } } /** * Checks if the product is part of the themes or plugins * purchased by the user belonging to the token. * * @access private * @since 1.0.0 * @param string $token A token to check. * @param int $page The page number if one is necessary. * @return bool */ private function product_exists( $token = '', $page = '' ) { // Set the new token for the API call. if ( '' !== $token ) { $this->envato_api()->set_token( $token ); } if ( 'theme' === $this->args['type'] ) { $products = $this->envato_api()->themes( array(), $page ); } elseif ( 'plugin' === $this->args['type'] ) { $products = $this->envato_api()->plugins( array(), $page ); } // If a WP Error object is returned we need to check if API is down. if ( is_wp_error( $products ) ) { // 401 ( unauthorized ) and 403 ( forbidden ) mean the token is invalid, apart from that Envato API is down. if ( 401 !== $products->get_error_code() && 403 !== $products->get_error_code() && '' !== $products->get_error_message() ) { set_site_transient( 'fusion_envato_api_down', true, 600 ); } return false; } // Check iv product is part of the purchased themes/plugins. foreach ( $products as $product ) { if ( isset( $product['name'] ) ) { if ( $this->args['name'] === $product['name'] ) { return true; } } } if ( 100 === count( $products ) ) { $page = ( ! $page ) ? 2 : $page + 1; return $this->product_exists( '', $page ); } return false; } /** * Has user associated with current token purchased this product? * * @access public * @since 1.0.0 * @return bool */ public function is_registered() { // Get the option. $registered = get_option( 'fusion_registered' ); // Is the product registered? if ( isset( $registered[ $this->product_id ] ) && true === $registered[ $this->product_id ] ) { return true; } // Is the Envato API down? if ( get_site_transient( 'fusion_envato_api_down' ) ) { return true; } // Fallback to false. return false; } /** * Prints the registration form. * * @access public * @since 1.0.0 * @return void */ public function the_form() { // Print styles. $this->form_styles(); // No need to display anything if this is a bundled product. // TODO: Please note another implementation a few lines below. if ( isset( self::$bundled[ $this->product_id ] ) ) { return; } // Check registration. $this->check_registration(); // Get the current token. $token = $this->get_token( $this->product_id ); // Is the product registered? $is_registered = $this->is_registered( $this->product_id ); // Get the fusion_registered option. $registered = get_option( 'fusion_registered' ); if ( ! isset( $registered['scopes'] ) ) { $registered['scopes'] = array(); } ?>
args['name'] ) ); ?>
product_id ] ) || ! is_array( $registered['scopes'] ) ) { $registered['scopes'][ $this->product_id ] = $this->envato_api()->get_token_scopes(); update_option( 'fusion_registered', $registered ); } $scopes_ok = $this->envato_api()->check_token_scopes( $registered['scopes'][ $this->product_id ] ); ?>View Your Envato Account Username, Download Your Purchased Items, List Purchases You\'ve Made, Verify Purchases You\'ve Made.', 'Avada' ); // WPCS: XSS ok. ?>