Shortcode.php
118 lines
| 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 3.7.0 Sanitize attributes |
| 30 | * @since 3.0.3 Use static function on array_map callback to pass the id as reference for _give_redirect_form_id to prevent warnings on PHP 8.0.1 or plus |
| 31 | * @since 2.9.0 |
| 32 | **/ |
| 33 | public function renderCallback($attributes) |
| 34 | { |
| 35 | $attributes = give_clean($attributes); |
| 36 | |
| 37 | $attributes = $this->parseAttributes( |
| 38 | [ |
| 39 | 'ids' => [], |
| 40 | 'tags' => [], |
| 41 | 'categories' => [], |
| 42 | 'goal' => '1000', |
| 43 | 'enddate' => '', |
| 44 | 'color' => '#28c77b', |
| 45 | 'heading' => 'Example Heading', |
| 46 | 'image' => GIVE_PLUGIN_URL . 'build/assets/dist/images/onboarding-preview-form-image.min.jpg', |
| 47 | 'summary' => 'This is a summary.', |
| 48 | |
| 49 | ], |
| 50 | $attributes, |
| 51 | 'give_multi_form_goal' |
| 52 | ); |
| 53 | |
| 54 | $multiFormGoal = new MultiFormGoal( |
| 55 | [ |
| 56 | 'ids' => array_map( |
| 57 | static function ($id) { |
| 58 | _give_redirect_form_id($id); |
| 59 | |
| 60 | return $id; |
| 61 | }, |
| 62 | $attributes['ids'] |
| 63 | ), |
| 64 | 'tags' => $attributes['tags'], |
| 65 | 'categories' => $attributes['categories'], |
| 66 | 'goal' => $attributes['goal'], |
| 67 | 'enddate' => $attributes['enddate'], |
| 68 | 'color' => $attributes['color'], |
| 69 | 'heading' => $attributes['heading'], |
| 70 | 'imageSrc' => $attributes['image'], |
| 71 | 'summary' => $attributes['summary'], |
| 72 | ] |
| 73 | ); |
| 74 | |
| 75 | return $multiFormGoal->getOutput(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Parse multiple attributes with defualt values and types (infered from the default values). |
| 80 | * @link https://developer.wordpress.org/reference/functions/shortcode_atts/ |
| 81 | * @since 2.9.6 |
| 82 | * |
| 83 | * @param array $pairs Entire list of supported attributes and their defaults. |
| 84 | * @param array $attributes User defined attributes. |
| 85 | * |
| 86 | * @reutrn array |
| 87 | */ |
| 88 | protected function parseAttributes($pairs, $attributes) |
| 89 | { |
| 90 | if ($attributes) { |
| 91 | foreach ($attributes as $key => &$attribute) { |
| 92 | if (isset($pairs[$key]) && is_array($pairs[$key])) { |
| 93 | $attribute = $this->parseAttributeArray($attribute); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return shortcode_atts($pairs, $attributes, $this->tag); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Parses an individual attributes as an array (or from a comma-separated string). |
| 103 | * @since 2.9.6 |
| 104 | * |
| 105 | * @param string|array $value |
| 106 | * |
| 107 | * @return array |
| 108 | */ |
| 109 | protected function parseAttributeArray($value) |
| 110 | { |
| 111 | if ( ! is_array($value) && ! empty($value)) { |
| 112 | $value = explode(',', $value); |
| 113 | } |
| 114 | |
| 115 | return $value; |
| 116 | } |
| 117 | } |
| 118 |