Completed
Push — develop ( 2c39bb...a4e86c )
by Gennady
15:59 queued 10s
created

GravityView_Edit_Entry_Locking::get_string()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
if ( ! class_exists( 'GFForms' ) ) {
4
	die();
5
}
6
7
/**
8
 * An entry locking class that syncs with GFEntryLocking.
9
 */
10
class GravityView_Edit_Entry_Locking {
11
	/**
12
	 * Load extension entry point.
13
	 *
14
	 * @return void
15
	 */
16
	public function load() {
17
		if ( ! has_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ) ) {
18
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
19
		}
20
	}
21
22
	/**
23
	 * Enqueue the required scripts and styles from Gravity Forms.
24
	 *
25
	 * Called via load() and `wp_enqueue_scripts`
26
	 *
27
	 * @return void
28
	 */
29
	public function enqueue_scripts() {
30
		if ( ! $entry = gravityview()->request->is_edit_entry() ) {
31
			return;
32
		}
33
34
		$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
35
		$locking_path = GFCommon::get_base_url() . '/includes/locking/';
36
37
		wp_enqueue_script( 'gforms_locking', $locking_path . "js/locking{$min}.js", array( 'jquery', 'heartbeat' ), GFCommon::$version );
38
		wp_enqueue_style( 'gforms_locking_css', $locking_path . "css/locking{$min}.css", array( 'edit' ), GFCommon::$version );
39
40
		$translations = array_map( 'wp_strip_all_tags', $this->get_strings() );
41
42
		$strings = array(
43
			'noResponse'    => $translations['no_response'],
44
			'requestAgain'  => $translations['request_again'],
45
			'requestError'  => $translations['request_error'],
46
			'gainedControl' => $translations['gained_control'],
47
			'rejected'      => $translations['request_rejected'],
48
			'pending'       => $translations['request_pending'],
49
		);
50
51
		$lock_user_id = $this->check_lock( $entry['id'] );
52
53
		$vars = array(
54
			'hasLock'    => ! $lock_user_id ? 1 : 0,
55
			'lockUI'     => $this->get_lock_ui( $lock_user_id ),
56
			'objectID'   => $entry['id'],
57
			'objectType' => 'entry',
58
			'strings'    => $strings,
59
		);
60
61
		wp_localize_script( 'gforms_locking', 'gflockingVars', $vars );
62
	}
63
64
	/**
65
	 * Returns a string with the Lock UI HTML markup.
66
	 *
67
	 * Called script enqueing, added to JavaScript gforms_locking global variable.
68
	 *
69
	 * @param int $user_id The User ID that has the current lock. Will be empty if entry is not locked
70
	 *                     or is locked to the current user. See self::check_lock
71
	 *
72
	 * @return string The Lock UI dialog box, etc.
73
	 */
74
	public function get_lock_ui( $user_id ) {
75
		$user = get_userdata( $user_id );
76
77
		$locked = $user_id && $user;
78
79
		$hidden = $locked ? '' : ' hidden';
80
		if ( $locked ) {
81
82
			$message = '<div class="gform-locked-message">
83
                            <div class="gform-locked-avatar">' . get_avatar( $user->ID, 64 ) . '</div>
84
                            <p class="currently-editing" tabindex="0">' . sprintf( $this->get_string( 'currently_locked' ), $user->display_name ) . '</p>
85
                            <p>
86
87
                                <a id="gform-take-over-button" style="display:none" class="button button-primary wp-tab-first" href="' . esc_url( add_query_arg( 'get-edit-lock', '1' ) ) . '">' . __( 'Take Over', 'gravityforms' ) . '</a>
88
                                <button id="gform-lock-request-button" class="button button-primary wp-tab-last">' . __( 'Request Control', 'gravityforms' ) . '</button>
89
                                <a class="button" onclick="history.back(-1); return false;">' . $this->get_string( 'cancel' ) . '</a>
90
                            </p>
91
                            <div id="gform-lock-request-status">
92
                                <!-- placeholder -->
93
                            </div>
94
                        </div>';
95
96
		} else {
97
98
			$message = '<div class="gform-taken-over">
99
                            <div class="gform-locked-avatar"></div>
100
                            <p class="wp-tab-first" tabindex="0">
101
                                <span class="currently-editing"></span><br>
102
                            </p>
103
                            <p>
104
                                <a id="gform-release-lock-button" class="button button-primary wp-tab-last"  href="' . esc_url( add_query_arg( 'release-edit-lock', '1' ) ) . '">' . $this->get_string( 'accept' ) . '</a>
105
                                <button id="gform-reject-lock-request-button" style="display:none"  class="button button-primary wp-tab-last">' . __( 'Reject Request', 'gravityforms' ) . '</button>
106
                            </p>
107
                        </div>';
108
109
		}
110
		$html = '<div id="gform-lock-dialog" class="notification-dialog-wrap' . $hidden . '">
111
                    <div class="notification-dialog-background"></div>
112
                    <div class="notification-dialog">';
113
		$html .= $message;
114
115
		$html .= '   </div>
116
                 </div>';
117
118
		return $html;
119
	}
120
121
	/**
122
	 * Localized string for the UI.
123
	 *
124
	 * Uses gravityforms textdomain unchanged.
125
	 *
126
	 * @return array An array of translations.
127
	 */
