PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.5.7
GiveWP – Donation Plugin and Fundraising Platform v2.5.7
4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / admin / shortcodes / abstract-shortcode-generator.php
give / includes / admin / shortcodes Last commit date
abstract-shortcode-generator.php 6 years ago admin-shortcodes.js 7 years ago class-shortcode-button.php 6 years ago mce-plugin.js 8 years ago shortcode-give-donation-grid.php 6 years ago shortcode-give-donation-history.php 6 years ago shortcode-give-donor-wall.php 6 years ago shortcode-give-form.php 6 years ago shortcode-give-goal.php 6 years ago shortcode-give-login.php 6 years ago shortcode-give-profile-editor.php 6 years ago shortcode-give-receipt.php 6 years ago shortcode-give-register.php 6 years ago shortcode-give-totals.php 6 years ago
abstract-shortcode-generator.php
469 lines
1 <?php
2 /**
3 * Shortcode Dialog Generator abstract class
4 *
5 * @package Give/Admin
6 * @author Paul Ryley
7 * @copyright Copyright (c) 2016, GiveWP
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @version 1.0
10 * @since 1.3
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Class Give_Shortcode_Generator
20 */
21 abstract class Give_Shortcode_Generator {
22
23 /**
24 * The current class name
25 *
26 * @since 1.0
27 */
28 public $self;
29
30 /**
31 * The current shortcode
32 *
33 * @since 1.0
34 */
35 public $shortcode;
36
37 /**
38 * The current shortcode tag
39 *
40 * @since 1.0
41 */
42 public $shortcode_tag;
43
44 /**
45 * Shortcode field errors
46 *
47 * @since 1.0
48 */
49 protected $errors;
50
51 /**
52 * Required shortcode fields
53 *
54 * @since 1.0
55 */
56 protected $required;
57
58 /**
59 * Class constructor
60 *
61 * @param string $shortcode The shortcode tag
62 *
63 * @since 1.0
64 */
65 public function __construct( $shortcode ) {
66
67 $this->shortcode_tag = $shortcode;
68
69 add_action( 'admin_init', array( $this, 'init' ) );
70
71 }
72
73 /**
74 * Kick things off for the shortcode generator
75 *
76 * @since 1.3.0.2
77 */
78 public function init() {
79
80 if ( $this->shortcode_tag ) {
81
82 $this->self = get_class( $this );
83
84 $this->errors = array();
85 $this->required = array();
86
87 // Generate the fields, errors, and requirements
88 $fields = $this->get_fields();
89
90 $defaults = array(
91 'btn_close' => esc_html__( 'Close', 'give' ),
92 'btn_okay' => esc_html__( 'Insert Shortcode', 'give' ),
93 'errors' => $this->errors,
94 'fields' => $fields,
95 'label' => '[' . $this->shortcode_tag . ']',
96 'required' => $this->required,
97 'title' => esc_html__( 'Insert Shortcode', 'give' ),
98 );
99
100 if ( user_can_richedit() ) {
101
102 Give_Shortcode_Button::$shortcodes[ $this->shortcode_tag ] = wp_parse_args( $this->shortcode, $defaults );
103
104 }
105 }
106
107 }
108
109
110 /**
111 * Define the shortcode attribute fields
112 *
113 * @return false|array
114 *
115 * @since 1.0
116 */
117 public function define_fields() {
118
119 return false;
120 }
121
122 /**
123 * Generate the shortcode dialog fields
124 *
125 * @param array $defined_fields
126 *
127 * @return array
128 *
129 * @since 1.0
130 */
131 protected function generate_fields( $defined_fields ) {
132
133 $fields = array();
134
135 if ( is_array( $defined_fields ) ) {
136
137 foreach ( $defined_fields as $field ) {
138
139 $defaults = array(
140 'label' => false,
141 'name' => false,
142 'options' => array(),
143 'placeholder' => false,
144 'tooltip' => false,
145 'type' => '',
146 );
147
148 $field = wp_parse_args( (array) $field, $defaults );
149 $method = 'generate_' . strtolower( $field['type'] );
150
151 if ( method_exists( $this, $method ) ) {
152
153 $field = call_user_func( array( $this, $method ), $field );
154
155 if ( $field ) {
156 $fields[] = $field;
157 }
158 }
159 }
160 }
161
162 return $fields;
163 }
164
165 /**
166 * Get the generated shortcode dialog fields
167 *
168 * @return array
169 *
170 * @since 1.0
171 */
172 protected function get_fields() {
173
174 $defined_fields = $this->define_fields();
175 $generated_fields = $this->generate_fields( $defined_fields );
176
177 $errors = array();
178
179 if ( ! empty( $this->errors ) ) {
180 foreach ( $this->required as $name => $alert ) {
181 // Using WordPress function in place of array_column wp_list_pluck as it support older version as well.
182 if ( false === array_search( $name, give_list_pluck( $generated_fields, 'name' ) ) ) {
183
184 $errors[] = $this->errors[ $name ];
185 }
186 }
187
188 $this->errors = $errors;
189 }
190
191 if ( ! empty( $errors ) ) {
192
193 return $errors;
194 }
195
196 return $generated_fields;
197 }
198
199
200 /**
201 * Generate a TinyMCE docs_link field
202 *
203 * @param $field
204 *
205 * @return array
206 */
207 protected function generate_docs_link( $field ){
208 // Add custom style to override mce style.
209 $dashicon_style = 'width: 20px;
210 height: 20px;
211 font-size: 17px;
212 line-height: 1;
213 font-family: dashicons;
214 text-decoration: inherit;
215 font-weight: normal;
216 font-style: normal;
217 vertical-align: top;
218 text-align: center;
219 transition: color .1s ease-in 0;';
220
221 $a_style = 'color: #999;
222 text-decoration: none;
223 font-style: italic;
224 font-size: 13px;';
225
226 $p_style = 'text-align:right;';
227
228 return $this->generate_container(
229 array(
230 'type' => 'container',
231 'html' => sprintf(
232 '<p class="give-docs-link" style="%5$s"><a href="%4$s" style="%3$s" target="_blank">%1$s<span class="dashicons dashicons-editor-help" style="%2$s"></a></span></p>',
233 $field['text'],
234 $dashicon_style,
235 $a_style,
236 esc_url( $field['link'] ),
237 $p_style
238 )
239 )
240 );
241 }
242
243 /**
244 * Generate a TinyMCE container field
245 *
246 * @param array $field
247 *
248 * @return array|false
249 *
250 * @since 1.0
251 */
252 protected function generate_container( $field ) {
253
254 if ( array_key_exists( 'html', $field ) ) {
255
256 return array(
257 'type' => $field['type'],
258 'html' => $field['html'],
259 );
260 }
261
262 return false;
263 }
264
265 /**
266 * Generate a TinyMCE listbox field
267 *
268 * @param array $field
269 *
270 * @return array|false
271 *
272 * @since 1.0
273 */
274 protected function generate_listbox( $field ) {
275
276 $listbox = shortcode_atts( array(
277 'label' => '',
278 'minWidth' => '',
279 'name' => false,
280 'tooltip' => '',
281 'type' => '',
282 'value' => '',
283 'classes' => ''
284 ), $field );
285
286 if ( $this->validate( $field ) ) {
287
288 $new_listbox = array();
289
290 foreach ( $listbox as $key => $value ) {
291
292 if ( $key == 'value' && empty( $value ) ) {
293 $new_listbox[ $key ] = $listbox['name'];
294 } else if ( $value ) {
295 $new_listbox[ $key ] = $value;
296 }
297 }
298
299 // do not reindex array!
300 $field['options'] = array(
301 '' => ( $field['placeholder'] ? $field['placeholder'] : esc_attr__( '- Select -', 'give' ) ),
302 ) + $field['options'];
303
304 foreach ( $field['options'] as $value => $text ) {
305 $new_listbox['values'][] = array(
306 'text' => $text,
307 'value' => $value,
308 );
309 }
310
311 return $new_listbox;
312 }
313
314 return false;
315 }
316
317 /**
318 * Generate a TinyMCE listbox field for a post_type.
319 *
320 * @param array $field
321 *
322 * @return array|false
323 *
324 * @since 1.0
325 */
326 protected function generate_post( $field ) {
327
328 $args = array(
329 'post_type' => 'post',
330 'orderby' => 'title',
331 'order' => 'ASC',
332 'posts_per_page' => 30,
333 'suppress_filters' => false,
334 );
335
336 $args = wp_parse_args( (array) $field['query_args'], $args );
337
338 $posts = get_posts( $args );
339 $options = array();
340
341 if ( ! empty( $posts ) ) {
342 foreach ( $posts as $post ) {
343 $options[ absint( $post->ID ) ] = empty( $post->post_title )
344 ? sprintf( __( 'Untitled (#%s)', 'give' ), $post->ID )
345 /** This filter is documented in wp-includes/post-template.php */
346 : apply_filters( 'the_title', $post->post_title, $post->ID );
347 }
348
349 $field['type'] = 'listbox';
350 $field['options'] = $options;
351
352 return $this->generate_listbox( $field );
353 }
354
355 // perform validation here before returning false
356 $this->validate( $field );
357
358 return false;
359 }
360
361 /**
362 * Generate a TinyMCE textbox field
363 *
364 * @param array $field
365 *
366 * @return array|false
367 *
368 * @since 1.0
369 */
370 protected function generate_textbox( $field ) {
371
372 $textbox = shortcode_atts( array(
373 'label' => '',
374 'maxLength' => '',
375 'minHeight' => '',
376 'minWidth' => '',
377 'multiline' => false,
378 'name' => false,
379 'tooltip' => '',
380 'type' => '',
381 'value' => '',
382 'classes' => '',
383 'placeholder' => ''
384 ), $field );
385
386 // Remove empty placeholder.
387 if( empty( $textbox['placeholder'] ) ) {
388 unset( $textbox['placeholder'] );
389 }
390
391 if ( $this->validate( $field ) ) {
392 return array_filter( $textbox, array( $this, 'return_textbox_value' ) );
393 }
394
395 return false;
396 }
397
398 /**
399 * Validate Textbox Value
400 *
401 * @param $value
402 *
403 * @return bool
404 */
405 function return_textbox_value( $value ) {
406 return $value !== '';
407 }
408
409 /**
410 * Perform validation for a single field
411 *
412 * Returns true or false depending on whether the field has a 'name' attribute.
413 * This method also populates the shortcode's $errors and $required arrays.
414 *
415 * @param array $field
416 *
417 * @return bool
418 *
419 * @since 1.0
420 */
421 protected function validate( $field ) {
422
423 extract( shortcode_atts(
424 array(
425 'name' => false,
426 'required' => false,
427 'label' => '',
428 ), $field )
429 );
430
431 if ( $name ) {
432
433 if ( isset( $required['error'] ) ) {
434
435 $error = array(
436 'type' => 'container',
437 'html' => $required['error'],
438 );
439
440 $this->errors[ $name ] = $this->generate_container( $error );
441 }
442
443 if ( ! ! $required || is_array( $required ) ) {
444
445 $alert = esc_html__( 'Some of the shortcode options are required.', 'give' );
446
447 if ( isset( $required['alert'] ) ) {
448
449 $alert = $required['alert'];
450
451 } else if ( ! empty( $label ) ) {
452
453 $alert = sprintf(
454 /* translators: %s: option label */
455 esc_html__( 'The "%s" option is required.', 'give' ),
456 str_replace( ':', '', $label )
457 );
458 }
459
460 $this->required[ $name ] = $alert;
461 }
462
463 return true;
464 }
465
466 return false;
467 }
468 }
469