Last active
June 4, 2022 14:58
-
-
Save wpmudev-sls/6bd6f29b85c4856da0a3a678c4e76a50 to your computer and use it in GitHub Desktop.
[Forminator] - Restrict Content Based on Stripe Subscription
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: [Forminator] - Restrict Content Based on Stripe Subscription | |
| * Plugin URI: https://wpmudev.com/ | |
| * Description: Implement helper functions to check by "Form ID" and "email" if a user has an active stripe subscription. | |
| * Author: Glauber Silva @ WPMUDEV | |
| * Author URI: https://wpmudev.com/ | |
| * Jira Task: SLS-3589 | |
| * License: GPLv2 or later | |
| */ | |
| defined( 'ABSPATH' ) || exit; | |
| /** | |
| * Below you can see a sample demonstrating how to use the | |
| * forminator_user_has_active_subscription() method to | |
| * check if the logged user has acces to specific pages. | |
| */ | |
| add_action('init', | |
| function(){ | |
| if ( ! is_user_logged_in() || ! class_exists( 'Forminator_API' ) || ! class_exists( 'Forminator_Stripe_Subscription' ) ) { | |
| return false; | |
| } | |
| $user_email = wp_get_current_user()->user_email; | |
| $form_id = 1615; // Set the Forminator Form ID with the "stripe subscription" field here. | |
| /** | |
| * Set the pages, separate by a comma, that just users with an active subscription can access. | |
| */ | |
| $pages_to_check_subscription = array( | |
| '/content-1-slug', | |
| '/content-2-slug', | |
| '/content-3-slug', | |
| ); | |
| global $wp; | |
| if ( is_object( $wp ) ) { | |
| $current_url = home_url( add_query_arg( $wp->query_vars, $wp->request ) ); | |
| } else { | |
| $current_url = home_url( add_query_arg( '', '' ) ); // $request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); | |
| } | |
| foreach ( $pages_to_check_subscription as $page ) { | |
| if ( false !== strpos( $current_url, $page ) ) { | |
| $active_subscription = forminator_user_has_active_subscription( $form_id , $user_email ); | |
| if ( ! $active_subscription ) { | |
| echo '<div style="border: 5px solid red; margin: 10%; word-wrap: break-word; padding:10px;">'; | |
| echo 'You need to have an active subscription to access this content.'; | |
| echo '</div>'; | |
| exit; | |
| } | |
| } | |
| } | |
| } | |
| ); | |
| function forminator_user_has_active_subscription( $form_id, $user_email ) { | |
| $entries = forminator_get_entries_by_form_id_and_meta_value( $form_id , $user_email ); | |
| foreach ( $entries as $entrie ) { | |
| foreach ( $entrie->meta_data as $slug => $data ) { | |
| if ( false !== strpos( $slug, 'stripe') && | |
| isset( $data['value']['subscription_id'] ) && | |
| isset( $data['value']['status'] ) && | |
| 'Active' === $data['value']['status'] ) { | |
| return true; | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| function forminator_get_entries_by_form_id_and_meta_value( $form_id, $meta_value ) { | |
| if ( ! class_exists( 'Forminator_Form_Entry_Model' ) ) { | |
| return false; | |
| } | |
| global $wpdb; | |
| $entries = array(); | |
| $table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY ); | |
| $entries_meta_table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY_META ); | |
| $sql = "SELECT entries.entry_id FROM {$table_name} entries | |
| INNER JOIN {$entries_meta_table_name} AS metas | |
| ON (entries.entry_id = metas.entry_id) | |
| WHERE entries.is_spam = 0 AND entries.form_id = %d AND metas.meta_value = %s"; | |
| $results = $wpdb->get_results( $wpdb->prepare( $sql, $form_id, $meta_value ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| if ( ! empty( $results ) ) { | |
| foreach ( $results as $result ) { | |
| $entries[] = new Forminator_Form_Entry_Model( $result->entry_id ); | |
| } | |
| } | |
| return $entries; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment