PluginProbe ʕ •ᴥ•ʔ
WP Job Manager / 2.4.7
WP Job Manager v2.4.7
2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.11.0 1.11.1 1.12.0 1.12.1 1.13.0 1.14.0 1.15.0 1.16.0 1.16.1 1.17.0 1.18.0 1.19.0 1.2.0 1.20.0 1.20.1 1.21.0 1.21.1 1.21.2 1.21.3 1.21.4 1.22.0 1.22.1 1.22.2 1.22.3 1.23.0 1.23.1 1.23.10 1.23.11 1.23.12 1.23.13 1.23.2 1.23.3 1.23.4 1.23.5 1.23.6 1.23.7 1.23.8 1.23.9 1.24.0 1.24.0.1 1.25.0 1.25.0.1 1.25.1 1.25.1.1 1.25.2 1.25.2.1 1.25.3 1.25.3.1 1.26.0 1.26.0.1 1.26.1 1.26.1.1 1.26.2 1.26.2.1 1.27.0 1.27.0.1 1.28.0 1.28.0.1 1.29.0 1.29.0.1 1.29.1 1.29.1.1 1.29.2 1.29.2.1 1.29.3 1.29.3.1 1.3.0 1.3.1 1.30.0 1.30.0.1 1.30.1 1.30.1.1 1.30.2 1.30.2.1 1.31.0 1.31.0.1 1.31.1 1.31.1.1 1.31.2 1.31.3 1.32.0 1.32.1 1.32.2 1.32.3 1.33.0 1.33.1 1.33.2 1.33.3 1.33.4 1.33.5 1.34.0 1.34.1 1.34.2 1.34.3 1.34.4 1.34.5 1.35.0 1.35.1 1.35.2 1.35.3 1.36.0 1.36.1 1.36.2 1.37.0 1.38.0 1.38.1 1.39.0 1.4.0 1.40.0 1.40.1 1.40.2 1.41.0 1.42.0 1.5.0 1.5.1 1.5.2 1.6.0 1.7.0 1.7.1 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 1.9.1 1.9.2 1.9.3 2.0.0 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.4.1
wp-job-manager / includes / class-wp-job-manager-usage-tracking.php
wp-job-manager / includes Last commit date
3rd-party 3 days ago abstracts 3 months ago admin 2 weeks ago emails 2 weeks ago forms 3 days ago helper 3 months ago promoted-jobs 3 days ago ui 2 weeks ago widgets 2 weeks ago class-access-token.php 2 years ago class-dev-tools.php 2 years ago class-guest-session.php 2 years ago class-guest-user.php 2 years ago class-job-dashboard-shortcode.php 6 months ago class-job-listing-stats.php 2 years ago class-job-overlay.php 2 years ago class-stats-dashboard.php 2 years ago class-stats-script.php 3 months ago class-stats.php 3 months ago class-wp-job-manager-ajax.php 2 months ago class-wp-job-manager-api.php 6 years ago class-wp-job-manager-blocks.php 2 weeks ago class-wp-job-manager-cache-helper.php 3 months ago class-wp-job-manager-category-walker.php 6 years ago class-wp-job-manager-com-api.php 2 years ago class-wp-job-manager-data-cleaner.php 2 years ago class-wp-job-manager-data-exporter.php 3 months ago class-wp-job-manager-dependency-checker.php 2 years ago class-wp-job-manager-email-notifications.php 2 years ago class-wp-job-manager-forms.php 5 years ago class-wp-job-manager-geocode.php 2 weeks ago class-wp-job-manager-install.php 2 years ago class-wp-job-manager-post-types.php 3 days ago class-wp-job-manager-recaptcha.php 6 months ago class-wp-job-manager-rest-api.php 2 months ago class-wp-job-manager-shortcodes.php 2 weeks ago class-wp-job-manager-usage-tracking-data.php 2 years ago class-wp-job-manager-usage-tracking.php 2 years ago class-wp-job-manager-widget.php 2 weeks ago class-wp-job-manager.php 3 months ago trait-singleton.php 2 years ago
class-wp-job-manager-usage-tracking.php
316 lines
1 <?php
2 /**
3 * File containing the class WP_Job_Manager_Usage_Tracking.
4 *
5 * @package wp-job-manager
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 require_once __DIR__ . '/../lib/usage-tracking/class-wp-job-manager-usage-tracking-base.php';
13
14 /**
15 * WPJM Usage Tracking subclass.
16 */
17 class WP_Job_Manager_Usage_Tracking extends WP_Job_Manager_Usage_Tracking_Base {
18
19 const WPJM_SETTING_NAME = 'job_manager_usage_tracking_enabled';
20
21 const WPJM_TRACKING_INFO_URL = 'https://wpjobmanager.com/document/what-data-does-wpjm-track';
22
23 /**
24 * WP_Job_Manager_Usage_Tracking constructor.
25 */
26 protected function __construct() {
27 parent::__construct();
28
29 // Add filter for settings.
30 add_filter( 'job_manager_settings', [ $this, 'add_setting_field' ] );
31
32 // In the setup wizard, do not display the normal opt-in dialog.
33 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Input is used safely.
34 if ( isset( $_GET['page'] ) && 'job-manager-setup' === $_GET['page'] ) {
35 remove_action( 'admin_notices', [ $this, 'maybe_display_tracking_opt_in' ] );
36 }
37 }
38
39 /**
40 * Gets the base data returned with system information.
41 *
42 * @return array
43 */
44 protected function get_base_system_data() {
45 $base_data = [];
46 $base_data['version'] = JOB_MANAGER_VERSION;
47
48 return $base_data;
49 }
50
51 /**
52 * Track a WP Job Manager event.
53 *
54 * @since 1.33.0
55 *
56 * @param string $event_name The name of the event, without the `wpjm` prefix.
57 * @param array $properties The event properties to be sent.
58 */
59 public static function log_event( $event_name, $properties = [] ) {
60 $properties = array_merge(
61 WP_Job_Manager_Usage_Tracking_Data::get_event_logging_base_fields(),
62 $properties
63 );
64
65 self::get_instance()->send_event( $event_name, $properties );
66 }
67
68 /**
69 * Get the current user's primary role.
70 *
71 * @return string
72 */
73 private static function get_current_role() {
74 $current_user = wp_get_current_user();
75 $roles = $current_user->roles;
76
77 if ( empty( $roles ) ) {
78 return 'guest';
79 }
80
81 if ( in_array( 'administrator', $roles, true ) ) {
82 return 'administrator';
83 }
84
85 if ( in_array( 'employer', $roles, true ) ) {
86 return 'employer';
87 }
88
89 return array_shift( $roles );
90 }
91
92
93 /**
94 * Check if current request is a REST API request.
95 *
96 * @todo move this to WP_Job_Manager_REST_API
97 * @return bool
98 */
99 public static function is_rest_request() {
100 return defined( 'REST_REQUEST' ) && REST_REQUEST;
101 }
102
103 /**
104 * Track the job submission event.
105 *
106 * @param int $post_id Post ID.
107 * @param array $properties Default properties to use.
108 */
109 public static function track_job_submission( $post_id, $properties = [] ) {
110 // Only track the first time a job is submitted.
111 if ( get_post_meta( $post_id, '_tracked_submitted' ) ) {
112 return;
113 }
114 update_post_meta( $post_id, '_tracked_submitted', time() );
115
116 $properties['job_id'] = intval( $post_id );
117 $properties['post_status'] = get_post_status( $post_id );
118
119 $user_role = self::get_current_role();
120 if ( $user_role ) {
121 $properties['user_role'] = $user_role;
122 }
123
124 self::log_event( 'job_listing_submitted', $properties );
125 }
126
127 /**
128 * Track the job approval event.
129 *
130 * @param int $post_id Post ID.
131 * @param array $properties Default properties to use.
132 */
133 public static function track_job_approval( $post_id, $properties = [] ) {
134 // Only track the first time a job is approved.
135 if ( get_post_meta( $post_id, '_tracked_approved' ) ) {
136 return;
137 }
138 update_post_meta( $post_id, '_tracked_approved', time() );
139
140 $post = get_post( $post_id );
141
142 $properties['job_id'] = intval( $post_id );
143 $properties['age'] = time() - strtotime( $post->post_date_gmt );
144
145 $user_role = self::get_current_role();
146 if ( $user_role ) {
147 $properties['user_role'] = $user_role;
148 }
149
150 self::log_event( 'job_listing_approved', $properties );
151 }
152
153 /**
154 * Implementation for abstract functions.
155 */
156
157 /**
158 * Return the instance of this class.
159 *
160 * @return self
161 */
162 public static function get_instance() {
163 return self::get_instance_for_subclass( static::class );
164 }
165
166 /**
167 * Get prefix for the usage data setting.
168 *
169 * @return string
170 */
171 protected function get_prefix() {
172 return 'job_manager';
173 }
174
175 /**
176 * Get prefix for the event sent for usage tracking.
177 *
178 * @return string
179 */
180 protected function get_event_prefix() {
181 return 'wpjm';
182 }
183
184 /**
185 * Get the text domain used in the plugin. Deprecated - use 'wp-job-manager' directly.
186 *
187 * @deprecated 1.42.0
188 *
189 * @return string
190 */
191 protected function get_text_domain() {
192 return 'wp-job-manager';
193 }
194
195 /**
196 * Get the status of usage tracking.
197 *
198 * @return bool
199 */
200 public function get_tracking_enabled() {
201 return get_option( self::WPJM_SETTING_NAME ) || false;
202 }
203
204 /**
205 * Set whether or not usage tracking is enabled.
206 *
207 * @param bool $enable
208 */
209 public function set_tracking_enabled( $enable ) {
210 update_option( self::WPJM_SETTING_NAME, $enable );
211 }
212
213 /**
214 * Check if the current user can manage usage tracking settings.
215 *
216 * @return bool
217 */
218 protected function current_user_can_manage_tracking() {
219 return current_user_can( 'manage_options' );
220 }
221
222 /**
223 * Get the text to show in the opt-in dialog.
224 *
225 * @return string
226 */
227 protected function opt_in_dialog_text() {
228 return sprintf(
229 // translators: Placeholder %s is a URL to the document on wpjobmanager.com with info on usage tracking.
230 __(
231 '<p>We\'d love if you helped us make WP Job Manager better by allowing us to collect
232 <a target="_blank" href="%s">usage tracking data</a>.</p><p>No sensitive information is
233 collected, and you can opt out at any time.</p>',
234 'wp-job-manager'
235 ),
236 self::WPJM_TRACKING_INFO_URL
237 );
238 }
239
240 /*
241 * Public functions.
242 */
243
244 /**
245 * Hide the opt-in for enabling usage tracking.
246 **/
247 public function hide_tracking_opt_in() { // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod
248 parent::hide_tracking_opt_in();
249 }
250
251 /**
252 * Allowed html tags, used by wp_kses, for the translated opt-in dialog
253 * text.
254 *
255 * @return array the html tags.
256 **/
257 public function opt_in_dialog_text_allowed_html() { // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod
258 return parent::opt_in_dialog_text_allowed_html();
259 }
260
261 /**
262 * Get the opt-in text.
263 *
264 * @return string
265 */
266 public function opt_in_checkbox_text() {
267 return sprintf(
268 // translators: the href tag contains the URL for the page telling users what data WPJM tracks.
269 __(
270 'Help us make Job Manager better by allowing us to collect
271 <a target="_blank" href="%s">usage tracking data</a>.
272 No sensitive information is collected.',
273 'wp-job-manager'
274 ),
275 self::WPJM_TRACKING_INFO_URL
276 );
277 }
278
279
280 /**
281 * Hooks.
282 */
283
284 /**
285 * Add tracking setting field to general settings.
286 *
287 * @param array $fields
288 * @return array
289 */
290 public function add_setting_field( $fields ) {
291 $fields['general'][1][] = [
292 'name' => self::WPJM_SETTING_NAME,
293 'std' => '0',
294 'type' => 'checkbox',
295 'desc' => '',
296 'label' => __( 'Enable Usage Tracking', 'wp-job-manager' ),
297 'cb_label' => $this->opt_in_checkbox_text(),
298 ];
299
300 return $fields;
301 }
302
303
304 /**
305 * Helpers.
306 */
307
308 /**
309 * Clear options used for usage tracking.
310 */
311 public function clear_options() {
312 delete_option( self::WPJM_SETTING_NAME );
313 delete_option( $this->hide_tracking_opt_in_option_name );
314 }
315 }
316