Test Failed
Push — issues/2980 ( 6931f9...aadbca )
by Ravinder
05:37
created

Give_Shortcode_Button::is_add_button()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 2
nop 0
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
/**
3
 * Shortcode Button Class
4
 *
5
 * @package     Give
6
 * @subpackage  Admin
7
 * @author      Paul Ryley
8
 * @copyright   Copyright (c) 2016, WordImpress
9
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
10
 * @version     1.0
11
 * @since       1.3.0
12
 */
13
14
// Exit if accessed directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
/**
20
 * Class Give_Shortcode_Button
21
 */
22
final class Give_Shortcode_Button {
23
24
	/**
25
	 * All shortcode tags
26
	 *
27
	 * @since 1.0
28
	 */
29
	public static $shortcodes;
30
31
	/**
32
	 * Class constructor
33
	 */
34
	public function __construct() {
35
		add_action( 'admin_init', array( $this, 'init'), 999 );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
36
	}
37
38
	/**
39
	 * Initialize
40
	 *
41
	 * @since 2.1.0
42
	 * @access public
43
	 */
44
	public function init(){
45
		if ( $this->is_add_button() ) {
46
			add_filter( 'mce_external_plugins', array( $this, 'mce_external_plugins' ), 15 );
47
48
			add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_assets' ) );
49
			add_action( 'admin_enqueue_scripts', array( $this, 'admin_localize_scripts' ), 13 );
50
			add_action( 'media_buttons', array( $this, 'shortcode_button' ) );
51
		}
52
53
		add_action( "wp_ajax_give_shortcode", array( $this, 'shortcode_ajax' ) );
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal wp_ajax_give_shortcode 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...
54
		add_action( "wp_ajax_nopriv_give_shortcode", array( $this, 'shortcode_ajax' ) );
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal wp_ajax_nopriv_give_shortcode 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...
55
	}
56
57
	/**
58
	 * Register any TinyMCE plugins
59
	 *
60
	 * @param array $plugin_array
61
	 *
62
	 * @return array|bool
63
	 *
64
	 * @since 1.0
65
	 */
66
	public function mce_external_plugins( $plugin_array ) {
67
68
		if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
69
			return false;
70
		}
71
72
		$plugin_array['give_shortcode'] = GIVE_PLUGIN_URL . 'includes/admin/shortcodes/mce-plugin.js';
73
74
		return $plugin_array;
75
	}
76
77
	/**
78
	 * Enqueue the admin assets
79
	 *
80
	 * @return void
81
	 *
82
	 * @since 1.0
83
	 */
84
	public function admin_enqueue_assets() {
85
		$direction = ( is_rtl() || isset( $_GET['d'] ) && 'rtl' === $_GET['d'] ) ? '.rtl' : '';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
86
87
		wp_enqueue_script(
88
			'give_shortcode',
89
			GIVE_PLUGIN_URL . 'includes/admin/shortcodes/admin-shortcodes.js',
90
			array( 'jquery' ),
91
			GIVE_VERSION,
92
			true
93
		);
94
95
		wp_enqueue_style(
96
			'give-admin-shortcode-button-style',
97
			GIVE_PLUGIN_URL . 'assets/dist/css/admin-shortcode-button' . $direction . '.css',
98
			array(),
99
			GIVE_VERSION
100
		);
101
	}
102
103
	/**
104
	 * Localize the admin scripts
105
	 *
106
	 * @return void
107
	 *
108
	 * @since 1.0
109
	 */
110
	public function admin_localize_scripts() {
111
112
		if ( ! empty( self::$shortcodes ) ) {
113
114
			$variables = array();
115
116
			foreach ( self::$shortcodes as $shortcode => $values ) {
117
				if ( ! empty( $values['required'] ) ) {
118
					$variables[ $shortcode ] = $values['required'];
119
				}
120
			}
121
122
			wp_localize_script( 'give_shortcode', 'scShortcodes', $variables );
123
		}
124
	}
125
126
	/**
127
	 * Adds the "Donation Form" button above the TinyMCE Editor on add/edit screens.
128
	 *
129
	 * @return string|bool
130
	 *
131
	 * @since 1.0
132
	 */