128
	public function get_strings() {
129
		$translations = array(
130
			'currently_locked'  => __( 'This entry is currently locked. Click on the "Request Control" button to let %s know you\'d like to take over.', 'gravityforms' ), 'currently_editing' => '%s is currently editing this entry',
131
			'taken_over'        => __( '%s has taken over and is currently editing this entry.', 'gravityforms' ),
132
			'lock_requested'    => __( '%s has requested permission to take over control of this entry.', 'gravityforms' ),
133
			'accept'            => __( 'Accept', 'gravityforms' ),
134
			'cancel'            => __( 'Cancel', 'gravityforms' ),
135
			'currently_editing' => __( '%s is currently editing', 'gravityforms' ),
136
			'taken_over'        => __( '%s has taken over and is currently editing.', 'gravityforms' ),
137
			'gained_control'    => __( 'You now have control', 'gravityforms' ),
138
			'request_pending'   => __( 'Pending', 'gravityforms' ),
139
			'no_response'       => __( 'No response', 'gravityforms' ),
140
			'request_again'     => __( 'Request again', 'gravityforms' ),
141
			'request_error'     => __( 'Error', 'gravityforms' ),
142
			'request_rejected'  => __( 'Your request was rejected', 'gravityforms' ),
143
		);
144
145
		return $translations;
146
	}
147
148
	/**
149
	 * Get a localized string.
150
	 *
151
	 * @param string $string The string to get.
152
	 *
153
	 * @return string A localized string. See self::get_strings()
154
	 */
155
	public function get_string( $string ) {
156
		return \GV\Utils::get( $this->get_strings(), $string, '' );
157
	}
158
159
	/**
160
	 * Lock the entry... maybe.
161
	 *
162
	 * Has 3 modes of locking:
163
	 *
164
	 *  - acquire (get), which reloads the page after locking the entry
165
	 *  - release, which reloads the page after unlocking the entry
166
	 *  - default action to lock on load if not locked
167
	 *
168
	 * @param int $entry_id The entry ID.
169
	 *
170
	 * @return void
171
	 */
172
	public function maybe_lock_object( $entry_id ) {
173
		global $wp;
174
		$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
175
176
		if ( isset( $_GET['get-edit-lock'] ) ) {
177
			$this->set_lock( $entry_id );
178
			echo '<script>window.location = ' . json_encode( remove_query_arg( 'get-edit-lock', $current_url ) ) . ';</script>';
179
			exit();
180
		} else if ( isset( $_GET['release-edit-lock'] ) ) {
181
			$this->delete_lock_meta( $entry_id );
182
			$current_url = remove_query_arg( 'edit', $current_url );
183
			echo '<script>window.location = ' . json_encode( remove_query_arg( 'release-edit-lock', $current_url ) ) . ';</script>';
184
			exit();
185
		} else {
186
			if ( ! $user_id = $this->check_lock( $entry_id ) ) {
187
				$this->set_lock( $entry_id );
188
			}
189
		}
190
	}
191
192
	/**
193
	 * Is this entry locked to some other user?
194
	 *
195
	 * @param int $entry_id The entry ID.
196
	 *
197
	 * @return boolean Yes or no.
198
	 */
199
	public function check_lock( $entry_id ) {
200
		if ( ! $user_id = $this->get_lock_meta( $entry_id ) ) {
201
			return false;
202
		}
203
204
		if ( $user_id != get_current_user_id() ) {
205
			return $user_id;
206
		}
207
208
		return false;
209
	}
210
211
	/**
212
	 * The lock for an entry.
213
	 *
214
	 * Leverages Gravity Forms' persistent caching mechanisms.
215
	 *
216
	 * @param int $entry_id The entry ID.
217
	 *
218
	 * @return int|null The User ID or null.
219
	 */
220
	public function get_lock_meta( $entry_id ) {
221
		return GFCache::get( 'lock_entry_' . $entry_id );
222
	}
223
224
	/**
225
	 * Set the lock for an entry.
226
	 *
227
	 * @param int $entry_id The entry ID.
228
	 * @param int $user_id The user ID to lock the entry to.
229
	 *
230
	 * @return void
231
	 */
232
	public function update_lock_meta( $entry_id, $user_id ) {
233
		GFCache::set( 'lock_entry_' . $entry_id, $user_id, true, 1500 );
234
	}
235
236
	/**
237
	 * Release the lock for an entry.
238
	 *
239
	 * @param int $entry_id The entry ID.
240
	 *
241
	 * @return void
242
	 */
243
	public function delete_lock_meta( $entry_id ) {
244
		GFCache::delete( 'lock_entry_' . $entry_id );
245
	}
246
247
	/**
248
	 * Lock the entry to the current user.
249
	 *
250
	 * @param int $entry_id The entry ID.
251
	 *
252
	 * @return int|false Locked or not.
253
	 */
254
	public function set_lock( $entry_id ) {
255
		if ( 0 == ( $user_id = get_current_user_id() ) ) {
256
			return false;
257
		}
258
259
		$this->update_lock_meta( $entry_id, $user_id );
260
261
		return $user_id;
262
	}
263
}
264