Completed
Push — develop ( faed85...5d0bd3 )
by Zack
04:39
created

includes/class-admin.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class GravityView_Admin {
4
5
	function __construct() {
6
7
		if( ! is_admin() ) { return; }
8
9
		// If Gravity Forms isn't active or compatibile, stop loading
10
		if( false === GravityView_Compatibility::is_valid() ) {
0 ignored issues
show
Deprecated Code introduced by
The method GravityView_Compatibility::is_valid() has been deprecated.

This method has been deprecated.

Loading history...
11
			return;
12
		}
13
14
		$this->include_required_files();
15
		$this->add_hooks();
16
	}
17
18
	/**
19
	 * @since 1.15
20
	 * @return void
21
	 */
22
	private function include_required_files() {
23
24
		// Migrate Class
25
		require_once( GRAVITYVIEW_DIR . 'includes/class-gravityview-migrate.php' );
26
27
		// Don't load tooltips if on Gravity Forms, otherwise it overrides translations
28
		if( class_exists( 'GFCommon' ) && class_exists( 'GFForms' ) && !GFForms::is_gravity_page() ) {
29
			require_once( GFCommon::get_base_path() . '/tooltips.php' );
30
		}
31
32
		require_once( GRAVITYVIEW_DIR . 'includes/admin/metaboxes/class-gravityview-admin-metaboxes.php' );
33
		require_once( GRAVITYVIEW_DIR . 'includes/admin/entry-list.php' );
34
		require_once( GRAVITYVIEW_DIR . 'includes/class-gravityview-change-entry-creator.php' );
35
36
		/** @since 1.15 **/
37
		require_once( GRAVITYVIEW_DIR . 'includes/admin/class-gravityview-support-port.php' );
38
39
		/** @since 1.6 */
40
		require_once( GRAVITYVIEW_DIR . 'includes/class-gravityview-admin-duplicate-view.php' );
41
42
		/** @since 1.17 */
43
		require_once( GRAVITYVIEW_DIR . 'includes/admin/class-gravityview-admin-no-conflict.php' );
44
	}
45
46
	/**
47
	 * @since 1.7.5
48
	 * @return void
49
	 */
50
	private function add_hooks() {
51
52
		// Filter Admin messages
53
		add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
54
		add_filter( 'bulk_post_updated_messages', array( $this, 'post_updated_messages' ) );
55
56
		add_filter( 'plugin_action_links_'. plugin_basename( GRAVITYVIEW_FILE ) , array( $this, 'plugin_action_links' ) );
57
58
		add_action( 'plugins_loaded', array( $this, 'backend_actions' ), 100 );
59
60
	}
61
62
	/**
63
	 * Get text for no views found.
64
	 *
65
	 * @since 1.18 Moved to GravityView_Admin
66
	 *
67
	 * @return string HTML message with no container tags.
68
	 */
69
	public static function no_views_text() {
70
		
71
		if ( isset( $_REQUEST['post_status'] ) && 'trash' === $_REQUEST['post_status'] ) {
72
			return __( 'No Views found in Trash', 'gravityview' );
73
		} elseif( ! empty( $_GET['s'] ) ) {
74
			return __( 'No Views found.', 'gravityview' );
75
		}
76
77
		// Floaty the Astronaut says "oi"
78
		$image = self::get_floaty();
79
80
		if ( GVCommon::has_cap( 'edit_gravityviews' ) ) {
81
			$output = sprintf( esc_attr__( "%sYou don't have any active views. Let&rsquo;s go %screate one%s!%s\n\nIf you feel like you're lost in space and need help getting started, check out the %sGetting Started%s page.", 'gravityview' ), '<h3>', '<a href="' . admin_url( 'post-new.php?post_type=gravityview' ) . '">', '</a>', '</h3>', '<a href="' . admin_url( 'edit.php?post_type=gravityview&page=gv-getting-started' ) . '">', '</a>' );
82
		} else {
83
			$output = esc_attr__( 'There are no active Views', 'gravityview' );
84
		}
85
86
		return $image . wpautop( $output );
87
	}
88
89
	/**
90
	 * Display error HTML in Edit View when the form is in the trash or no longer exists in Gravity Forms
91
	 *
92
	 * @since 1.19
93
	 *
94
	 * @param int $form_id Gravity Forms
95
	 *
96
	 * @return void
97
	 */
98
	public static function connected_form_warning( $form_id = 0 ) {
99
        global $pagenow;
100
101
		if ( ! is_int( $form_id ) || $pagenow === 'post-new.php' ) {
102
			return;
103
		}
104
105
		$form_info = GFFormsModel::get_form( $form_id, true );
106
107
		$error = '';
108
		if ( empty( $form_info ) ) {
109
			$error = esc_html__( 'The form connected to this View no longer exists.', 'gravityview' );
110
			$error .= ' ' . esc_html__( 'Select another form as the data source for this View.', 'gravityview' );
111
		} elseif ( $form_info->is_trash ) {
112
			$error = esc_html__( 'The connected form is in the trash.', 'gravityview' );
113
			$error .= ' ' . gravityview_get_link( admin_url( 'admin.php?page=gf_edit_forms&filter=trash' ), esc_html__( 'Restore the form from the trash', 'gravityview' ) );
114
			$error .= ' ' . esc_html__( 'or select another form.', 'gravityview' );
115
		}
116
117
		if( $error ) {
118
			?>
119
			<div class="wp-dialog notice-warning inline error wp-clearfix">
120
				<?php echo gravityview_get_floaty(); ?>
121
				<h3><?php echo $error; ?></h3>
122
			</div>
123
			<?php
124
		}
125
	}
126
127
	/**
128
	 * Function to launch admin objects
129
	 *
130
	 * @access public
131
	 * @return void
132
	 */
133
	public function backend_actions() {
134
135
		/** @define "GRAVITYVIEW_DIR" "../" */
136
		include_once( GRAVITYVIEW_DIR .'includes/admin/class.field.type.php' );
137
		include_once( GRAVITYVIEW_DIR .'includes/admin/class.render.settings.php' );
138
		include_once( GRAVITYVIEW_DIR .'includes/admin/class-gravityview-admin-view-item.php' );
139
		include_once( GRAVITYVIEW_DIR .'includes/admin/class-gravityview-admin-view-field.php' );
140
		include_once( GRAVITYVIEW_DIR .'includes/admin/class-gravityview-admin-view-widget.php' );
141
		include_once( GRAVITYVIEW_DIR .'includes/class-admin-views.php' );
142
		include_once( GRAVITYVIEW_DIR .'includes/class-admin-welcome.php' );
143
		include_once( GRAVITYVIEW_DIR .'includes/class-admin-add-shortcode.php' );
144
		include_once( GRAVITYVIEW_DIR .'includes/class-admin-approve-entries.php' );
145
146
		/**
147
		 * @action `gravityview_include_backend_actions` Triggered after all GravityView admin files are loaded
148
		 *
149
		 * Nice place to insert extensions' backend stuff
150
		 */
151
		do_action('gravityview_include_backend_actions');
152
	}
153
154
	/**
155
	 * Modify plugin action links at plugins screen
156
	 *
157
	 * @since 1.15 Added check for `gravityview_view_settings` and `gravityview_support_port` capabilities
158
	 * @access public
159
	 * @static
160
	 * @param array $links Array of action links under GravityView on the plugin page
161
	 * @return array Action links with Settings and Support included, if the user has the appropriate caps
162
	 */
163
	public static function plugin_action_links( $links ) {
164
165
		$actions = array();
166
167
		if( GVCommon::has_cap( 'gravityview_view_settings' ) ) {
168
			$actions[] = sprintf( '<a href="%s">%s</a>', admin_url( 'edit.php?post_type=gravityview&page=gravityview_settings' ), esc_html__( 'Settings', 'gravityview' ) );
169
		}
170
171
		if( GVCommon::has_cap( 'gravityview_support_port' ) ) {
172
			$actions[] = '<a href="http://docs.gravityview.co">' . esc_html__( 'Support', 'gravityview' ) . '</a>';
173
		}
174
175
		return array_merge( $actions, $links );
176
	}
177
178
	/**
179
	 * Get an image of our intrepid explorer friend
180
	 * @return string HTML image tag with floaty's cute mug on it
181
	 */
182
	public static function get_floaty() {
183
		return gravityview_get_floaty();
184
	}
185
186
	/**
187
	 * Filter Admin messages
188
	 *
189
	 * @param  array      $messages Existing messages
190
	 * @return array                Messages with GravityView views!
191
	 */
192
	function post_updated_messages( $messages, $bulk_counts = NULL ) {
193
		global $post;
194
195
		$post_id = get_the_ID();
196
197
		// By default, there will only be one item being modified.
198
		// When in the `bulk_post_updated_messages` filter, there will be passed a number
199
		// of modified items that will override this array.
200
		$bulk_counts = is_null( $bulk_counts ) ? array( 'updated' => 1 , 'locked' => 1 , 'deleted' => 1 , 'trashed' => 1, 'untrashed' => 1 ) : $bulk_counts;
201
202
		// If we're starting fresh, a new form was created.
203
		// We should let the user know this is the case.
204
		$start_fresh = get_post_meta( $post_id, '_gravityview_start_fresh', true );
205
206
		$new_form_text = '';
207
208
		if( !empty( $start_fresh ) ) {
209
210
			// Get the form that was created
211
			$connected_form = gravityview_get_form_id( $post_id );
212
213
			if( !empty( $connected_form ) ) {
214
				$form = gravityview_get_form( $connected_form );
215
				$form_name = esc_attr( $form['title'] );
216
				$image = self::get_floaty();
217
				$new_form_text .= '<h3>'.$image.sprintf( __( 'A new form was created for this View: "%s"', 'gravityview' ), $form_name ).'</h3>';
218
				$new_form_text .=  sprintf( __( '%sThere are no entries for the new form, so the View will also be empty.%s To start collecting entries, you can add submissions through %sthe preview form%s and also embed the form on a post or page using this code: %s
219
220
					You can %sedit the form%s in Gravity Forms and the updated fields will be available here. Don&rsquo;t forget to %scustomize the form settings%s.
221
					', 'gravityview' ), '<strong>', '</strong>', '<a href="'.site_url( '?gf_page=preview&amp;id='.$connected_form ).'">', '</a>', '<code>[gravityform id="'.$connected_form.'" name="'.$form_name.'"]</code>', '<a href="'.admin_url( 'admin.php?page=gf_edit_forms&amp;id='.$connected_form ).'">', '</a>', '<a href="'.admin_url( 'admin.php?page=gf_edit_forms&amp;view=settings&amp;id='.$connected_form ).'">', '</a>');
222
				$new_form_text = wpautop( $new_form_text );
223
224
				delete_post_meta( $post_id, '_gravityview_start_fresh' );
225
			}
226
		}
227
228
		$messages['gravityview'] = array(
229
			0  => '', // Unused. Messages start at index 1.
230
			/* translators: %s and %s are HTML tags linking to the View on the website */
231
			1  => sprintf(__( 'View updated. %sView on website.%s', 'gravityview' ), '<a href="'.get_permalink( $post_id ).'">', '</a>'),
232
			/* translators: %s and %s are HTML tags linking to the View on the website */
233
			2  => sprintf(__( 'View updated. %sView on website.%s', 'gravityview' ), '<a href="'.get_permalink( $post_id ).'">', '</a>'),
234
			3  => __( 'View deleted.', 'gravityview' ),
235
			/* translators: %s and %s are HTML tags linking to the View on the website */
236
			4  => sprintf(__( 'View updated. %sView on website.%s', 'gravityview' ), '<a href="'.get_permalink( $post_id ).'">', '</a>'),
237
			/* translators: %s: date and time of the revision */
238
			5  => isset( $_GET['revision'] ) ? sprintf( __( 'View restored to revision from %s', 'gravityview' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
239
			/* translators: %s and %s are HTML tags linking to the View on the website */
240
			6  => sprintf(__( 'View published. %sView on website.%s', 'gravityview' ), '<a href="'.get_permalink( $post_id ).'">', '</a>') . $new_form_text,
241
			/* translators: %s and %s are HTML tags linking to the View on the website */
242
			7  => sprintf(__( 'View saved. %sView on website.%s', 'gravityview' ), '<a href="'.get_permalink( $post_id ).'">', '</a>') . $new_form_text,
243
			8  => __( 'View submitted.', 'gravityview' ),
244
			9  => sprintf(
245
		        /* translators: Date and time the View is scheduled to be published */
246
				__( 'View scheduled for: %1$s.', 'gravityview' ),
247
				// translators: Publish box date format, see http://php.net/date
248
				date_i18n( __( 'M j, Y @ G:i', 'gravityview' ), strtotime( ( isset( $post->post_date ) ? $post->post_date : NULL )  ) )
249
			) . $new_form_text,
250
			/* translators: %s and %s are HTML tags linking to the View on the website */
251
			10  => sprintf(__( 'View draft updated. %sView on website.%s', 'gravityview' ), '<a href="'.get_permalink( $post_id ).'">', '</a>') . $new_form_text,
252
253
			/**
254
			 * These apply to `bulk_post_updated_messages`
255
			 * @file wp-admin/edit.php
256
			 */
257
			'updated'   => _n( '%s View updated.', '%s Views updated.', $bulk_counts['updated'], 'gravityview' ),
258
			'locked'    => _n( '%s View not updated, somebody is editing it.', '%s Views not updated, somebody is editing them.', $bulk_counts['locked'], 'gravityview' ),
259
			'deleted'   => _n( '%s View permanently deleted.', '%s Views permanently deleted.', $bulk_counts['deleted'], 'gravityview' ),
260
			'trashed'   => _n( '%s View moved to the Trash.', '%s Views moved to the Trash.', $bulk_counts['trashed'], 'gravityview' ),
261
			'untrashed' => _n( '%s View restored from the Trash.', '%s Views restored from the Trash.', $bulk_counts['untrashed'], 'gravityview' ),
262
		);
263
264
		return $messages;
265
	}
266
267
268
	/**
269
	 * Get admin notices
270
	 * @deprecated since 1.12
271
	 * @return array
272
	 */
273
	public static function get_notices() {
274
		return GravityView_Admin_Notices::get_notices();
275
	}
276
277
	/**
278
	 * Add a notice to be displayed in the admin.
279
	 * @deprecated since 1.12
280
	 * @param array $notice Array with `class` and `message` keys. The message is not escaped.
281
	 */
282
	public static function add_notice( $notice = array() ) {
283
		GravityView_Admin_Notices::add_notice( $notice );
284
	}
285
286
	/**
287
	 * Check if Gravity Forms plugin is active and show notice if not.
288
	 *
289
	 * @deprecated since 1.12
290
	 * @see GravityView_Compatibility::get_plugin_status()
291
	 * @return boolean True: checks have been passed; GV is fine to run; False: checks have failed, don't continue loading
292
	 */
293
	public static function check_gravityforms() {
294
		return GravityView_Compatibility::check_gravityforms();
295
	}
296
297
	/**
298
	 * Check if specified plugin is active, inactive or not installed
299
	 *
300
	 * @deprecated since 1.12
301
	 * @see GravityView_Compatibility::get_plugin_status()
302
303
	 * @return boolean|string True: plugin is active; False: plugin file doesn't exist at path; 'inactive' it's inactive
304
	 */
305
	static function get_plugin_status( $location = '' ) {
306
		return GravityView_Compatibility::get_plugin_status( $location );
307
	}
308
309
	/**
310
	 * Is the current admin page a GravityView-related page?
311
	 *
312
	 * @todo Convert to use WP_Screen
313
	 * @param string $hook
314
	 * @param null|string $page Optional. String return value of page to compare against.
315
	 *
316
	 * @return bool|string|void If `false`, not a GravityView page. `true` if $page is passed and is the same as current page. Otherwise, the name of the page (`single`, `settings`, or `views`)
317
	 */
318
	static function is_admin_page( $hook = '', $page = NULL ) {
319
		global $current_screen, $plugin_page, $pagenow, $post;
320
321
		if( ! is_admin() ) { return false; }
322
323
		$is_page = false;
324
325
		$is_gv_screen = (!empty($current_screen) && isset($current_screen->post_type) && $current_screen->post_type === 'gravityview');
326
327
		$is_gv_post_type_get = (isset($_GET['post_type']) && $_GET['post_type'] === 'gravityview');
328
329
		$is_gv_settings_get = isset( $_GET['page'] ) && $_GET['page'] === 'gravityview_settings';
330
331
		if( empty( $post ) && $pagenow === 'post.php' && !empty( $_GET['post'] ) ) {
332
			$gv_post = get_post( intval( $_GET['post'] ) );
333
			$is_gv_post_type = (!empty($gv_post) && !empty($gv_post->post_type) && $gv_post->post_type === 'gravityview');
334
		} else {
335
			$is_gv_post_type = (!empty($post) && !empty($post->post_type) && $post->post_type === 'gravityview');
336
		}
337
338
		if( $is_gv_screen || $is_gv_post_type || $is_gv_post_type || $is_gv_post_type_get || $is_gv_settings_get ) {
339
340
			// $_GET `post_type` variable
341
			if(in_array($pagenow, array( 'post.php' , 'post-new.php' )) ) {
342
				$is_page = 'single';
343
			} else if ( in_array( $plugin_page, array( 'gravityview_settings', 'gravityview_page_gravityview_settings' ) ) || ( !empty( $_GET['page'] ) && $_GET['page'] === 'gravityview_settings' ) ) {
344
				$is_page = 'settings';
345
			} else {
346
				$is_page = 'views';
347
			}
348
		}
349
350
		/**
351
		 * @filter `gravityview_is_admin_page` Is the current admin page a GravityView-related page?
352
		 * @param[in,out] string|bool $is_page If false, no. If string, the name of the page (`single`, `settings`, or `views`)
353
		 * @param[in] string $hook The name of the page to check against. Is passed to the method.
354
		 */
355
		$is_page = apply_filters( 'gravityview_is_admin_page', $is_page, $hook );
356
357
		// If the current page is the same as the compared page
358
		if( !empty( $page ) ) {
359
			return $is_page === $page;
360
		}
361
362
		return $is_page;
363
	}
364
365
}
366
367
new GravityView_Admin;
368
369
/**
370
 * Alias for GravityView_Admin::is_admin_page()
371
 *
372
 * @see GravityView_Admin::is_admin_page
373
 *
374
 * @param string $hook
375
 * @param null|string $page Optional. String return value of page to compare against.
376
 *
377
 * @return bool|string|void If `false`, not a GravityView page. `true` if $page is passed and is the same as current page. Otherwise, the name of the page (`single`, `settings`, or `views`)
378
 */
379
function gravityview_is_admin_page($hook = '', $page = NULL) {
380
	return GravityView_Admin::is_admin_page( $hook, $page );
381
}
382