133
	public function shortcode_button() {
134
135
		$shortcodes = array();
136
137
		foreach ( self::$shortcodes as $shortcode => $values ) {
138
139
			/**
140
			 * Filters the condition for including the current shortcode
141
			 *
142
			 * @since 1.0
143
			 */
144
			if ( apply_filters( sanitize_title( $shortcode ) . '_condition', true ) ) {
145
146
				$shortcodes[ $shortcode ] = sprintf(
147
					'<div class="sc-shortcode mce-menu-item give-shortcode-item-%1$s" data-shortcode="%2$s">%3$s</div>',
148
					$shortcode,
149
					$shortcode,
150
					$values['label']
151
				);
152
			}
153
		}
154
155
		if ( ! empty( $shortcodes ) ) {
156
157
			// check current WP version
158
			$img = ( version_compare( get_bloginfo( 'version' ), '3.5', '<' ) )
159
				? '<img src="' . GIVE_PLUGIN_URL . 'assets/dist/images/give-media.png" />'
160
				: '<span class="wp-media-buttons-icon" id="give-media-button" style="background-image: url(' . give_svg_icons( 'give_grey' ) . ');"></span>';
161
162
			reset( $shortcodes );
163
164
			if ( 1 === count( $shortcodes ) ) {
165
166
				$shortcode = key( $shortcodes );
167
168
				printf(
169
					'<button type="button" class="button sc-shortcode" data-shortcode="%s">%s</button>',
170
					$shortcode,
171
					sprintf( '%s %s %s',
172
						$img,
173
						__( 'Insert', 'give' ),
174
						self::$shortcodes[ $shortcode ]['label']
175
					)
176
				);
177
			} else {
178
				printf(
179
					'<div class="sc-wrap">' .
180
					'<button class="button sc-button" type="button">%s %s</button>' .
181
					'<div class="sc-menu mce-menu">%s</div>' .
182
					'</div>',
183
					$img,
184
					__( 'Give Shortcodes', 'give' ),
185
					implode( '', array_values( $shortcodes ) )
186
				);
187
			}
188
		}
189
	}
190
191
	/**
192
	 * Load the shortcode dialog fields via AJAX
193
	 *
194
	 * @return void
195
	 *
196
	 * @since 1.0
197
	 */
198
	public function shortcode_ajax() {
199
200
		$shortcode = isset( $_POST['shortcode'] ) ? $_POST['shortcode'] : false;
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...
201
		$response  = false;
202
203
		if ( $shortcode && array_key_exists( $shortcode, self::$shortcodes ) ) {
204
205
			$data = self::$shortcodes[ $shortcode ];
206
207
			if ( ! empty( $data['errors'] ) ) {
208
				$data['btn_okay'] = array( esc_html__( 'Okay', 'give' ) );
209
			}
210
211
			$response = array(
212
				'body'      => $data['fields'],
213
				'close'     => $data['btn_close'],
214
				'ok'        => $data['btn_okay'],
215
				'shortcode' => $shortcode,
216
				'title'     => $data['title'],
217
			);
218
		} else {
219
			// todo: handle error
220
			error_log( print_r( 'AJAX error!', 1 ) );
0 ignored issues
show
introduced by
The use of function print_r() is discouraged
Loading history...
221
		}
222
223
		wp_send_json( $response );
224
	}
225
226
227
	/**
228
	 * Flag to check add shortcode button to current screen or not
229
	 *
230
	 * @since  2.1.0
231
	 * @access private
232
	 * @return bool
233
	 */
234
	private function is_add_button() {
235
		global $pagenow;
236
237
		$shortcode_button_pages = apply_filters( 'give_shortcode_button_pages', array(
238
			'post.php',
239
			'page.php',
240
			'post-new.php',
241
			'post-edit.php',
242
			'edit.php',
243
			'edit.php?post_type=page',
244
		) );
245
246
		// Only run in admin post/page creation and edit screens
247
		if (
248
			! is_admin()
249
			|| ! in_array( $pagenow, $shortcode_button_pages )
250
			|| ! apply_filters( 'give_shortcode_button_condition', true )
251
			|| empty( self::$shortcodes )
252
		) {
253
			return false;
254
		}
255
256
		return true;
257
	}
258
}
259
260
new Give_Shortcode_Button;
261