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

GravityView_Admin_Add_Shortcode::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 19 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
23
			add_action( 'media_buttons', array( $this, 'add_shortcode_button'), 30);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
24
25
			add_action( 'admin_footer',	array( $this, 'add_shortcode_popup') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
26
27
			// adding styles and scripts
28
			add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts_and_styles') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
42
		global $current_screen, $pagenow;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
43
		return !empty( $current_screen->post_type ) && 'gravityview' != $current_screen->post_type && in_array( $pagenow , array( 'post.php' , 'post-new.php' ) );
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
introduced by
Expected 0 spaces between "'post.php'" and comma; 1 found
Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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() ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
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>
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
Coding Style Comprehensibility introduced by
The string literal Insert View does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
80
		global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
81
82
		if( !$this->is_post_editor_screen() ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
83
			return;
84
		}
85
86
		$post_type = get_post_type_object($post->post_type);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
87
88
		$views = get_posts( array('post_type' => 'gravityview', 'posts_per_page' => -1 ) );
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
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
introduced by
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>
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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;
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
112
								echo '<option value="'. $view->ID .'">'. esc_html( sprintf('%s #%d', $title, $view->ID ) ) .'</option>';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$view'
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
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' );
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
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" />
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
138
						<input class="button button-secondary alignright" type="submit" onclick="tb_remove(); return false;" value="<?php esc_attr_e("Cancel", 'gravityview'); ?>" />
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
Coding Style Comprehensibility introduced by
The string literal Cancel does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
174
		wp_enqueue_style( 'gravityview_postedit_styles' );
175
176
		$script_debug = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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 );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
180
		wp_enqueue_script( 'gravityview_postedit_scripts' );
181
		wp_localize_script('gravityview_postedit_scripts', 'gvGlobals', array(
182
			'nonce' => wp_create_nonce( 'gravityview_ajaxaddshortcode'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
183
			'loading_text' => esc_html__( 'Loading&hellip;', 'gravityview' ),
184
			'alert_1' => esc_html__( 'Please select a View', 'gravityview'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
199
200
		// Not properly formatted request
201
		if ( empty( $_POST['viewid'] ) || !is_numeric( $_POST['viewid'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
202
			exit( false );
0 ignored issues
show
Coding Style Compatibility introduced by
The method get_sortable_fields() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
203
		}
204
205
		// Not valid request
206
		if( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'gravityview_ajaxaddshortcode' ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
207
			exit( false );
0 ignored issues
show
Coding Style Compatibility introduced by
The method get_sortable_fields() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
208
		}
209
210
		$viewid = (int)$_POST['viewid'];
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
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 );
0 ignored issues
show
Coding Style Compatibility introduced by
The method get_sortable_fields() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
222
	}
223
224
}
225
226
new GravityView_Admin_Add_Shortcode;
227