Completed
Push — master ( 2cfa6f...8927a4 )
by Zack
10:00 queued 06:05
created

includes/class-admin-add-shortcode.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
 * Adds a button to add the View shortcode into the post content
4
 *
5
 * @package   GravityView
6
 * @license   GPL2+
7
 * @author    Katz Web Services, Inc.
8
 * @link      http://gravityview.co
9
 * @copyright Copyright 2014, Katz Web Services, Inc.
10
 *
11
 * @since 1.0.0
12
 */
13
14
/** If this file is called directly, abort. */
15
if ( ! defined( 'ABSPATH' ) ) {
16
	die;
17
}
18
19
class GravityView_Admin_Add_Shortcode {
20
21
	function __construct() {
22
23
			add_action( 'media_buttons', array( $this, 'add_shortcode_button'), 30);
24
25
			add_action( 'admin_footer',	array( $this, 'add_shortcode_popup') );
26
27
			// adding styles and scripts
28
			add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts_and_styles') );
29
30
			// ajax - populate sort fields based on the selected view
31
			add_action( 'wp_ajax_gv_sortable_fields', array( $this, 'get_sortable_fields' ) );
32
	}
33
34
35
	/**
36
	 * check if screen post editor and is not related with post type 'gravityview'
37
	 *
38
	 * @access public
39
	 * @return bool
40
	 */
41
	function is_post_editor_screen() {
42
		global $current_screen, $pagenow;
43
		return !empty( $current_screen->post_type ) && 'gravityview' != $current_screen->post_type && in_array( $pagenow , array( 'post.php' , 'post-new.php' ) );
44
	}
45
46
47
	/**
48
	 * Add shortcode button to the Add Media right
49
	 *
50
	 * @access public
51
	 * @return void
52
	 */
53
	function add_shortcode_button() {
54
55
		/**
56
		 * @since 1.15.3
57
		 */
58
		if( ! GVCommon::has_cap( array( 'publish_gravityviews' ) ) ) {
59
			return;
60
		}
61
62
		if( !$this->is_post_editor_screen() ) {
63
			return;
64
		}
65
		?>
66
		<a href="#TB_inline?width=600&amp;height=800&amp;inlineId=select_gravityview_view" class="thickbox hide-if-no-js button gform_media_link" id="add_gravityview" title="<?php esc_attr_e("Insert View", 'gravityview'); ?>"><span class="icon gv-icon-astronaut-head"></span><?php esc_html_e( 'Add View', 'gravityview' ); ?></a>
67
		<?php
68
69
	}
70
71
72
73
	/**
74
	 * Add shortcode popup div
75
	 *
76
	 * @access public
77
	 * @return void
78
	 */
79
	function add_shortcode_popup() {
80
		global $post;
81
82
		if( !$this->is_post_editor_screen() ) {
83
			return;
84
		}
85
86
		$post_type = get_post_type_object($post->post_type);
87
88
		$views = get_posts( array('post_type' => 'gravityview', 'posts_per_page' => -1 ) );
89
90
		// If there are no views set up yet, we get outta here.
91
		if( empty( $views ) ) {
92
			echo '<div id="select_gravityview_view"><div class="wrap">' . GravityView_Admin::no_views_text() . '</div></div>';
0 ignored issues
show
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'GravityView_Admin'
Loading history...
93
			return;
94
		}
95
96
		?>
97
		<div id="select_gravityview_view">
98
			<form action="#" method="get" id="select_gravityview_view_form">
99
				<div class="wrap">
100
101
					<h2 class=""><?php esc_html_e( 'Embed a View', 'gravityview' ); ?></h2>
102
					<p class="subtitle"><?php printf( esc_attr ( __( 'Use this form to embed a View into this %s. %sLearn more about using shortcodes.%s', 'gravityview') ), $post_type->labels->singular_name, '<a href="http://docs.gravityview.co/article/73-using-the-shortcode" target="_blank" rel="noopener noreferrer">', '</a>' ); ?></p>
103
104
					<div>
105
						<h3><label for="gravityview_id"><?php esc_html_e( 'Select a View', 'gravityview' ); ?></label></h3>
106
107
						<select name="gravityview_id" id="gravityview_id">
108
							<option value=""><?php esc_html_e( '&mdash; Select a View to Insert &mdash;', 'gravityview' ); ?></option>
109
							<?php
110
							foreach( $views as $view ) {
111
								$title = empty( $view->post_title ) ? __('(no title)', 'gravityview') : $view->post_title;
112
								echo '<option value="'. $view->ID .'">'. esc_html( sprintf('%s #%d', $title, $view->ID ) ) .'</option>';
113
							}
114
							?>
115
						</select>
116
					</div>
117
118
					<table class="form-table hide-if-js">
119
120
						<caption><?php esc_html_e( 'View Settings', 'gravityview' ); ?></caption>
121
122
						<?php
123
124
						$settings = GravityView_View_Data::get_default_args( true );
125
126
						foreach ( $settings as $key => $setting ) {
127
128
							if( empty( $setting['show_in_shortcode'] ) ) { continue; }
129
130
							GravityView_Render_Settings::render_setting_row( $key, array(), NULL, 'gravityview_%s', 'gravityview_%s' );
131
						}
132
						?>
133
134
					</table>
135
136
					<div class="submit">
137
						<input type="submit" class="button button-primary button-large alignleft hide-if-js" value="<?php esc_attr_e('Insert View', 'gravityview' ); ?>" id="insert_gravityview_view" />
138
						<input class="button button-secondary alignright" type="submit" onclick="tb_remove(); return false;" value="<?php esc_attr_e("Cancel", 'gravityview'); ?>" />
139
					</div>
140
141
				</div>
142
			</form>
143
		</div>
144
		<?php
145
146
	}
147
148
149
150
151
	/**
152
	 * Enqueue scripts and styles
153
	 *
154
	 * @access public
155
	 * @return void
156
	 */
157
	function add_scripts_and_styles() {
158
159
		if( ! $this->is_post_editor_screen() ) {
160
			return;
161
		}
162
163
		wp_enqueue_style( 'dashicons' );
164
165
		// date picker
166
		wp_enqueue_script( 'jquery-ui-datepicker' );
167
168
		$protocol = is_ssl() ? 'https://' : 'http://';
169
170
		wp_enqueue_style( 'jquery-ui-datepicker', $protocol.'ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/smoothness/jquery-ui.css', array(), GravityView_Plugin::version );
171
172
		//enqueue styles
173
		wp_register_style( 'gravityview_postedit_styles', plugins_url('assets/css/admin-post-edit.css', GRAVITYVIEW_FILE), array(), GravityView_Plugin::version );
174
		wp_enqueue_style( 'gravityview_postedit_styles' );
175
176
		$script_debug = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
177
178
		// custom js
179
		wp_register_script( 'gravityview_postedit_scripts',  plugins_url('assets/js/admin-post-edit'.$script_debug.'.js', GRAVITYVIEW_FILE), array( 'jquery', 'jquery-ui-datepicker' ), GravityView_Plugin::version );
180
		wp_enqueue_script( 'gravityview_postedit_scripts' );
181
		wp_localize_script('gravityview_postedit_scripts', 'gvGlobals', array(
182
			'nonce' => wp_create_nonce( 'gravityview_ajaxaddshortcode'),
183
			'loading_text' => esc_html__( 'Loading&hellip;', 'gravityview' ),
184
			'alert_1' => esc_html__( 'Please select a View', 'gravityview'),
185
		));
186
187
	}
188
189
190
191
	/**
192
	 * Ajax
193
	 * Given a View id, calculates the assigned form, and returns the form fields (only the sortable ones )
194
	 *
195
	 * @access public
196
	 * @return void
197
	 */
198
	function get_sortable_fields() {
199
200
		// Not properly formatted request
201
		if ( empty( $_POST['viewid'] ) || !is_numeric( $_POST['viewid'] ) ) {
202
			exit( false );
203
		}
204
205
		// Not valid request
206
		if( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'gravityview_ajaxaddshortcode' ) ) {
207
			exit( false );
208
		}
209
210
		$viewid = (int)$_POST['viewid'];
211
212
		// fetch form id assigned to the view
213
		$formid = gravityview_get_form_id( $viewid );
214
215
		// Get the default sort field for the view
216
		$sort_field = gravityview_get_template_setting( $viewid, 'sort_field' );
217
218
		// Generate the output `<option>`s
219
		$response = gravityview_get_sortable_fields( $formid, $sort_field );
220
221
		exit( $response );
222
	}
223
224
}
225
226
new GravityView_Admin_Add_Shortcode;
227