PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/tiled-gallery/tiled-gallery/tiled-gallery-shape.php

https://gitlab.com/chernushov881/charity-fund
PHP | 209 lines | 168 code | 41 blank | 0 comment | 38 complexity | c801c88a6f292df3509dfc29a0cab09d MD5 | raw file
  1. <?php
  2. class Jetpack_Tiled_Gallery_Shape {
  3. static $shapes_used = array();
  4. public function __construct( $images ) {
  5. $this->images = $images;
  6. $this->images_left = count( $images );
  7. }
  8. public function sum_ratios( $number_of_images = 3 ) {
  9. return array_sum( array_slice( wp_list_pluck( $this->images, 'ratio' ), 0, $number_of_images ) );
  10. }
  11. public function next_images_are_symmetric() {
  12. return $this->images_left > 2 && $this->images[0]->ratio == $this->images[2]->ratio;
  13. }
  14. public function is_not_as_previous( $n = 1 ) {
  15. return ! in_array( get_class( $this ), array_slice( self::$shapes_used, -$n ) );
  16. }
  17. public function is_wide_theme() {
  18. return Jetpack::get_content_width() > 1000;
  19. }
  20. public function image_is_landscape( $image ) {
  21. return $image->ratio >= 1 && $image->ratio < 2;
  22. }
  23. public function image_is_portrait( $image ) {
  24. return $image->ratio < 1;
  25. }
  26. public function image_is_panoramic( $image ) {
  27. return $image->ratio >= 2;
  28. }
  29. public static function set_last_shape( $last_shape ) {
  30. self::$shapes_used[] = $last_shape;
  31. }
  32. public static function reset_last_shape() {
  33. self::$shapes_used = array();
  34. }
  35. }
  36. class Jetpack_Tiled_Gallery_Three extends Jetpack_Tiled_Gallery_Shape {
  37. public $shape = array( 1, 1, 1 );
  38. public function is_possible() {
  39. $ratio = $this->sum_ratios( 3 );
  40. $has_enough_images = $this->images_left >= 3 && ! in_array( $this->images_left, array( 4, 6 ) );
  41. return $has_enough_images && $this->is_not_as_previous( 3 ) &&
  42. ( ( $ratio < 2.5 ) || ( $ratio < 5 && $this->next_images_are_symmetric() ) || $this->is_wide_theme() );
  43. }
  44. }
  45. class Jetpack_Tiled_Gallery_Four extends Jetpack_Tiled_Gallery_Shape {
  46. public $shape = array( 1, 1, 1, 1 );
  47. public function is_possible() {
  48. return $this->is_not_as_previous() &&
  49. (
  50. ( $this->sum_ratios( 4 ) < 3.5 && $this->images_left > 5 ) ||
  51. ( $this->sum_ratios( 4 ) < 7 && $this->images_left == 4 )
  52. );
  53. }
  54. }
  55. class Jetpack_Tiled_Gallery_Five extends Jetpack_Tiled_Gallery_Shape {
  56. public $shape = array( 1, 1, 1, 1, 1 );
  57. public function is_possible() {
  58. return $this->is_wide_theme() && $this->is_not_as_previous() && $this->sum_ratios( 5 ) < 5 &&
  59. ( $this->images_left == 5 || ( $this->images_left != 10 && $this->images_left > 6 ) );
  60. }
  61. }
  62. class Jetpack_Tiled_Gallery_Two_One extends Jetpack_Tiled_Gallery_Shape {
  63. public $shape = array( 2, 1 );
  64. public function is_possible() {
  65. return $this->is_not_as_previous( 3 ) && $this->images_left >= 2 &&
  66. $this->images[2]->ratio < 1.6 && $this->images[0]->ratio >= 0.9 && $this->images[0]->ratio < 2.0 && $this->images[1]->ratio >= 0.9 && $this->images[1]->ratio < 2.0;
  67. }
  68. }
  69. class Jetpack_Tiled_Gallery_One_Two extends Jetpack_Tiled_Gallery_Shape {
  70. public $shape = array( 1, 2 );
  71. public function is_possible() {
  72. return $this->is_not_as_previous( 3 ) && $this->images_left >= 2 &&
  73. $this->images[0]->ratio < 1.6 && $this->images[1]->ratio >= 0.9 && $this->images[1]->ratio < 2.0 && $this->images[2]->ratio >= 0.9 && $this->images[2]->ratio < 2.0;
  74. }
  75. }
  76. class Jetpack_Tiled_Gallery_One_Three extends Jetpack_Tiled_Gallery_Shape {
  77. public $shape = array( 1, 3 );
  78. public function is_possible() {
  79. return $this->is_not_as_previous( 3 ) && $this->images_left > 3 &&
  80. $this->image_is_portrait( $this->images[0] ) &&
  81. $this->image_is_landscape( $this->images[1] ) &&
  82. $this->image_is_landscape( $this->images[2] ) &&
  83. $this->image_is_landscape( $this->images[3] );
  84. }
  85. }
  86. class Jetpack_Tiled_Gallery_Three_One extends Jetpack_Tiled_Gallery_Shape {
  87. public $shape = array( 3, 1 );
  88. public function is_possible() {
  89. return $this->is_not_as_previous( 3 ) && $this->images_left > 3 &&
  90. $this->image_is_portrait( $this->images[3] ) &&
  91. $this->image_is_landscape( $this->images[0] ) &&
  92. $this->image_is_landscape( $this->images[1] ) &&
  93. $this->image_is_landscape( $this->images[2] );
  94. }
  95. }
  96. class Jetpack_Tiled_Gallery_Panoramic extends Jetpack_Tiled_Gallery_Shape {
  97. public $shape = array( 1 );
  98. public function is_possible() {
  99. return $this->image_is_panoramic( $this->images[0] );
  100. }
  101. }
  102. class Jetpack_Tiled_Gallery_Symmetric_Row extends Jetpack_Tiled_Gallery_Shape {
  103. public $shape = array( 1, 2, 1 );
  104. public function is_possible() {
  105. return $this->is_not_as_previous( 5 ) &&
  106. $this->images_left > 3 &&
  107. $this->images_left != 5 &&
  108. $this->image_is_portrait( $this->images[0] ) &&
  109. $this->image_is_landscape( $this->images[1] ) &&
  110. $this->image_is_landscape( $this->images[2] ) &&
  111. $this->image_is_portrait( $this->images[3] );
  112. }
  113. }
  114. class Jetpack_Tiled_Gallery_Reverse_Symmetric_Row extends Jetpack_Tiled_Gallery_Shape {
  115. public $shape = array( 2, 1, 2 );
  116. public function is_possible() {
  117. return $this->is_not_as_previous( 5 ) && $this->images_left > 15 &&
  118. $this->image_is_landscape( $this->images[0] ) &&
  119. $this->image_is_landscape( $this->images[1] ) &&
  120. $this->image_is_portrait( $this->images[2] ) &&
  121. $this->image_is_landscape( $this->images[3] ) &&
  122. $this->image_is_landscape( $this->images[4] );
  123. }
  124. }
  125. class Jetpack_Tiled_Gallery_Long_Symmetric_Row extends Jetpack_Tiled_Gallery_Shape {
  126. public $shape = array( 3, 1, 3 );
  127. public function is_possible() {
  128. return $this->is_not_as_previous( 5 ) && $this->images_left > 15 &&
  129. $this->image_is_landscape( $this->images[0] ) &&
  130. $this->image_is_landscape( $this->images[1] ) &&
  131. $this->image_is_landscape( $this->images[2] ) &&
  132. $this->image_is_portrait( $this->images[3] ) &&
  133. $this->image_is_landscape( $this->images[4] ) &&
  134. $this->image_is_landscape( $this->images[5] ) &&
  135. $this->image_is_landscape( $this->images[6] );
  136. }
  137. }
  138. class Jetpack_Tiled_Gallery_Three_Columns extends Jetpack_Tiled_Gallery_Shape {
  139. public $shape = array();
  140. public function __construct( $images ) {
  141. parent::__construct( $images );
  142. $total_ratio = $this->sum_ratios( $this->images_left );
  143. $approximate_column_ratio = $total_ratio / 3;
  144. $column_one_images = $column_two_images = $column_three_images = $sum = 0;
  145. foreach ( $this->images as $image ) {
  146. if ( $sum <= $approximate_column_ratio ) {
  147. $column_one_images++;
  148. }
  149. if ( $sum > $approximate_column_ratio && $sum <= 2 * $approximate_column_ratio ) {
  150. $column_two_images++;
  151. }
  152. $sum += $image->ratio;
  153. }
  154. $column_three_images = $this->images_left - $column_two_images - $column_one_images;
  155. if ( $column_one_images ) {
  156. $this->shape[] = $column_one_images;
  157. }
  158. if ( $column_two_images ) {
  159. $this->shape[] = $column_two_images;
  160. }
  161. if ( $column_three_images ) {
  162. $this->shape[] = $column_three_images;
  163. }
  164. }
  165. public function is_possible() {
  166. return ! empty( $this->shape );
  167. }
  168. }