Opal-Estate-Pro/inc/admin/agency/class-agency.php

170 lines
4.2 KiB
PHP
Raw Normal View History

2019-10-23 10:43:40 +02:00
<?php
2019-09-10 06:27:33 +02:00
if ( ! defined( 'ABSPATH' ) ) {
2019-10-23 10:43:40 +02:00
exit; // Exit if accessed directly
2019-09-10 06:27:33 +02:00
}
2019-10-23 10:43:40 +02:00
class Opalestate_Admin_Agency {
2019-09-10 06:27:33 +02:00
/**
2019-10-23 10:43:40 +02:00
* Opalestate_Admin_Agency constructor.
2019-09-10 06:27:33 +02:00
*/
2019-10-23 10:43:40 +02:00
public function __construct() {
add_action( 'cmb2_admin_init', [ $this, 'metaboxes' ] );
add_action( 'save_post', [ $this, 'on_save_post' ], 13, 2 );
add_action( 'user_register', [ $this, 'on_update_user' ], 10, 1 );
add_action( 'profile_update', [ $this, 'on_update_user' ], 10, 1 );
2019-09-10 06:27:33 +02:00
}
/**
* Update relationship post and user data, auto update meta information from post to user
*/
2019-10-23 10:43:40 +02:00
public function on_save_post( $post_id ) {
$post_type = get_post_type( $post_id );
if ( $post_type == 'opalestate_agency' ) {
if ( isset( $_POST[ OPALESTATE_AGENCY_PREFIX . 'user_id' ] ) && $_POST[ OPALESTATE_AGENCY_PREFIX . 'user_id' ] ) {
$user_id = absint( $_POST[ OPALESTATE_AGENCY_PREFIX . 'user_id' ] );
2019-09-10 06:27:33 +02:00
update_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', $post_id );
2019-10-23 10:43:40 +02:00
OpalEstate_Agency::update_user_data( $user_id );
2019-09-10 06:27:33 +02:00
}
}
}
/**
* Auto update meta information to post from user data updated or created
*/
public function on_update_user() {
2019-10-23 10:43:40 +02:00
if ( isset( $_POST['user_id'] ) && (int) $_POST['user_id'] && isset( $_POST['role'] ) ) {
if ( $_POST['role'] == 'opalestate_agency' ) {
$user_id = absint( $_POST['user_id'] );
static::update_user_metas( $user_id );
2019-09-10 06:27:33 +02:00
2019-10-23 10:43:40 +02:00
$related_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true );
$post = get_post( $related_id );
if ( isset( $post->ID ) && $post->ID ) {
2019-09-10 06:27:33 +02:00
OpalEstate_Agency::update_data_from_user( $related_id );
}
}
}
}
2019-10-23 10:43:40 +02:00
/**
* Update some user metas.
*
* @param int $related_id Post ID.
*/
public static function update_user_metas( $user_id ) {
$terms = [
'location',
'state',
'city',
];
foreach ( $terms as $term ) {
if ( isset( $_POST[ OPALESTATE_USER_PROFILE_PREFIX . $term ] ) ) {
update_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . $term, $_POST[ OPALESTATE_USER_PROFILE_PREFIX . $term ] );
}
}
}
2019-09-10 06:27:33 +02:00
/**
*
*/
2019-10-23 10:43:40 +02:00
public function metaboxes_fields( $prefix = '' ) {
2019-09-10 06:27:33 +02:00
if ( ! $prefix ) {
$prefix = OPALESTATE_AGENCY_PREFIX;
}
2019-10-23 10:43:40 +02:00
$fields = [
[
'name' => esc_html__( 'Gallery', 'opalestate-pro' ),
'desc' => esc_html__( 'Select one, to add new you create in location of estate panel', 'opalestate-pro' ),
'id' => $prefix . "gallery",
'type' => 'file_list',
],
[
2019-09-25 04:08:16 +02:00
'name' => esc_html__( 'Slogan', 'opalestate-pro' ),
2019-09-10 06:27:33 +02:00
'id' => "{$prefix}slogan",
2019-10-23 10:43:40 +02:00
'type' => 'text',
],
];
2019-09-10 06:27:33 +02:00
2019-10-23 10:43:40 +02:00
return apply_filters( 'opalestate_postype_agency_metaboxes_fields', $fields );
2019-09-10 06:27:33 +02:00
}
/**
*
*/
2019-10-23 10:43:40 +02:00
public function metaboxes() {
global $pagenow;
2019-09-10 06:27:33 +02:00
2019-10-23 10:43:40 +02:00
if ( ( $pagenow == 'post.php' || $pagenow == 'post-new.php' ) ) {
2019-09-10 06:27:33 +02:00
$prefix = OPALESTATE_AGENCY_PREFIX;
2019-10-23 10:43:40 +02:00
$metabox = new Opalestate_Agency_MetaBox();
$fields = $this->metaboxes_fields();
$fields = array_merge_recursive( $fields,
$metabox->get_office_fields( $prefix ),
2019-09-10 06:27:33 +02:00
$metabox->get_address_fields( $prefix )
);
2019-10-23 10:43:40 +02:00
$box_options = [
2019-09-10 06:27:33 +02:00
'id' => $prefix . 'edit',
'title' => esc_html__( 'Metabox', 'opalestate-pro' ),
2019-10-23 10:43:40 +02:00
'object_types' => [ 'opalestate_agency' ],
2019-09-10 06:27:33 +02:00
'show_names' => true,
2019-10-23 10:43:40 +02:00
];
2019-09-10 06:27:33 +02:00
// Setup meta box
$cmb = new_cmb2_box( $box_options );
// Setting tabs
2019-10-23 10:43:40 +02:00
$tabs_setting = [
2019-09-10 06:27:33 +02:00
'config' => $box_options,
'layout' => 'vertical', // Default : horizontal
2019-10-23 10:43:40 +02:00
'tabs' => [],
];
2019-09-10 06:27:33 +02:00
2019-10-23 10:43:40 +02:00
$tabs_setting['tabs'][] = [
2019-09-10 06:27:33 +02:00
'id' => 'p-general',
2019-10-23 10:43:40 +02:00
'icon' => 'dashicons-admin-home',
2019-09-10 06:27:33 +02:00
'title' => esc_html__( 'General', 'opalestate-pro' ),
2019-10-23 10:43:40 +02:00
'fields' => $fields,
];
2019-09-10 06:27:33 +02:00
2019-10-23 10:43:40 +02:00
$tabs_setting['tabs'][] = [
2019-09-10 06:27:33 +02:00
'id' => 'p-socials',
2019-10-23 10:43:40 +02:00
'icon' => 'dashicons-share',
2019-09-10 06:27:33 +02:00
'title' => esc_html__( 'Socials', 'opalestate-pro' ),
2019-10-23 10:43:40 +02:00
'fields' => $metabox->get_social_fields( $prefix ),
];
2019-09-10 06:27:33 +02:00
2019-10-23 10:43:40 +02:00
$tabs_setting['tabs'][] = [
2019-09-10 06:27:33 +02:00
'id' => 'p-target',
2019-10-23 10:43:40 +02:00
'icon' => 'dashicons-admin-tools',
2019-09-10 06:27:33 +02:00
'title' => esc_html__( 'Team', 'opalestate-pro' ),
2019-10-23 10:43:40 +02:00
'fields' => $metabox->metaboxes_target(),
];
2019-09-10 06:27:33 +02:00
// Set tabs
2019-10-23 10:43:40 +02:00
$cmb->add_field( [
2019-09-10 06:27:33 +02:00
'id' => '__tabs',
'type' => 'tabs',
2019-10-23 10:43:40 +02:00
'tabs' => $tabs_setting,
] );
2019-09-10 06:27:33 +02:00
}
}
}
2019-10-23 10:43:40 +02:00
2019-09-10 06:27:33 +02:00
new Opalestate_Admin_Agency();