Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls wpmudev-sls/ms-pods.php
Last active Oct 22, 2018

Embed
What would you like to do?
Basic integration with Pods framework for user fields
<?php
/**
* Plugin Name: [Membership] - Pods integration
* Plugin URI: https://premium.wpmudev.org/
* Description: Basic integration with Pods framework for user fields
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_MS_Pods' ) ) {
class WPMUDEV_MS_Pods {
private static $_instance = null;
private $pods = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_MS_Pods();
}
return self::$_instance;
}
private function __construct() {
if ( ! function_exists( 'pods' ) || ! function_exists( 'mslib3' ) ) {
return;
}
add_action( 'ms_view_account_profile_before_card', array( $this, 'account_view' ),20 ,2 );
add_action( 'ms_view_frontend_profile_after_fields', array( $this, 'filter_compact_code' ) );
add_action( 'profile_update', array( $this, 'profile_update' ), 999 );
}
private function get_user_pods( $ids_only = true ) {
global $wpdb;
$args = array(
'meta_query' => array(
array(
'key' => 'type',
'value' => 'user'
)
),
'post_type' => '_pods_pod',
'posts_per_page' => -1
);
if ( $ids_only ) {
$args['fields'] = 'ids';
}
return get_posts($args);
}
public function get_pods_fields( $user_id = null, $user_pods = null ) {
if ( is_null( $user_id ) ) {
if ( ! is_user_logged_in() ) {
return array();
}
$user_id = get_current_user_id();
}
elseif( $user_id instanceof WP_User ) {
$user_id = $user_id->ID;
}
else {
$user_id = (int) $user_id;
}
if ( is_null( $user_pods ) || ! is_array( $user_pods ) || empty( $user_pods ) ) {
$user_pods = is_null( $this->pods ) ? $this->get_user_pods() : $this->pods;
}
$fields = array();
foreach ( $user_pods as $key => $pod_id ) {
$args = array(
'post_type' => '_pods_field',
'posts_per_page' => -1,
'post_status' => 'publish',
'post_parent' => $pod_id,
'order' => 'ASC'
);
$field_items = get_posts( $args );
if ( ! empty( $field_items ) ){
$_fields = array();
foreach ( $field_items as $field ) {
$type = get_post_meta( $field->ID, 'type', true );
$value = get_user_meta( $user_id, $field->post_name, true );
$_fields[ "field-{$field->ID}" ] = array(
'id' => $field->ID,
'title' => $field->post_title,
'name' => $field->post_name,
'type' => $type,
'value' => $value
);
}
$fields = array_merge( $fields, $_fields );
}
}
return $fields;
}
public function account_view( $member, $account ) {
$fields = $this->get_pods_fields();
$out = '';
if ( ! empty( $fields ) ) {
$out .= '<table id="wpmudev_ms_pods_fields_table" style="display:block;">';
foreach ( $fields as $field_id => $field ) {
$field = (object) $field;
$value = '';
if ( is_array( $field->value ) ) {
if (
isset( $field->value['post_type'] ) &&
isset( $field->value['post_name'] ) &&
isset( $field->value['guid'] )
)
{
$_field = (object) $field->value;
$value = esc_html( $_field->post_name );
if ( 'attachment' == $field->value['post_type'] ) {
$value = "<img src='{$_field->guid}' width='150px' /> {$value}";
}
}
}
else {
$value = esc_html( $field->value );
}
$out.= "<tr>";
$out .= "<th class='ms-label-title'>{$field->title}:</th>";
$out .= "<td class='ms-label-field'>{$value}</td>";
$out.= "</tr>";
}
$out .= '</table>';
ob_start();
?>
<script type="text/javascript">
(function($){
$(document).ready(function(){
var acc_table = $('.ms-account-wrapper #account-profile table:first'),
pods_table = $('#wpmudev_ms_pods_fields_table'),
pods_table_rows = pods_table.find('tr');
acc_table.append( pods_table_rows );
pods_table.remove();
});
})(jQuery);
</script>
<?php
$out .= ob_get_clean();
}
echo $out;
}
public function filter_compact_code() {
add_filter( 'ms_compact_code',array( $this, 'edit_page_form' ), 20 );
}
public function edit_page_form( $html ) {
if ( ! is_user_logged_in() ) {
return $html;
}
$out = '';
$user = wp_get_current_user();
$pods = $this->get_user_pods( false );
$is_first_pod = true;
if ( empty( $pods ) ) {
return $html;
}
foreach ( $pods as $pod_post ) {
$pod = pods( $pod_post->post_name, $user->ID);
$fields = $this->get_pods_fields( $user->ID, array( $pod_post->post_title ) );
$pod_fields = array();
$form_fields = array();
if ( $is_first_pod ) {
$form_fields = array(
'first_name' => array(
'label' => 'First name',
'type' => 'text',
'options' => array(
'required' => true,
)
),
'last_name' => array(
'label' => 'Last name',
'type' => 'text',
'options' => array(
'required' => true,
),
),
'user_email',
'user_ms_pass' => array(
'label' => 'Password',
'type' => 'password',
'options' => array(
'required' => false
)
),
'user_ms_pass_confirm' => array(
'label' => 'Confirm Password',
'type' => 'password',
'options' => array(
'required' => false
)
)
);
}
if ( ! empty( $fields ) ) {
foreach ( $fields as $field_id => $field ) {
$pod_fields[] = $field['name'];
}
$form_fields = array_merge( $form_fields, $pod_fields );
}
$out .= $pod->form( $form_fields );
}
if ( ! empty( $out ) ) {
ob_start();
?>
<script type="text/javascript">
(function($){
$(document).ready(function(){
var button = $('.pods-submittable .pods-submit-button'),
back_url = '<?php echo esc_url_raw( remove_query_arg( array( 'action', 'success' ) ) ); ?>',
back_button = $('<a />', {
id: 'ms-acc-back-button',
text: 'Back'
});
back_button.attr( 'href', back_url );
$( '.pods-submittable-fields .pods-submit' ).prepend( back_button );
button.on( 'click', function(e){
let form = $(this).closest( '.pods-submittable' ),
num_rex = /[0-9]/,
chr_rex = /[a-z]/,
has_error = false,
error_text = '',
msg = '';
$( '#message, #message-error' ).remove();
// In case the form contains password field, the confirm password must have the same password
if (
form.has( '#pods-form-ui-pods-field-user-ms-pass' ) &&
form.has( '#pods-form-ui-pods-field-user-ms-pass-confirm' )
)
{
let pass = form.find( '#pods-form-ui-pods-field-user-ms-pass' ).val(),
pass_conf = form.find( '#pods-form-ui-pods-field-user-ms-pass-confirm' ).val();
if ( pass != '' || pass_conf != '' ) {
if ( pass.length < 6 ) {
has_error = true;
error_text += '<div>Password needs must be at least 6 characters in length</div>';
}
if ( pass != pass_conf ) {
has_error = true;
error_text += '<div>Both passwords need to match</div>';
}
if( ! num_rex.test( pass ) ) {
has_error = true;
error_text += '<div>Password needs to contain at least one number</div>';
}
if( ! chr_rex.test( pass ) ) {
has_error = true;
error_text += '<div>Password needs to contain at least one letter</div>';
}
if ( has_error ) {
let msg = '<div id="message-error" class="pods-form-front-success" style="background-image:none; background:#FF9494;border-color:#555; color: #444;">' + error_text + '</div>';
form.before( msg );
e.preventDefault();
return false;
}
}
}
});
});
})(jQuery);
</script>
<?php
$out .= ob_get_clean();
}
return $out;
}
function profile_update( $user_id ) {
if ( isset( $_POST['pods_field_first_name'] ) ) {
update_user_meta( $user_id , 'first_name', sanitize_text_field( $_POST['pods_field_first_name'] ) );
update_user_meta( $user_id , 'ms_first_name', sanitize_text_field( $_POST['pods_field_first_name'] ) );
}
if ( isset( $_POST['pods_field_last_name'] ) ) {
update_user_meta( $user_id , 'last_name', sanitize_text_field( $_POST['pods_field_last_name'] ) );
update_user_meta( $user_id , 'ms_last_name', sanitize_text_field( $_POST['pods_field_last_name'] ) );
}
if ( isset( $_POST['pods_field_user_email'] ) ) {
update_user_meta( $user_id, 'ms_email', sanitize_text_field( $_POST['pods_field_user_email'] ) );
update_user_meta( $user_id, 'ms_email', sanitize_text_field( $_POST['pods_field_user_email'] ) );
}
if (
( isset( $_POST['pods_field_user_ms_pass'] ) && ! empty( $_POST['pods_field_user_ms_pass'] ) ) &&
( isset( $_POST['pods_field_user_ms_pass_confirm'] ) && ! empty( $_POST['pods_field_user_ms_pass_confirm'] ) )
)
{
wp_set_password( $_POST['pods_field_user_ms_pass'], $user_id );
}
}
}
if ( ! function_exists( 'wpmudev_ms_pods' ) ) {
function wpmudev_ms_pods(){
return WPMUDEV_MS_Pods::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_ms_pods', 10 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.