Update functions.

This commit is contained in:
Hoang Huu 2019-10-23 17:28:48 +07:00
parent b57e76d12f
commit f8b6371ee5
2 changed files with 55 additions and 34 deletions

@ -52,7 +52,6 @@ class Opalestate_Agent_Front {
* Auto update meta information to post from user data updated or created * Auto update meta information to post from user data updated or created
*/ */
public function init() { public function init() {
add_action( 'opalestate_on_set_role_agent', [ $this, 'on_set_role' ], 1, 9 ); add_action( 'opalestate_on_set_role_agent', [ $this, 'on_set_role' ], 1, 9 );
add_filter( 'opalestate_before_render_profile_agent_form', [ $this, 'render_front_form' ], 2, 2 ); add_filter( 'opalestate_before_render_profile_agent_form', [ $this, 'render_front_form' ], 2, 2 );
add_filter( 'pre_get_posts', [ $this, 'archives_query' ], 1 ); add_filter( 'pre_get_posts', [ $this, 'archives_query' ], 1 );
@ -66,7 +65,10 @@ class Opalestate_Agent_Front {
/** /**
* render_extra_profile_link. * Render extra profile link.
*
* @param $menu
* @return mixed
*/ */
public function render_extra_profile_link( $menu ) { public function render_extra_profile_link( $menu ) {
global $current_user; global $current_user;
@ -133,12 +135,14 @@ class Opalestate_Agent_Front {
} }
} }
/**
* Process upload files.
*
* @param int $post_id Post ID.
*/
private function process_upload_files( $post_id ) { private function process_upload_files( $post_id ) {
// Upload images for featured and gallery images.
//upload images for featured and gallery images
if ( isset( $_FILES ) && ! empty( $_FILES ) ) { if ( isset( $_FILES ) && ! empty( $_FILES ) ) {
///
$fields = [ $fields = [
$this->get_field_name( 'gallery' ), $this->get_field_name( 'gallery' ),
$this->get_field_name( 'avatar_id' ), $this->get_field_name( 'avatar_id' ),
@ -146,11 +150,11 @@ class Opalestate_Agent_Front {
]; ];
foreach ( $_FILES as $key => $value ) { foreach ( $_FILES as $key => $value ) {
// allow processing in fixed collection // Allow processing in fixed collection.
if ( in_array( $key, $fields ) ) { if ( in_array( $key, $fields ) ) {
$ufile = $_FILES[ $key ]; $ufile = $_FILES[ $key ];
/// /////
if ( isset( $ufile['name'] ) && is_array( $ufile['name'] ) ) { if ( isset( $ufile['name'] ) && is_array( $ufile['name'] ) ) {
$output = []; $output = [];
@ -180,16 +184,20 @@ class Opalestate_Agent_Front {
$_key = str_replace( "_id", "", $key ); $_key = str_replace( "_id", "", $key );
$_POST[ $_key ] = $new_atm['url']; $_POST[ $_key ] = $new_atm['url'];
} }
$this->new_attachmenet_ids[ $new_atm['attachment_id'] ] = $new_atm['attachment_id']; $this->new_attachmenet_ids[ $new_atm['attachment_id'] ] = $new_atm['attachment_id'];
} }
} }
} }
} }
} }
} }
/**
* On save front data.
*
* @return false|mixed|string|void
*/
public function on_save_front_data() { public function on_save_front_data() {
if ( isset( $_POST[ 'nonce_CMB2php' . OPALESTATE_AGENT_PREFIX . 'front' ] ) ) { if ( isset( $_POST[ 'nonce_CMB2php' . OPALESTATE_AGENT_PREFIX . 'front' ] ) ) {
$post_id = $this->update_data_agent_or_agency( OPALESTATE_AGENT_PREFIX ); $post_id = $this->update_data_agent_or_agency( OPALESTATE_AGENT_PREFIX );
@ -217,14 +225,14 @@ class Opalestate_Agent_Front {
} }
/** /**
* * Get field name.
*/ */
private function get_field_name( $field ) { private function get_field_name( $field ) {
return OPALESTATE_AGENT_PREFIX . $field; return OPALESTATE_AGENT_PREFIX . $field;
} }
/** /**
* * Update data for agent or agency.
*/ */
private function update_data_agent_or_agency( $prefix ) { private function update_data_agent_or_agency( $prefix ) {
global $current_user; global $current_user;
@ -270,7 +278,7 @@ class Opalestate_Agent_Front {
/* /*
* Processing upload files * Processing upload files
*/ */
$this->process_upload_files( $post_id, $_POST ); $this->process_upload_files( $post_id );
cmb2_get_metabox_form( $metaboxes[ $prefix . 'front' ], $post_id ); cmb2_get_metabox_form( $metaboxes[ $prefix . 'front' ], $post_id );
$cmb = cmb2_get_metabox( $prefix . 'front', $post_id ); $cmb = cmb2_get_metabox( $prefix . 'front', $post_id );
@ -333,22 +341,21 @@ class Opalestate_Agent_Front {
return opalestate_load_template_path( 'shortcodes/agent-carousel', $atts ); return opalestate_load_template_path( 'shortcodes/agent-carousel', $atts );
} }
/**
* Archive query.
*
* @param $query
* @return mixed
*/
public function archives_query( $query ) { public function archives_query( $query ) {
if ( $query->is_main_query() && is_post_type_archive( 'opalestate_agent' ) ) { if ( $query->is_main_query() && is_post_type_archive( 'opalestate_agent' ) ) {
$args = []; $args = [];
$min = opalestate_options( 'search_agent_min_price', 0 ); $min = opalestate_options( 'search_agent_min_price', 0 );
$max = opalestate_options( 'search_agent_max_price', 10000000 ); $max = opalestate_options( 'search_agent_max_price', 10000000 );
$search_min_price = isset( $_GET['min_price'] ) ? sanitize_text_field( $_GET['min_price'] ) : ''; $search_min_price = isset( $_GET['min_price'] ) ? sanitize_text_field( $_GET['min_price'] ) : '';
$search_max_price = isset( $_GET['max_price'] ) ? sanitize_text_field( $_GET['max_price'] ) : ''; $search_max_price = isset( $_GET['max_price'] ) ? sanitize_text_field( $_GET['max_price'] ) : '';
$search_min_area = isset( $_GET['min_area'] ) ? sanitize_text_field( $_GET['min_area'] ) : '';
$search_max_area = isset( $_GET['max_area'] ) ? sanitize_text_field( $_GET['max_area'] ) : '';
$s = isset( $_GET['search_text'] ) ? sanitize_text_field( $_GET['search_text'] ) : null;
$paged = ( get_query_var( 'paged' ) == 0 ) ? 1 : get_query_var( 'paged' ); $paged = ( get_query_var( 'paged' ) == 0 ) ? 1 : get_query_var( 'paged' );
$default = [ $default = [
'post_type' => 'opalestate_agent', 'post_type' => 'opalestate_agent',
@ -436,9 +443,10 @@ class Opalestate_Agent_Front {
return $query; return $query;
} }
/** /**
* Search agents.
* *
* @return string
*/ */
public function search_agents() { public function search_agents() {
return opalestate_load_template_path( 'shortcodes/search-agents' ); return opalestate_load_template_path( 'shortcodes/search-agents' );
@ -470,6 +478,13 @@ class Opalestate_Agent_Front {
} }
} }
/**
* Create agent.
*
* @param array $args
* @param $user_id
* @return int|\WP_Error
*/
public function create_agent( $args = [], $user_id ) { public function create_agent( $args = [], $user_id ) {
$data = get_user_by( 'id', $user_id ); $data = get_user_by( 'id', $user_id );
@ -515,7 +530,11 @@ class Opalestate_Agent_Front {
} }
/** /**
* Render front form.
* *
* @param $metaboxes
* @param int $post_id
* @return mixed
*/ */
public function render_front_form( $metaboxes, $post_id = 0 ) { public function render_front_form( $metaboxes, $post_id = 0 ) {
$metabox = new Opalestate_Agent_MetaBox(); $metabox = new Opalestate_Agent_MetaBox();

@ -40,15 +40,13 @@ function opalestate_output_msg_json( $result = false, $message = '', $args = [],
* Process upload images for properties * Process upload images for properties
*/ */
function opalesate_upload_image( $submitted_file, $parent_id = 0 ) { function opalesate_upload_image( $submitted_file, $parent_id = 0 ) {
require_once ABSPATH . 'wp-admin/includes/image.php';
// do_action( 'opalestate_before_process_ajax_upload_file' );
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/media.php';
$uploaded_image = wp_handle_upload( $submitted_file, // Handle PHP uploads in WordPress, sanitizing file names, checking extensions for mime type, and moving the
[ 'test_form' => false ] ); //Handle PHP uploads in WordPress, sanitizing file names, checking extensions for mime type, and moving the file to the appropriate directory within the uploads directory. // file to the appropriate directory within the uploads directory.
$uploaded_image = wp_handle_upload( $submitted_file, [ 'test_form' => false ] );
if ( isset( $uploaded_image['file'] ) ) { if ( isset( $uploaded_image['file'] ) ) {
$file_name = basename( $submitted_file['name'] ); $file_name = basename( $submitted_file['name'] );
@ -63,10 +61,15 @@ function opalesate_upload_image( $submitted_file, $parent_id = 0 ) {
'post_status' => 'inherit', 'post_status' => 'inherit',
]; ];
$attach_id = wp_insert_attachment( $attachment_details, $uploaded_image['file'], $parent_id ); // This function inserts an attachment into the media library // This function inserts an attachment into the media library.
$attach_data = wp_generate_attachment_metadata( $attach_id, $attach_id = wp_insert_attachment( $attachment_details, $uploaded_image['file'], $parent_id );
$uploaded_image['file'] ); // This function generates metadata for an image attachment. It also creates a thumbnail and other intermediate sizes of the image attachment based on the sizes defined
wp_update_attachment_metadata( $attach_id, $attach_data ); // Update metadata for an attachment. // This function generates metadata for an image attachment.
// It also creates a thumbnail and other intermediate sizes of the image attachment based on the sizes defined
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_image['file'] );
// Update metadata for an attachment.
wp_update_attachment_metadata( $attach_id, $attach_data );
$thumbnail_url = opalestate_get_upload_image_url( $attach_data ); $thumbnail_url = opalestate_get_upload_image_url( $attach_data );
@ -79,7 +82,6 @@ function opalesate_upload_image( $submitted_file, $parent_id = 0 ) {
update_post_meta( $attach_id, '_pending_to_use_', 1 ); update_post_meta( $attach_id, '_pending_to_use_', 1 );
return $ajax_response; return $ajax_response;
} }
return []; return [];