Update membership.php

This commit is contained in:
Hoang Huu 2019-09-23 17:48:24 +07:00
parent 76dcd7ae25
commit d4f3c23691

View File

@ -19,6 +19,7 @@ if ( ! defined( 'ABSPATH' ) ) {
if ( defined( 'OpalMembership' ) ) {
return;
}
/**
* @class OpalEstate_Membership: as vendor class is using for processing logic with update/set permission for user submitting property.
*
@ -33,76 +34,76 @@ class OpalEstate_Membership{
if ( get_current_user_id() ) {
/* estate */
add_action( 'show_admin_bar' , array( __CLASS__, 'hide_admin_toolbar' ) ); // hide admin toolbar
add_action( 'show_admin_bar', [ __CLASS__, 'hide_admin_toolbar' ] ); // hide admin toolbar
/* cmb2 meta box hook membership information */
add_filter( 'opalmembership_postype_membership_metaboxes_fields', array( __CLASS__, 'metabox' ), 10 );
add_filter( 'opalmembership_postype_membership_metaboxes_fields', [ __CLASS__, 'metabox' ], 10 );
/* add user agent role after insert new user member */
// add_filter( 'opalmembership_create_user_data' , array( __CLASS__, 'add_user_role_data' ), 10 );
// add_action( 'opalmembership_create_new_user_successfully', array( __CLASS__, 'trigger_create_user_agent' ), 10, 3 );
/**
* save user meta when save user agent post type
*/
add_action( 'cmb2_save_post_fields', array( __CLASS__, 'trigger_update_user_meta' ), 10, 4 );
add_action( 'profile_update', array( __CLASS__, 'trigger_update_agent_meta' ), 10, 2 );
add_action( 'cmb2_save_post_fields', [ __CLASS__, 'trigger_update_user_meta' ], 10, 4 );
add_action( 'profile_update', [ __CLASS__, 'trigger_update_agent_meta' ], 10, 2 );
/**
* Call Hook after updated membership information in user data.
*/
add_action( 'opalmembership_after_update_user_membership' , array( __CLASS__,'on_set_user_update_membership') , 10, 3 );
add_action( 'opalmembership_after_update_user_membership', [ __CLASS__, 'on_set_user_update_membership' ], 10, 3 );
/**
* HOOK TO My Properties Page Set action to check when user set property as featured.
*/
add_filter( 'opalestate_set_feature_property_checked' , array( __CLASS__,'feature_property_checked') );
add_action( 'opalestate_toggle_featured_property_before' , array( __CLASS__,'update_featured_remaining_listing'), 10, 2 );
add_filter( 'opalestate_set_feature_property_checked', [ __CLASS__, 'feature_property_checked' ] );
add_action( 'opalestate_toggle_featured_property_before', [ __CLASS__, 'update_featured_remaining_listing' ], 10, 2 );
/**
* HOOK to Submssion Page: Check permission before submitting
*/
// check validation before
add_action( 'opalestate_process_submission_before' , array( __CLASS__, 'check_membership_validation' ), 1 );
add_action( 'opalestate_process_submission_before', [ __CLASS__, 'check_membership_validation' ], 1 );
add_action( 'opalestate_submission_form_before' , array( __CLASS__ ,'show_membership_warning'), 9 );
add_action( 'opalestate_submission_form_before' , array( __CLASS__, 'check_membership_validation_message' ) );
add_action( 'opalestate_submission_form_before', [ __CLASS__, 'show_membership_warning' ], 9 );
add_action( 'opalestate_submission_form_before', [ __CLASS__, 'check_membership_validation_message' ] );
add_action( 'opalestate_process_edit_submission_before' , array( __CLASS__, 'check_edit_post' ) );
add_action( 'opalestate_process_add_submission_before' , array( __CLASS__, 'check_add_post' ) );
add_action( 'opalestate_process_edit_submission_before', [ __CLASS__, 'check_edit_post' ] );
add_action( 'opalestate_process_add_submission_before', [ __CLASS__, 'check_add_post' ] );
/// check before uploading image
add_action( 'opalestate_before_process_ajax_upload_file' , array( __CLASS__, 'check_ajax_upload' ) );
add_action( 'opalestate_process_submission_after' , array( __CLASS__, 'update_remainng_listing' ) , 10, 3 );
add_action( 'opalestate_before_process_ajax_upload_file', [ __CLASS__, 'check_ajax_upload' ] );
add_action( 'opalestate_process_submission_after', [ __CLASS__, 'update_remainng_listing' ], 10, 3 );
/**
* HOOK to user management Menu
*/
add_filter( 'opalestate_management_user_menu' , array( __CLASS__, 'membership_menu' ) );
add_filter( 'opalestate_management_user_menu', [ __CLASS__, 'membership_menu' ] );
/**
* Hook to widget to show addition about current package.
*/
add_action( 'opalmembership_current_package_summary_after' , array( __CLASS__, 'render_membership_summary' ), 10, 2 );
add_action( 'opalmembership_current_package_summary_after', [ __CLASS__, 'render_membership_summary' ], 10, 2 );
/**
* Add 'opalesate_agent' role to get_users member data
*/
// add_action( 'opalmembership_member_table_arguments', array( __CLASS__, 'member_table_get_user_arguments' ) );
// show in membership dashboard
add_action( 'opalmembership_dashboard_container_before' , array( __CLASS__, 'check_membership_validation_message' ) );
add_action( 'opalmembership_dashboard_container_before', [ __CLASS__, 'check_membership_validation_message' ] );
// included logic functions
require_once( 'free-package.php' );
require_once( 'functions.php' );
add_action( 'opalmembership_current_package_summary_after' , array( __CLASS__, 'render_membership_summary' ), 10, 2 );
add_action( 'opalmembership_current_package_summary_after', [ __CLASS__, 'render_membership_summary' ], 10, 2 );
add_action( 'cmb2_admin_init', array( __CLASS__, 'register_user_package_metabox') );
add_action( 'cmb2_admin_init', [ __CLASS__, 'register_user_package_metabox' ] );
add_action( 'profile_update' , array( __CLASS__, 'on_update_user' ), 10, 1 );
add_action( 'profile_update', [ __CLASS__, 'on_update_user' ], 10, 1 );
}
/**
* Hook to loop of package membership
*/
add_action( 'opalmembership_content_single_before' , array( __CLASS__, 'render_membership_pricing_box' ) );
add_action( 'opalmembership_content_single_before', [ __CLASS__, 'render_membership_pricing_box' ] );
}
@ -117,15 +118,15 @@ class OpalEstate_Membership{
}
public static function render_membership_free_package() {
echo opalestate_load_template_path( 'parts/membership-free-package', array() );
echo opalestate_load_template_path( 'parts/membership-free-package', [] );
}
public static function render_membership_pricing_box() {
echo opalestate_load_template_path( 'parts/membership-pricing-info', array() );
echo opalestate_load_template_path( 'parts/membership-pricing-info', [] );
}
public static function member_table_get_user_arguments( $args ) {
return array_merge_recursive( $args, array( 'role__in' => array( 'opalestate_agent' ) ) );
return array_merge_recursive( $args, [ 'role__in' => [ 'opalestate_agent' ] ] );
}
@ -135,22 +136,22 @@ class OpalEstate_Membership{
wp_get_current_user();
$user_id = $current_user->ID;
$args = array(
$args = [
'post_type' => 'opalestate_agent',
'author' => $user_id,
'post_status' => 'any'
);
'post_status' => 'any',
];
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
$prop = array(
$prop = [
'ID' => $post->ID,
'post_type' => 'opalestate_agent',
'post_status' => 'expired'
);
'post_status' => 'expired',
];
wp_update_post( $prop );
}
@ -177,7 +178,8 @@ class OpalEstate_Membership{
$std = new stdClass();
$std->status = false;
$std->message = esc_html__( 'Could not allow uploading image', 'opalestate-pro' );
echo json_encode( $std ); exit();
echo json_encode( $std );
exit();
}
}
@ -185,12 +187,15 @@ class OpalEstate_Membership{
* hide admin toolbar with user role agent
*/
public static function hide_admin_toolbar( $show ) {
if ( ! is_user_logged_in() ) { return $show; }
if ( ! is_user_logged_in() ) {
return $show;
}
$user = wp_get_current_user();
if ( opalestate_get_option( 'hide_toolbar' ) && $user->roles && in_array( 'opalestate_agent', $user->roles ) ) {
return false;
}
return $show;
}
@ -199,43 +204,45 @@ class OpalEstate_Membership{
*/
public static function metabox( $fields ) {
if ( ! defined( 'OPALMEMBERSHIP_PACKAGES_PREFIX' ) ) return $fields;
if ( ! defined( 'OPALMEMBERSHIP_PACKAGES_PREFIX' ) ) {
return $fields;
}
$prefix = OPALMEMBERSHIP_PACKAGES_PREFIX;
$fields[] = array(
'name' => esc_html__( 'Number Of Properties', 'opalestate-pro' ),
'id' => $prefix . 'package_listings',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
'min' => 0
),
'std' => '1',
'description' => esc_html__( 'Number of properties with this package. If not set it will be unlimited.', 'opalestate-pro' )
);
$fields[] = array(
'name' => esc_html__( 'Number Of Featured Properties', 'opalestate-pro' ),
'id' => $prefix . 'package_featured_listings',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
'min' => 0
),
'std' => '1',
'description' => esc_html__( 'Number of properties can make featured with this package.', 'opalestate-pro' )
);
$fields[] = array(
'name' => esc_html__( ' Unlimited listings ?', 'opalestate-pro' ),
$fields[] = [
'name' => esc_html__( 'Limited listings?', 'opalestate-pro' ),
'id' => $prefix . 'unlimited_listings',
'type' => 'checkbox',
'std' => '1',
'description' => esc_html__( 'No, it is not unlimited, If not set it will be unlimited. Number of properties can make featured with this package.', 'opalestate-pro' )
);
'description' => esc_html__( 'Check if set limited listings. Default: Unlimited listings. Notice: Enter Number Of Properties when set limited listings.', 'opalestate-pro' ),
];
$fields[] = [
'name' => esc_html__( 'Number Of Properties', 'opalestate-pro' ),
'id' => $prefix . 'package_listings',
'type' => 'text',
'attributes' => [
'type' => 'number',
'pattern' => '\d*',
'min' => 0,
],
'std' => '1',
'description' => esc_html__( 'Number of properties with this package. If not set it will be unlimited.', 'opalestate-pro' ),
];
$fields[] = [
'name' => esc_html__( 'Number Of Featured Properties', 'opalestate-pro' ),
'id' => $prefix . 'package_featured_listings',
'type' => 'text',
'attributes' => [
'type' => 'number',
'pattern' => '\d*',
'min' => 0,
],
'std' => '1',
'description' => esc_html__( 'Number of properties can make featured with this package.', 'opalestate-pro' ),
];
return $fields;
}
@ -246,27 +253,28 @@ class OpalEstate_Membership{
public static function membership_menu( $menu ) {
if ( function_exists( "opalmembership_get_dashdoard_page_uri" ) ) {
global $opalmembership_options;
$menu['membership'] = array(
$menu['membership'] = [
'icon' => 'fa fa-user',
'link' => opalmembership_get_dashdoard_page_uri(),
'title' => esc_html__( 'My Membership', 'opalestate-pro' ),
'id' => isset( $opalmembership_options['dashboard_page'] ) ? $opalmembership_options['dashboard_page'] : 0
);
'id' => isset( $opalmembership_options['dashboard_page'] ) ? $opalmembership_options['dashboard_page'] : 0,
];
$menu['membership_history'] = array(
$menu['membership_history'] = [
'icon' => 'fa fa-user',
'link' => opalmembership_get_payment_history_page_uri(),
'title' => esc_html__( 'My Invoices', 'opalestate-pro' ),
'id' => isset( $opalmembership_options['dashboard_page'] ) ? $opalmembership_options['dashboard_page'] : 0
);
'id' => isset( $opalmembership_options['dashboard_page'] ) ? $opalmembership_options['dashboard_page'] : 0,
];
$menu['packages'] = array(
$menu['packages'] = [
'icon' => 'fa fa-certificate',
'link' => opalmembership_get_membership_page_uri(),
'title' => esc_html__( 'Renew membership', 'opalestate-pro' ),
'id' => isset( $opalmembership_options['dashboard_page'] ) ? $opalmembership_options['dashboard_page'] : 0
);
'id' => isset( $opalmembership_options['dashboard_page'] ) ? $opalmembership_options['dashboard_page'] : 0,
];
}
return $menu;
}
@ -275,6 +283,7 @@ class OpalEstate_Membership{
*/
public static function add_user_role_data( $cred ) {
$cred['role'] = 'opalestate_agent';
return $cred;
}
@ -283,11 +292,11 @@ class OpalEstate_Membership{
*/
public static function trigger_create_user_agent( $user_id, $user_data, $cred ) {
// create new post(opalestate_agent)
$agent_id = opalesate_insert_user_agent( array(
$agent_id = opalesate_insert_user_agent( [
'first_name' => $cred['first_name'],
'last_name' => $cred['last_name'],
'email' => $cred['user_email']
) );
'email' => $cred['user_email'],
] );
update_post_meta( $agent_id, OPALESTATE_AGENT_PREFIX . 'user_id', $user_id );
}
@ -297,10 +306,14 @@ class OpalEstate_Membership{
*/
public static function trigger_update_user_meta( $agent_id, $cmb_id, $updated, $cmb2 ) {
if ( $cmb_id !== 'opalestate_agt_info' || empty( $cmb2->data_to_save ) ) return;
if ( $cmb_id !== 'opalestate_agt_info' || empty( $cmb2->data_to_save ) ) {
return;
}
$user_id = get_post_meta( $agent_id, OPALESTATE_AGENT_PREFIX . 'user_id', true );
if ( ! $user_id ) return;
if ( ! $user_id ) {
return;
}
foreach ( $cmb2->data_to_save as $name => $value ) {
@ -314,12 +327,16 @@ class OpalEstate_Membership{
* trigger save agent post meta
*/
public static function trigger_update_agent_meta( $user_id, $old_user_meta ) {
if ( empty( $_POST ) ) return;
if ( empty( $_POST ) ) {
return;
}
global $wpdb;
$sql = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_value = %d AND meta_key = %s", $user_id, OPALESTATE_AGENT_PREFIX . 'user_id' );
$agent_id = $wpdb->get_var( $sql );
if ( ! $agent_id ) return;
if ( ! $agent_id ) {
return;
}
foreach ( $_POST as $name => $value ) {
if ( strpos( $name, OPALESTATE_USER_PROFILE_PREFIX ) === 0 ) {
update_post_meta( $agent_id, OPALESTATE_AGENT_PREFIX . substr( $name, strlen( OPALESTATE_USER_PROFILE_PREFIX ) ), $value );
@ -368,7 +385,6 @@ class OpalEstate_Membership{
$new_featured_listings = 0;
}
if ( $pack_unlimited_listings == 1 ) {
$new_listings = -1;
}
@ -393,6 +409,7 @@ class OpalEstate_Membership{
if ( isset( $_POST['property_id'] ) ) {
return opalesate_get_user_featured_remaining_listing( $user_id );
}
return false;
}
@ -422,11 +439,12 @@ class OpalEstate_Membership{
return opalestate_output_msg_json( true,
__( 'Your membership package is expired or Your package has 0 left listing, please upgrade now.', 'opalestate-pro' ),
array(
[
'heading' => esc_html__( 'Submission Information', 'opalestate-pro' ),
'redirect' => opalmembership_get_membership_page_uri(array('warning=1'))
)) ;
'redirect' => opalmembership_get_membership_page_uri( [ 'warning=1' ] ),
] );
}
return;
}
@ -448,7 +466,8 @@ class OpalEstate_Membership{
$has = opalesate_check_has_add_listing( $user_id );
if ( $has == false ) {
wp_redirect( opalmembership_get_membership_page_uri( array('warning=2') ) ); exit;
wp_redirect( opalmembership_get_membership_page_uri( [ 'warning=2' ] ) );
exit;
}
}
@ -464,7 +483,7 @@ class OpalEstate_Membership{
return;
}
echo opalestate_load_template_path( 'parts/membership-warning', array('user_id' => $user_id) );
echo opalestate_load_template_path( 'parts/membership-warning', [ 'user_id' => $user_id ] );
}
@ -506,6 +525,7 @@ class OpalEstate_Membership{
public static function membership_username_actions( $actions, $items ) {
$actions['edit'] = sprintf( '<a href="%s">%s</a>', get_edit_post_link( 0 ), esc_html__( 'Edit', 'opalestate-pro' ) );
return $actions;
}
@ -519,13 +539,13 @@ class OpalEstate_Membership{
}
$prefix = OPALMEMBERSHIP_USER_PREFIX_;
$fields = array();
$fields = [];
foreach ( $fields as $field ) {
$cmb_user->add_field( $field );
}
$fields = array();
$fields = [];
$date = null;
$current_user = wp_get_current_user();
@ -541,71 +561,71 @@ class OpalEstate_Membership{
/**
* Metabox for the user profile screen
*/
$cmb_user = new_cmb2_box( array(
$cmb_user = new_cmb2_box( [
'id' => $prefix . 'package',
'title' => esc_html__( 'Membership Package', 'opalestate-pro' ), // Doesn't output for user boxes
'object_types' => array( 'user' ), // Tells CMB2 to use user_meta vs post_meta
'object_types' => [ 'user' ], // Tells CMB2 to use user_meta vs post_meta
'show_names' => true,
'new_user_section' => 'add-new-user', // where form will show on new user page. 'add-existing-user' is only other valid option.
) );
] );
$fields[] = array(
$fields[] = [
'name' => esc_html__( 'Package', 'opalestate-pro' ),
'id' => $prefix . 'package_id',
'type' => 'text',
'attributes' => array(
'attributes' => [
'type' => 'number',
'pattern' => '\d*',
'min' => 0
),
'min' => 0,
],
'std' => '1',
'description' => esc_html__( 'Set package ID with -1 as free package.', 'opalestate-pro' ),
'before_row' => '<hr><h3> '.__( 'Membership Information', 'opalestate-pro' ).' </h3>'
);
'before_row' => '<hr><h3> ' . __( 'Membership Information', 'opalestate-pro' ) . ' </h3>',
];
$fields[] = array(
$fields[] = [
'name' => esc_html__( 'Number Of Properties', 'opalestate-pro' ),
'id' => $prefix . 'package_listings',
'type' => 'text',
'attributes' => array(
'attributes' => [
'type' => 'number',
'pattern' => '\d*',
'min' => 0
),
'min' => 0,
],
'std' => '1',
'description' => esc_html__( 'Number of properties with this package. If not set it will be unlimited.', 'opalestate-pro' )
);
'description' => esc_html__( 'Number of properties with this package. If not set it will be unlimited.', 'opalestate-pro' ),
];
$fields[] = array(
$fields[] = [
'name' => esc_html__( 'Number Of Featured Properties', 'opalestate-pro' ),
'id' => $prefix . 'package_featured_listings',
'type' => 'text',
'attributes' => array(
'attributes' => [
'type' => 'number',
'pattern' => '\d*',
'min' => 0
),
'min' => 0,
],
'std' => '1',
'description' => esc_html__( 'Number of properties can make featured with this package.', 'opalestate-pro' )
);
'description' => esc_html__( 'Number of properties can make featured with this package.', 'opalestate-pro' ),
];
$fields[] = array(
$fields[] = [
'name' => esc_html__( 'Expired', 'opalestate-pro' ),
'id' => $prefix . 'package_expired_date',
'type' => 'text_date',
'default' => $date,
'std' => '1',
'description' => esc_html__( 'Show expired time in double format.', 'opalestate-pro' )
);
'description' => esc_html__( 'Show expired time in double format.', 'opalestate-pro' ),
];
$fields[] = array(
$fields[] = [
'name' => esc_html__( 'Expired', 'opalestate-pro' ),
'id' => $prefix . 'package_expired',
'type' => 'text',
'std' => '1',
'description' => esc_html__( 'Show expired time in double format.', 'opalestate-pro' )
);
'description' => esc_html__( 'Show expired time in double format.', 'opalestate-pro' ),
];
foreach ( $fields as $field ) {