| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\MultiFormGoals\MultiFormGoal; |
| 4 |
|
| 5 |
use Give\MultiFormGoals\MultiFormGoal\Model as MultiFormGoal; |
| 6 |
|
| 7 |
class Shortcode { |
| 8 |
|
| 9 |
/** |
| 10 |
* @since 2.9.6 Extracted from harded-coded value in `addShortcode()`. |
| 11 |
* @var string Shortcode tag to be searched in post content. |
| 12 |
* */ |
| 13 |
protected $tag = 'give_multi_form_goal'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Registers Multi-Form Goal Shortcode |
| 17 |
* |
| 18 |
* @since 2.9.0 |
| 19 |
**/ |
| 20 |
public function addShortcode() { |
| 21 |
add_shortcode( $this->tag, [ $this, 'renderCallback' ] ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Returns Shortcode markup |
| 26 |
* |
| 27 |
* @since 2.9.0 |
| 28 |
**/ |
| 29 |
public function renderCallback( $attributes ) { |
| 30 |
$attributes = $this->parseAttributes( |
| 31 |
[ |
| 32 |
'ids' => [], |
| 33 |
'tags' => [], |
| 34 |
'categories' => [], |
| 35 |
'goal' => '1000', |
| 36 |
'enddate' => '', |
| 37 |
'color' => '#28c77b', |
| 38 |
'heading' => 'Example Heading', |
| 39 |
'image' => GIVE_PLUGIN_URL . 'assets/dist/images/onboarding-preview-form-image.min.jpg', |
| 40 |
'summary' => 'This is a summary.', |
| 41 |
|
| 42 |
], |
| 43 |
$attributes, |
| 44 |
'give_multi_form_goal' |
| 45 |
); |
| 46 |
$multiFormGoal = new MultiFormGoal( |
| 47 |
[ |
| 48 |
'ids' => $attributes['ids'], |
| 49 |
'tags' => $attributes['tags'], |
| 50 |
'categories' => $attributes['categories'], |
| 51 |
'goal' => $attributes['goal'], |
| 52 |
'enddate' => $attributes['enddate'], |
| 53 |
'color' => $attributes['color'], |
| 54 |
'heading' => $attributes['heading'], |
| 55 |
'imageSrc' => $attributes['image'], |
| 56 |
'summary' => $attributes['summary'], |
| 57 |
] |
| 58 |
); |
| 59 |
return $multiFormGoal->getOutput(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Parse multiple attributes with defualt values and types (infered from the default values). |
| 64 |
* @link https://developer.wordpress.org/reference/functions/shortcode_atts/ |
| 65 |
* @since 2.9.6 |
| 66 |
* @param array $pairs Entire list of supported attributes and their defaults. |
| 67 |
* @param array $attributes User defined attributes. |
| 68 |
* @reutrn array |
| 69 |
*/ |
| 70 |
protected function parseAttributes( $pairs, $attributes ) { |
| 71 |
|
| 72 |
if ( $attributes ) { |
| 73 |
foreach ( $attributes as $key => &$attribute ) { |
| 74 |
if ( isset( $pairs[ $key ] ) && is_array( $pairs[ $key ] ) ) { |
| 75 |
$attribute = $this->parseAttributeArray( $attribute ); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return shortcode_atts( $pairs, $attributes, $this->tag ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Parses an individual attributes as an array (or from a comma-separated string). |
| 85 |
* @since 2.9.6 |
| 86 |
* @param string|array $value |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
protected function parseAttributeArray( $value ) { |
| 90 |
if ( ! is_array( $value ) && ! empty( $value ) ) { |
| 91 |
$value = explode( ',', $value ); |
| 92 |
} |
| 93 |
return $value; |
| 94 |
} |
| 95 |
} |
| 96 |
|