Shortcode.php
95 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 | * @since 2.10.0 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 | error_log( serialize( $attributes ) ); |
| 31 | |
| 32 | $attributes = $this->parseAttributes( |
| 33 | [ |
| 34 | 'ids' => [], |
| 35 | 'tags' => [], |
| 36 | 'categories' => [], |
| 37 | 'goal' => '1000', |
| 38 | 'enddate' => '', |
| 39 | 'color' => '#28c77b', |
| 40 | 'heading' => 'Example Heading', |
| 41 | 'image' => GIVE_PLUGIN_URL . 'assets/dist/images/onboarding-preview-form-image.min.jpg', |
| 42 | 'summary' => 'This is a summary.', |
| 43 | |
| 44 | ], |
| 45 | $attributes, |
| 46 | 'give_multi_form_goal' |
| 47 | ); |
| 48 | $multiFormGoal = new MultiFormGoal( |
| 49 | [ |
| 50 | 'ids' => $attributes['ids'], |
| 51 | 'tags' => $attributes['tags'], |
| 52 | 'categories' => $attributes['categories'], |
| 53 | 'goal' => $attributes['goal'], |
| 54 | 'enddate' => $attributes['enddate'], |
| 55 | 'color' => $attributes['color'], |
| 56 | 'heading' => $attributes['heading'], |
| 57 | 'imageSrc' => $attributes['image'], |
| 58 | 'summary' => $attributes['summary'], |
| 59 | ] |
| 60 | ); |
| 61 | return $multiFormGoal->getOutput(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Parse multiple attributes with defualt values and types (infered from the default values). |
| 66 | * @link https://developer.wordpress.org/reference/functions/shortcode_atts/ |
| 67 | * @since 2.10.0 |
| 68 | * @param array $pairs Entire list of supported attributes and their defaults. |
| 69 | * @param array $attributes User defined attributes. |
| 70 | * @reutrn array |
| 71 | */ |
| 72 | protected function parseAttributes( $pairs, $attributes ) { |
| 73 | foreach ( $attributes as $key => &$attribute ) { |
| 74 | if ( is_array( $pairs[ $key ] ) ) { |
| 75 | $attribute = $this->parseAttributeArray( $attribute ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return shortcode_atts( $pairs, $attributes, $this->tag ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Parses an individual attributes as an array (or from a comma-separated string). |
| 84 | * @since 2.10.0 |
| 85 | * @param string|array $value |
| 86 | * @return array |
| 87 | */ |
| 88 | protected function parseAttributeArray( $value ) { |
| 89 | if ( ! is_array( $value ) ) { |
| 90 | $value = explode( ',', $value ); |
| 91 | } |
| 92 | return $value; |
| 93 | } |
| 94 | } |
| 95 |