PageRenderTime 58ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/t/upfront/elements/upfront-post-data/lib/class_upfront_post_data_frontend_view.php

https://bitbucket.org/matthewselby/wpdev
PHP | 232 lines | 172 code | 40 blank | 20 comment | 30 complexity | a504e9014cc7dbf65e4677b3fc046671 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, LGPL-3.0, LGPL-2.1, AGPL-1.0, BSD-3-Clause, MIT, GPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Front-end rendering class.
  4. */
  5. class Upfront_PostDataView extends Upfront_Object_Group {
  6. private $_post;
  7. protected $_child_instances = array();
  8. protected $_breakpoint = false;
  9. public function __construct ($data, $breakpoint = false) {
  10. $this->_breakpoint = $breakpoint;
  11. parent::__construct($data);
  12. }
  13. public function get_css_class () {
  14. $classes = parent::get_css_class();
  15. $this->get_post();
  16. $classes .= !empty($this->_post->ID) && is_sticky( $this->_post->ID ) ? " uf-post-data uf-post-data-sticky" : " uf-post-data";
  17. // if the post does not have a theme image, assign a class to denote that
  18. if(!empty($this->_post->ID) && !has_post_thumbnail($this->_post->ID)) {
  19. $classes .= ' no-feature-image';
  20. }
  21. return $classes;
  22. }
  23. public function get_post () {
  24. if (empty($this->_post)) {
  25. $data = $this->_properties_to_array();
  26. if (empty($data)) $data = Upfront_Post_Data_Data::get_defaults();
  27. $this->_post = Upfront_Post_Data_Model::spawn_post($data);
  28. }
  29. return $this->_post;
  30. }
  31. public function instantiate_child ($child_data, $idx) {
  32. $key = md5(serialize($child_data));
  33. if (!empty($this->_child_instances[$key])) return $this->_child_instances[$key];
  34. $view_class = upfront_get_property_value("view_class", $child_data);
  35. $view = $view_class
  36. ? "Upfront_{$view_class}"
  37. : $this->_child_view_class
  38. ;
  39. if (!class_exists($view)) $view = $this->_child_view_class;
  40. $this->_child_instances[$key] = new $view($child_data, $this->_data, $this);
  41. return $this->_child_instances[$key];
  42. }
  43. private function _properties_to_array(){
  44. $out = array();
  45. foreach($this->_data['properties'] as $prop) {
  46. $out[$prop['name']] = !empty($prop['value']) ? $prop['value'] : '';
  47. }
  48. return $out;
  49. }
  50. public static function default_properties () {
  51. return Upfront_Post_Data_Data::get_defaults();
  52. }
  53. public function get_propagated_classes () {
  54. $classes = array();
  55. foreach ($this->_child_instances as $part_view) {
  56. if (!is_callable(array($part_view, 'get_propagated_classes'))) continue;
  57. $classes = array_merge($classes, $part_view->get_propagated_classes());
  58. }
  59. return array_unique($classes);
  60. }
  61. public function get_attr () {
  62. $attr = parent::get_attr();
  63. $propagated = array($attr);
  64. foreach ($this->_child_instances as $part_view) {
  65. if (!is_callable(array($part_view, 'get_propagated_attr'))) continue;
  66. $propagated[] = $part_view->get_propagated_attr();
  67. }
  68. $propagated = array_values(array_unique(array_filter($propagated)));
  69. return empty($propagated)
  70. ? $attr
  71. : join(' ', $propagated) . ' '
  72. ;
  73. }
  74. public function get_preset() {
  75. $preset_map = $this->_get_preset_map($this->_data);
  76. $preset = $this->_get_preset($this->_data, $preset_map, $this->_breakpoint);
  77. return $preset;
  78. }
  79. }
  80. class Upfront_PostDataPartView extends Upfront_Object {
  81. private $_parent;
  82. private $_part_type;
  83. private $_part_view;
  84. private $_preset_id;
  85. private $_markup = array();
  86. public function __construct ($data, $parent_data = '', $parent_obj = false) {
  87. parent::__construct($data, $parent_data);
  88. if ($parent_obj !== false) {
  89. $this->_parent = $parent_obj;
  90. }
  91. $this->_part_type = $this->_get_property('part_type');
  92. $props = !empty($parent_data['properties'])
  93. ? upfront_properties_to_array($parent_data['properties'])
  94. : Upfront_Post_Data_Data::get_defaults()
  95. ;
  96. $props['preset'] = $this->_parent->get_preset();
  97. $props = Upfront_Post_Data_Data::apply_preset($props);
  98. $this->_preset_id = Upfront_Post_Data_Data::get_preset_id($props);
  99. $view_class = Upfront_Post_Data_PartView::_get_view_class($props);
  100. $this->_part_view = new $view_class(array_merge(
  101. $props,
  102. $parent_data
  103. ));
  104. }
  105. public function get_markup () {
  106. if (empty($this->_markup)) {
  107. $post = $this->_parent->get_post();
  108. // Try to get content from plugins compatibility
  109. $content = apply_filters('upfront-postdata_get_markup_before', false, $post->post_type);
  110. if (!empty($content)) {
  111. // Show only content part and insert plugins rendering to it
  112. $this->_markup['content'] = $content;
  113. }
  114. if (empty($this->_markup)) {
  115. $this->_markup = $this->_part_view->get_markup($post);
  116. }
  117. }
  118. return isset($this->_markup[$this->get_part_type()])
  119. ? $this->_markup[$this->get_part_type()]
  120. : ''
  121. ;
  122. }
  123. public function get_part_type () {
  124. return $this->_part_type;
  125. }
  126. public function get_propagated_classes () {
  127. $part_type = $this->get_part_type();
  128. $cls = $this->_part_view->get_propagated_classes();
  129. $cls[] = $part_type;
  130. // Add `upost-data-object-{part type}` class to allow applying custom css per post part type
  131. // For each type there are part parts that need to be translated to element type class
  132. // Post data
  133. if (in_array($part_type, array('title', 'date_posted', 'content') )) {
  134. $cls[] = 'upost-data-object-post_data';
  135. // Post taxonomy
  136. } else if (in_array($part_type, array('categories', 'tags') )) {
  137. $cls[] = 'upost-data-object-taxonomy';
  138. // Post author
  139. } else if (in_array($part_type, array('author', 'author-email', 'author-url', 'author-bio', 'gravatar') )) {
  140. $cls[] = 'upost-data-object-author';
  141. // Post comments
  142. } else if (in_array($part_type, array('comment_count', 'comments', 'comment_form', 'comment_pagination') )) {
  143. $cls[] = 'upost-data-object-comments';
  144. } else {
  145. // Meta and featured image have single class that matches type
  146. $cls[] = 'upost-data-object-' . $part_type;
  147. }
  148. // We apply preset class on ObjectGroup, commented this so we are not adding double class
  149. //if (!empty($this->_preset_id)) $cls[] = esc_attr($this->_preset_id);
  150. return $cls;
  151. }
  152. /**
  153. * Check the part view for propagated attributes other than class.
  154. *
  155. * @return string
  156. */
  157. public function get_propagated_attr () {
  158. return is_callable(array($this->_part_view, 'get_propagated_attr'))
  159. ? $this->_part_view->get_propagated_attr()
  160. : ''
  161. ;
  162. }
  163. public function get_style_for($point, $scope, $col = false) {
  164. $part_type = $this->_get_property('part_type');
  165. $css = '';
  166. if ( 'content' == $part_type && $col !== false ) {
  167. $left_indent = $this->_part_view->get_property('left_indent');
  168. $right_indent = $this->_part_view->get_property('right_indent');
  169. $max_col = $col - intval($left_indent) - intval($right_indent);
  170. $variants = Upfront_ChildTheme::getPostImageVariants();
  171. if (is_array($variants)) foreach ( $variants as $variant ) {
  172. $left = intval($variant->group->left);
  173. $margin_left = intval($variant->group->margin_left);
  174. $margin_right = intval($variant->group->margin_right);
  175. $variant_max_col = $max_col - $margin_left - $margin_right;
  176. if ( isset($variant->group->float) && 'none' == $variant->group->float && $point->is_default() ) {
  177. $variant_max_col -= $left;
  178. }
  179. $variant_max_col = $variant_max_col > $col ? $col : $variant_max_col;
  180. if (0 === $col) $col = 1;
  181. $max_width = sprintf('%.3f%%', floor(($variant_max_col/$col*100)*1000)/1000);
  182. $css .= sprintf('%s #%s %s {%s}',
  183. '.' . ltrim($scope, '. '),
  184. $this->get_id(),
  185. '[data-variant="' . $variant->vid . '"]',
  186. 'max-width: ' . $max_width . ';'
  187. ) . "\n";
  188. }
  189. }
  190. return $css;
  191. }
  192. }