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