From 9bacfc2ddf370fbb844b37ea925ea76eb54ddc45 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 7 Sep 2018 15:01:25 -0400 Subject: [PATCH] Add script for updates active memberships Correcting their contact role to own entity manager. --- models/admin/ajax/billingFixActiveUsers.php | 119 ++++++++++++++++++++ setup/validActions.php | 1 + 2 files changed, 120 insertions(+) create mode 100644 models/admin/ajax/billingFixActiveUsers.php diff --git a/models/admin/ajax/billingFixActiveUsers.php b/models/admin/ajax/billingFixActiveUsers.php new file mode 100644 index 0000000..4c7c613 --- /dev/null +++ b/models/admin/ajax/billingFixActiveUsers.php @@ -0,0 +1,119 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/billingSupport.php'; +require_once GLM_MEMBERS_CONTACTS_PLUGIN_CLASS_PATH . '/data/dataContacts.php'; +/** + * Steve Note... + * + * You can get to this using the following URL. + * + * {host}/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=setupQueue + * + * You should be able to do this as POST or GET and should be able to add and read additional parameters. + * I added a "mystuff" parameter to the URL above and it does output from the code in the + * modelAction() function below. + * + * To add another model under models/admin/ajax all you need to do is create it and add it to the + * setup/validActions.php file. + * + */ + +// Load Members data abstract +// require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataImages.php'; + +/** + * This class performs the work of handling images passed to it via + * an AJAX call that goes through the WorPress AJAX Handler. + * + */ +class GlmMembersAdmin_ajax_billingFixActiveUsers +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + public function __construct ($wpdb, $config) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + } + + public function modelAction( $actionData = false ) + { + + // echo '
$actionData: ' . print_r( $actionData, true ) . '
'; + $days_after_expired = $this->config['settings']['days_after_expired']; + $current_date = date( 'Y-m-d' ); + $restrictedRole = $this->config['contact_role_numb']['LogInContact']; + // echo '
$restrictedRole: ' . print_r( $restrictedRole, true ) . '
'; + + // Update All expired accounts so they can only be in members only not admin. + // Find all member contacts (by ref_dest) with accounts that are expired. + // With those set their contact_role to 30 (login only) + if ( $days_after_expired = filter_var( $days_after_expired, FILTER_VALIDATE_INT ) ) { + // Grab the member accounts that need to be updated + $members = $this->wpdb->get_results( + "SELECT ref_name,ref_dest + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts + WHERE renewal_date + INTERVAL 1 YEAR + INTERVAL $days_after_expired DAY > '$current_date'", + ARRAY_A + ); + $total = count( $members ); + echo '
$total: ' . print_r( $total, true ) . '
'; + if ( isset( $members ) && $members ) { + foreach ( $members as $member ) { + echo '
$member: ' . print_r( $member, true ) . '
'; + $Contact = new GlmDataContacts( $this->wpdb, $this->config ); + $contact_id = $Contact->getContactIdByRefDest( $member['ref_dest'] ); + $user_id = $Contact->getWpUserId( $contact_id ); + $wpUser = get_user_by( 'id', $user_id ); + if ( $wpUser ) { + echo '
$wpUser-role: ' . print_r( $wpUser->roles, true ) . '
'; + $roles = $wpUser->roles; + if ( $roles[0] != 'glm_members_own_entity_manager' ) { + apply_filters( + 'glm_contact_update_user_role_by_ref_dest', + '', + $member['ref_dest'], + 'glm_members_member_contact', + 'glm_members_own_entity_manager' + ); + } + } + } + } + + } + + } +} diff --git a/setup/validActions.php b/setup/validActions.php index 0e761bc..2c0e1ae 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -69,6 +69,7 @@ $glmMembersBillingAddOnValidActions = array( 'accountsListExport' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'paymentsListExport' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'billingFlagExpiredUsers' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + 'billingFixActiveUsers' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, ), 'management' => array( 'billing' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, -- 2.17.1