ActionsGenerator.php
9 months ago
AddRatingGenerator.php
1 year ago
CourseMetaGenerator.php
9 months ago
ElementGenerator.php
1 year ago
MaterialGenerator.php
1 year ago
Preview.php
1 year ago
PriceGenerator.php
9 months ago
SocialLinkGenerator.php
1 year ago
ThumbnailGenerator.php
1 year ago
Preview.php
210 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Preview script for html markup generator |
| 5 | * |
| 6 | * @package tutor-droip-elements |
| 7 | */ |
| 8 | |
| 9 | namespace TutorLMSDroip\ElementGenerator; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; // Exit if accessed directly. |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Class Preview |
| 17 | * This class is used to define all preview functions. |
| 18 | */ |
| 19 | class Preview { |
| 20 | |
| 21 | use CourseMetaGenerator; |
| 22 | use AddRatingGenerator; |
| 23 | use MaterialGenerator; |
| 24 | use ActionsGenerator; |
| 25 | use PriceGenerator; |
| 26 | use SocialLinkGenerator; |
| 27 | use ThumbnailGenerator; |
| 28 | |
| 29 | /** |
| 30 | * Droip element object |
| 31 | * |
| 32 | * @var array $element | element data. |
| 33 | */ |
| 34 | private $element = array(); |
| 35 | /** |
| 36 | * Droip all elements object |
| 37 | * |
| 38 | * @var array $elements | array of elements. |
| 39 | */ |
| 40 | private $elements = array(); |
| 41 | /** |
| 42 | * Droip all style blocks object |
| 43 | * |
| 44 | * @var array $style_blocks | array of style blocks. |
| 45 | */ |
| 46 | private $style_blocks = array(); |
| 47 | /** |
| 48 | * Droip all style blocks object |
| 49 | * |
| 50 | * @var array $style_blocks | array of style blocks. |
| 51 | */ |
| 52 | private $attributes = array(); |
| 53 | /** |
| 54 | * Droip all options object |
| 55 | * |
| 56 | * @var array $options | array of options. |
| 57 | */ |
| 58 | private $options = array(); |
| 59 | /** |
| 60 | * Droip generate child element function |
| 61 | * |
| 62 | * @var callable $generate_child_element | function. |
| 63 | */ |
| 64 | private $generate_child_element = null; // function. |
| 65 | private $generate_element = null; // function. |
| 66 | /** |
| 67 | * Droip generate child element with new id function |
| 68 | * |
| 69 | * @var callable $generate_child_element_with_new_id | function. |
| 70 | */ |
| 71 | private $generate_child_element_with_new_id = null; // function. |
| 72 | private $get_data_and_styles_from_root = null; // function. |
| 73 | /** |
| 74 | * Droip element properties |
| 75 | * |
| 76 | * @var array $properties | array of element properties. |
| 77 | */ |
| 78 | private $properties = array(); |
| 79 | /** |
| 80 | * LMS course id |
| 81 | * |
| 82 | * @var int $course_id | course id. |
| 83 | */ |
| 84 | private $course_id = null; |
| 85 | |
| 86 | /** |
| 87 | * Class constructor |
| 88 | * |
| 89 | * @param array $props array of element properties. |
| 90 | * |
| 91 | * @since 1.0.0 |
| 92 | */ |
| 93 | public function __construct( $props ) { |
| 94 | $this->element = $props['element']; |
| 95 | $this->elements = $props['elements']; |
| 96 | $this->style_blocks = $props['style_blocks']; |
| 97 | $this->attributes = $props['attributes']; |
| 98 | $this->options = $props['options']; |
| 99 | $this->generate_child_element = $props['generate_child_element']; |
| 100 | $this->generate_element = $props['generate_element']; |
| 101 | $this->generate_child_element_with_new_id = $props['generate_child_element_with_new_id']; |
| 102 | $this->get_data_and_styles_from_root = $props['get_data_and_styles_from_root']; |
| 103 | $this->properties = $this->element['properties']; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * Generate elements |
| 109 | * |
| 110 | * $element: the element |
| 111 | * $attributes: elements html attributes |
| 112 | * $options: options is needed if any dynamic content need to be render |
| 113 | * $generate_child_element: this is a helper method for printing nested element or child elements. This method take 2 arg: 1. elemenbt id 2. $options. |
| 114 | * |
| 115 | * @return string html markup; |
| 116 | */ |
| 117 | public function generate_elements() { |
| 118 | switch ( $this->element['name'] ) { |
| 119 | case TDE_APP_PREFIX . '-course-meta': |
| 120 | return $this->generate_course_meta_markup(); |
| 121 | case TDE_APP_PREFIX . '-add-rating': |
| 122 | case TDE_APP_PREFIX . '-active-stars': |
| 123 | case TDE_APP_PREFIX . '-inactive-stars': |
| 124 | return $this->generate_add_rating_element(); |
| 125 | case TDE_APP_PREFIX . '-material': |
| 126 | return $this->generate_material_markup(); |
| 127 | case TDE_APP_PREFIX . '-action': |
| 128 | return $this->generate_action_markup(); |
| 129 | case TDE_APP_PREFIX . '-price': |
| 130 | return $this->generate_price_markup(); |
| 131 | case TDE_APP_PREFIX . '-price-value': |
| 132 | return $this->generate_price_value_markup(); |
| 133 | case TDE_APP_PREFIX . '-social-profile-link': |
| 134 | return $this->generate_social_link_markup(); |
| 135 | case TDE_APP_PREFIX . '-course-thumbnail': |
| 136 | return $this->generate_course_thumbnail_markup(); |
| 137 | |
| 138 | default: |
| 139 | return $this->generate_common_element(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Generate common element. |
| 145 | * |
| 146 | * @param bool $hide | false | true. | Hide element. |
| 147 | * @param bool $children_html | false | true. | Hide children. |
| 148 | * @return string |
| 149 | */ |
| 150 | private function generate_common_element( $hide = false, $children_html = false, $extra_attributes = '' ) { |
| 151 | $tag = isset( $this->properties['tag'] ) ? $this->properties['tag'] : 'div'; |
| 152 | $name = $this->element['name']; |
| 153 | if ( $hide ) { |
| 154 | $extra_attributes .= " data-element_hide='true'"; |
| 155 | } |
| 156 | // $extra_attributes .= " data-ele_name='$name'"; |
| 157 | |
| 158 | $html = ''; |
| 159 | if ( ! $children_html ) { |
| 160 | $children_html = $this->generate_child_elements(); |
| 161 | } |
| 162 | $html = "<$tag $this->attributes $extra_attributes>$children_html</$tag>"; |
| 163 | return $html; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Generate child elements. |
| 168 | * |
| 169 | * @return string |
| 170 | */ |
| 171 | private function generate_child_elements() { |
| 172 | $html = ''; |
| 173 | $child_count = isset( $this->element['children'] ) ? count( $this->element['children'] ) : 0; |
| 174 | for ( $i = 0; $i < $child_count; $i++ ) { |
| 175 | $html .= call_user_func( $this->generate_child_element, $this->element['children'][ $i ], $this->options ); |
| 176 | } |
| 177 | return $html; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Generate child element by name. |
| 182 | * |
| 183 | * @return array $element | Element array. |
| 184 | */ |
| 185 | private function group_elements_by_element_name() { |
| 186 | return array_reduce( |
| 187 | $this->element['children'], |
| 188 | function ( $carry, $item ) { |
| 189 | if ( isset( $this->elements[ $item ], $this->elements[ $item ]['name'] ) ) { |
| 190 | $carry[ $this->elements[ $item ]['name'] ] = $this->elements[ $item ]; |
| 191 | } |
| 192 | |
| 193 | return $carry; |
| 194 | }, |
| 195 | array() |
| 196 | ); |
| 197 | } |
| 198 | |
| 199 | private function get_all_data_and_styles_from_element_id( $element_id ) { |
| 200 | $data_n_styles = array( |
| 201 | 'blocks' => array(), |
| 202 | 'styles' => array(), |
| 203 | 'root' => $element_id, |
| 204 | ); |
| 205 | |
| 206 | call_user_func_array( $this->get_data_and_styles_from_root, array( $element_id, &$data_n_styles, &$this->elements, &$this->style_blocks ) ); |
| 207 | return $data_n_styles; |
| 208 | } |
| 209 | } |
| 210 |