PluginProbe
WPCasa – Real Estate for WordPress / trunk
WPCasa – Real Estate for WordPress vtrunk
1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.1.1 trunk 1.2.0 1.2.1 1.2.10 1.2.10.1 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.2.9.1 1.2.9.2 1.3.0 All 31 releases
wpcasa / includes / admin / class-wpsight-agents.php

class-wpsight-agents.php in WPCasa – Real Estate for WordPress trunk, at includes/admin/class-wpsight-agents.php

185 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 /**
6 * WPSight_Admin_Agents class
7 */
8 class WPSight_Admin_Agents {
9
10 /**
11 * Constructor
12 */
13 public function __construct() {
14
15 add_action( 'personal_options_update', array( $this, 'profile_agent_update_save' ) );
16 add_action( 'edit_user_profile_update', array( $this, 'profile_agent_update_save' ) );
17
18 add_action( 'pre_get_posts', array( $this, 'filter_media_files' ) );
19 add_filter( 'wp_count_attachments', array( $this, 'recount_attachments' ) );
20
21 }
22
23 /**
24 * profile_agent_update_save()
25 *
26 * Save update agent data in listings option on profile pages.
27 * When the option is checked, the agent information of all
28 * listings of this user will be updated with the profile info.
29 *
30 * @param interger $user_id The user ID of the user being edited
31 * @uses current_user_can()
32 * @uses wpsight_post_type()
33 * @uses wpsight_get_user_posts_by_type()
34 * @uses wp_get_attachment_url()
35 * @uses update_user_meta()
36 *
37 * @since 1.0.0
38 */
39 public function profile_agent_update_save( $user_id ) {
40
41 if( ! isset( $_POST[ '_wpnonce' ] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ '_wpnonce' ] ) ), 'update-user_' . $user_id ) ) {
42 return;
43 }
44
45 if ( ! current_user_can( 'listing_admin' ) && ! current_user_can( 'administrator' ) )
46 return false;
47
48 $agent_update = isset( $_POST['agent_update'] ) && rest_sanitize_boolean( $_POST['agent_update'] );
49
50 if( $agent_update ) {
51
52 // Get all listings created by this user
53 $user_listings = wpsight_get_user_posts_by_type( $user_id, wpsight_post_type() );
54
55 // Map listing agent options with profile info
56
57 $agent_options = array(
58 '_agent_name' => isset( $_POST['display_name'] ) ? sanitize_text_field( $_POST['display_name'] ) : '',
59 '_agent_company' => isset( $_POST['company'] ) ? sanitize_text_field( $_POST['company'] ) : '',
60 '_agent_phone' => isset( $_POST['phone'] ) ? sanitize_text_field( $_POST['phone'] ) : '',
61 '_agent_description' => isset( $_POST['description'] ) ? sanitize_post( $_POST['description'] ) : '',
62 '_agent_website' => isset( $_POST['url'] ) ? esc_url_raw( $_POST['url'] ) : '',
63 '_agent_twitter' => isset( $_POST['twitter'] ) ? sanitize_text_field( $_POST['twitter'] ) : '',
64 '_agent_facebook' => isset( $_POST['facebook'] ) ? sanitize_text_field( $_POST['facebook'] ) : '',
65 '_agent_logo' => isset( $_POST['agent_logo'] ) ? sanitize_text_field( $_POST['agent_logo'] ) : '',
66 '_agent_logo_id' => isset( $_POST['agent_logo_id'] ) ? sanitize_text_field( $_POST['agent_logo_id'] ) : ''
67 );
68
69 $agent_options = apply_filters( 'wpsight_profile_agent_update_save_options', $agent_options, $user_id );
70
71 // Loop through user listings
72
73 foreach( (array) $user_listings as $post_id ) {
74
75 // Loop through listing agent options
76
77 foreach( (array) $agent_options as $option => $getpost ) {
78
79 // Add filter before actually saving the data
80 $getpost = apply_filters( 'wpsight_profile_agent_update_post_meta', $getpost, $option, $post_id, $agent_options );
81
82 // Update post meta
83 update_post_meta( $post_id, $option, $getpost );
84
85 }
86
87 }
88
89 // Remove update value to let checkbox unchecked
90
91 update_user_meta( $user_id, 'agent_update', '' );
92 $_POST['agent_update'] = '';
93
94 }
95
96 }
97
98 /**
99 * media_library_restrict()
100 *
101 * Restrict media library to items uploaded
102 * by the current user if he does not have
103 * 'read_private_listings' capability.
104 *
105 * @param object WP_Query
106 * @uses get_current_screen()
107 * @uses current_user_can()
108 * @uses get_current_user_id()
109 *
110 * @since 1.0.0
111 */
112 public function media_library_restrict( $wp_query ) {
113
114 if ( 'upload' != get_current_screen()->id )
115 return;
116
117 if( current_user_can( 'edit_listings' ) && ! current_user_can( 'read_private_listings' ) && $wp_query->query['post_type'] === 'attachment' )
118 $wp_query->set( 'author', get_current_user_id() );
119
120 }
121
122 /**
123 * filter_media_files()
124 *
125 * Restrict media library to items uploaded
126 * by the current user if he does not have
127 * 'read_private_listings' capability.
128 *
129 * @param object WP_Query
130 * @uses current_user_can()
131 * @author Damir Calusic
132 * @see https://wordpress.org/plugins/wp-users-media/
133 *
134 * @since 1.0.0
135 */
136
137 public function filter_media_files( $wp_query ) {
138 global $current_user;
139
140 if( ! current_user_can( 'read_private_listings' ) && ( is_admin() && $wp_query->query['post_type'] === 'attachment' ) )
141 $wp_query->set( 'author', $current_user->ID );
142
143 }
144
145 /**
146 * recount_attachments()
147 *
148 * Recount attachments on media
149 * library screen and elsewhere.
150 *
151 * @author Damir Calusic
152 * @see https://wordpress.org/plugins/wp-users-media/
153 *
154 * @since 1.0.0
155 */
156
157 function recount_attachments( $_counts ) {
158 global $wpdb, $current_user;
159
160 $current_user_id = absint( $current_user->ID );
161 $count = $wpdb->get_results(
162 $wpdb->prepare(
163 "SELECT post_mime_type, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status != 'trash' AND post_author = %d GROUP BY post_mime_type",
164 $current_user_id
165 ),
166 ARRAY_A
167 );
168
169 $counts = array();
170 foreach( (array) $count as $row )
171 $counts[ $row['post_mime_type'] ] = $row['num_posts'];
172
173 $counts['trash'] = $wpdb->get_var(
174 $wpdb->prepare(
175 "SELECT COUNT( * ) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_author = %d AND post_status = 'trash'",
176 $current_user_id
177 )
178 );
179
180 return $counts;
181
182 }
183
184 }
185